c# 如何对窗体进行打印预览 20
代码部分:[DllImport("gdi32.dll")]publicstaticexternlongBitBlt(IntPtrhdcDest,intnXDest,int...
代码部分:
[DllImport("gdi32.dll")]
public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private void CaptureScreen()
{
try
{
Graphics mygraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void cmdPrintPreview_Click(object sender, System.EventArgs e)
{
try
{
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
CaptureScreen();
printPreviewDialog1.Document = this.printDocument1;
printPreviewDialog1.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
//MessageBox.Show("Not implemented yet.");
}
void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//Graphics g = e.Graphics; //获得绘图对象
e.Graphics.DrawImage(memoryImage, 0, 0);
}
当页面设置中,将纸张大小设置为自定义大小时,运行到printPreviewDialog1.Show();时,出现异常GDI+ 中发生一般性错误。 展开
[DllImport("gdi32.dll")]
public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private void CaptureScreen()
{
try
{
Graphics mygraphics = this.CreateGraphics();
Size s = this.Size;
memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void cmdPrintPreview_Click(object sender, System.EventArgs e)
{
try
{
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
CaptureScreen();
printPreviewDialog1.Document = this.printDocument1;
printPreviewDialog1.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
//MessageBox.Show("Not implemented yet.");
}
void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//Graphics g = e.Graphics; //获得绘图对象
e.Graphics.DrawImage(memoryImage, 0, 0);
}
当页面设置中,将纸张大小设置为自定义大小时,运行到printPreviewDialog1.Show();时,出现异常GDI+ 中发生一般性错误。 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询