在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();
}
我自己写了点打印的,一直不对~~~~~
那为高手指点下!!!!!
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();
}
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();
}
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方法.
楼主所说的:条形码(BarcodeControl)控件中个啥东东啊
然后重写OnPrintPage()方法(注意别搞错方法)
应该就可以了
然后通过屏幕打印截图,然后打印图片文件,这样是不是会快点。
zh_li_() ( ) 信誉:100 Blog 加为好友 2007-07-12 09:31:27 得分: 0
顺便问一个菜鸟问题:
楼主所说的:条形码(BarcodeControl)控件中个啥东东啊
BarcodeControl是科本® .NET 条形码控件(Cobainsoft® .NET BarCode Control®);
具体看:
http://www.cobainsoft.com/downloads/DownloadDetail.aspx?id=12
怎么捕捉到panel控件中的所有内容该怎么写??
谢谢各位了!!!
private void vistaButton1_Click(object sender, EventArgs e)
{
System.IO.Stream myStream;
SaveFileDialog fileDialog = new SaveFileDialog();
fileDialog.Filter = "GIF files (*.gif)|*.gif|PNG files (*.png)|*.png|JPEG files (*.jpg;*.jpeg)|*.jpg;*.jpeg|BMP files (*.bmp;*.dib)|*.bmp;*.dib|TIFF files (*.tif)|*.tif";
fileDialog.FilterIndex = 1;
fileDialog.FileName = DateTime.Now.ToString("yyyyMMddHHmmss");
fileDialog.RestoreDirectory = false;
ImageFormat[] afmt = { ImageFormat.Gif, ImageFormat.Png, ImageFormat.Jpeg, ImageFormat.Bmp, ImageFormat.Tiff }; if (fileDialog.ShowDialog() == DialogResult.OK)
{
if ((myStream = fileDialog.OpenFile()) != null)
{
int panelWidth = 1;
int panelHeight = 80; //barcodeControl2.MakeImage(afmt[fileDialog.FilterIndex - 1], panelWidth, panelHeight, true, false, null, myStream);//保存获取条形码图片
myStream.Close();
}
}
}