我想将打印内容写入JPG文件中,但不知BitBlt函数在C#中如何引用?或者有别的办法也行,拜托了。

解决方案 »

  1.   

    [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
            private static extern bool BitBlt(
                 IntPtr hdcDest, // handle to destination DC
                 int nXDest, // x-coord of destination upper-left corner
                 int nYDest, // y-coord of destination upper-left corner
                 int nWidth, // width of destination rectangle
                 int nHeight, // height of destination rectangle
                 IntPtr hdcSrc, // handle to source DC
                 int nXSrc, // x-coordinate of source upper-left corner
                 int nYSrc, // y-coordinate of source upper-left corner
                 System.Int32 dwRop // raster operation code
            );
      

  2.   

    非常感谢您的回帖,软件也通过了编译,但生成的2.jpg是空的。
    代码如下,请您再帮我看看。
     [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")] 
            private static extern bool BitBlt( 
                IntPtr hdcDest, // handle to destination DC 
                int nXDest, // x-coord of destination upper-left corner 
                int nYDest, // y-coord of destination upper-left corner 
                int nWidth, // width of destination rectangle 
                int nHeight, // height of destination rectangle 
                IntPtr hdcSrc, // handle to source DC 
                int nXSrc, // x-coordinate of source upper-left corner 
                int nYSrc, // y-coordinate of source upper-left corner 
                System.Int32 dwRop // raster operation code 
            );        private void PrintToFile(string name, PrintPageEventArgs e)
            {
                Bitmap bm1 = new Bitmap("c:\\1.jpg");
                Graphics g1 = Graphics.FromImage(bm1); //这仅是测试,实为 g1 = e.Graphics; 两者生成的2.jpg都是空的。
                IntPtr dc1 = g1.GetHdc();
               
                Bitmap bm2 = new Bitmap(810, 1150);
                Graphics g2 = Graphics.FromImage(bm2);
                IntPtr dc2 = g2.GetHdc();
                
                bool ll = false;
                ll=BitBlt(dc2, 0, 0, 810, 1150, dc1, 0, 0, 13369376);  // ll=true;
                g1.ReleaseHdc(dc1);
                g2.ReleaseHdc(dc2);            bm2.Save("c:\\2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);            bm1.Dispose();
                bm2.Dispose();
                            
            }
      

  3.   


    Graphics g1 = this.CreateGraphics();
    Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);
    Graphics g2 = Graphics.FromImage(MyImage);
    IntPtr dc1 = g1.GetHdc();
    IntPtr dc2 = g2.GetHdc();
    BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
    g1.ReleaseHdc(dc1);
    g2.ReleaseHdc(dc2);
    MyImage.Save(@"e:\1.bmp", ImageFormat.Bmp);
      

  4.   

    谢谢,非常感谢,
    确实是保存了this窗口的内容,但打印预览的内容无法保存。
    非常想在“打印预览”按钮上,把打印的内容保存为BMP,有招吗?
      

  5.   

    // PrintEventArgs e
     想用Graphics g1 = e.Graphics;代替您的Graphics g1 = this.CreateGraphics();
    但结果e:\1.bmp是空白,为什么呢?在e.Graphics中画图像都可以,就是考不到Image中。