高分求高手指点!!打印Panel控件中的所有内容???(问题解决就给分,不够再加!!)
在Panel控件中,有条形码(BarcodeControl)控件和pictureBox控件和一个Label,这三个组合在Panel控件中,我要把Panel控件的这些控件内容都打印和保存下来,代码该怎么写???
我自己写了点打印的,一直不对~~~~~
那为高手指点下!!!!!
private void SaveButton2_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
PrintPreviewDialog cppd = new PrintPreviewDialog();
cppd.Document = pd;
cppd.ShowDialog();
}
void pd_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = panel1.CreateGraphics();
Size s = panel1.Size;
Bitmap b = new Bitmap(s.Width, s.Height, g);
Graphics g2 = Graphics.FromImage(b);
IntPtr dc1 = g.GetHdc();
IntPtr dc2 = g2.GetHdc();
g.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
g.Dispose();
g2.Dispose();
}
[解决办法]
[System.Runtime.InteropServices.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()
{
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);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
}
private void printButton_Click(System.Object sender, System.EventArgs e)
{
CaptureScreen();
printPreviewDialog1.Show();
PrintDocument1.Print();
}
[解决办法]
1:你在事件pd_PrintPage里进行绘制一定要使用参数e.Graphics,而不是再使用其它Graphics,否则不会打印出来.
2:你要打印窗体或控件的内容,简单的可以使用Control.DrawToBitmap方法来先生成图片,然后在pd_PrintPage事件里使用e.Graphics.DrawImage()把这个图片打印到打印文档中.参考如下:
void pd_PrintPage(object sender, PrintPageEventArgs e)
{
Bitmap bit = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(bit, new Rectangle(0,0, this.Width, this.Height));
e.Graphics.DrawImage(bit, 0,0);
bit.Dispose();
}
因为有的控件不直接支持DrawToBitmap方法,因此还要处理控件的OnPrint方法,注意不是OnPaint方法.
[解决办法]
上面说的都比较麻烦 - -!
这样试试,先获得panel1.CreateGraphics(),将此Graphics 对象来绘制一副图片
然后重写OnPrintPage()方法(注意别搞错方法)
应该就可以了
[解决办法]
我这有个笨方法,取得PANEL的屏幕X和Y的值
然后通过屏幕打印截图,然后打印图片文件,这样是不是会快点。