在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();
            }

解决方案 »

  1.   

    [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();
    }
      

  2.   

    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方法.
      

  3.   

    顺便问一个菜鸟问题:
    楼主所说的:条形码(BarcodeControl)控件中个啥东东啊
      

  4.   

    上面说的都比较麻烦 - -!这样试试,先获得panel1.CreateGraphics(),将此Graphics 对象来绘制一副图片
    然后重写OnPrintPage()方法(注意别搞错方法)
    应该就可以了
      

  5.   

    我这有个笨方法,取得PANEL的屏幕X和Y的值
    然后通过屏幕打印截图,然后打印图片文件,这样是不是会快点。
      

  6.   

    谢谢各位大哥了!!!
     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
      

  7.   

    如果要保存这个控件中的所有内容,该怎么做???
    怎么捕捉到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();
                    }
                }
            }
      

  8.   

    Cobainsoft那个控件不能设置透明背景,哪位高手知道怎么弄?