使用api函数来截图,但是保存的文件却是全黑的,这问题到底出在哪啊?
下边是核心的代码,问题肯定出在这里边:int hdc=GetDC(0);
Graphics g=Graphics.FromHdc((IntPtr)hdc);
Bitmap bmp=new Bitmap(1024,768,g);
Graphics g2 = Graphics.FromImage(bmp);                            
IntPtr dc2 = g2.GetHdc();
BitBlt((IntPtr)dc2, 0, 0, 1024, 768, (IntPtr)hdc, 0, 0,13369376);
bmp.Save(@"c:\603.bmp");

解决方案 »

  1.   

    我的函数是保存到一个byte[]中,再存入数据库,你去看看是否有帮助,我的是可以实现的public static byte[] captureWinformImage(System.Windows.Forms.Control obj)
    {
    System.Drawing.Bitmap oB = new System.Drawing.Bitmap(obj.Width, obj.Height);
    System.Drawing.Graphics gForm = obj.CreateGraphics();
    IntPtr iHdcForm = gForm.GetHdc(); 
    System.Drawing.Graphics gBitmap = System.Drawing.Graphics.FromImage(oB); 
    IntPtr iHdcBitmap = gBitmap.GetHdc(); 
    int iResultBitBlt = BitBlt(iHdcBitmap,0,0,
    obj.ClientRectangle.Width, obj.ClientRectangle.Height, 
    iHdcForm, 0,0,SRCCOPY); 
    gBitmap.ReleaseHdc(iHdcBitmap);
    gForm.ReleaseHdc(iHdcForm); gBitmap.Dispose();
    gForm.Dispose();  System.IO.MemoryStream mStream = new System.IO.MemoryStream();
    oB.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg);
    mStream.Flush();
    mStream.Seek(0, System.IO.SeekOrigin.Begin);
    byte[] bytes = new byte[(int)mStream.Length];
    mStream.Read(bytes, 0, (int)mStream.Length);
    return bytes;
    }
      

  2.   

    int hdc=GetDC(0);
    问题在这里,要跟控件关联
      

  3.   

    我希望截的是全屏的图啊,如果和控件关联了,全屏的图不就截不到了么? wangrhliuyh(TTL) 老兄的程序也是只能得到程序窗口里的图吧?
    int hdc=GetDC(0);
    这里边的getdc是api 函数,这个真邪门。
      

  4.   

    全屏的hdc用这个试试CreateDC("DISPLAY", null, null, IntPtr.Zero);
      

  5.   

    [ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
    private static extern bool BitBlt (
    IntPtr hdcDest , //目标设备的句柄
    int nXDest , // 目标对象的左上角的X坐标
    int nYDest , // 目标对象的左上角的X坐标
    int nWidth , // 目标对象的矩形的宽度
    int nHeight , // 目标对象的矩形的长度
    IntPtr hdcSrc , // 源设备的句柄
    int nXSrc , // 源对象的左上角的X坐标
    int nYSrc , // 源对象的左上角的X坐标
    System.Int32 dwRop // 光栅的操作值
    ) ;
    [ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
    private static extern IntPtr CreateDC (
    string lpszDriver , // 驱动名称
    string lpszDevice , // 设备名称
    string lpszOutput , // 无用,可以设定位"NULL"
    IntPtr lpInitData // 任意的打印机数据
    ) ;IntPtr dc1 = CreateDC ( "DISPLAY" , null , null , ( IntPtr ) null ) ;
    Graphics g=Graphics.FromHdc(dc1);
    Bitmap map=new Bitmap(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height,g);
    Graphics g1=Graphics.FromImage(map);
    IntPtr dc3 = g.GetHdc() ;
    IntPtr dc2 = g1.GetHdc() ;
    BitBlt(dc2,0,0,System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height,dc3,0,0,13369376);
    g.ReleaseHdc(dc3);
    g1.ReleaseHdc(dc2); string dt=DateTime.Now.ToString();
    g1.DrawString(DateTime.Now.ToString(),new Font("宋体",36),new SolidBrush(Color.Red),new Point(430,330)); try
    {
    map.Save(@"C:\Inetpub\wwwroot\dw\aaa.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    catch
    {
    }
    finally
    {
    map.Dispose();
    GC.Collect();
    }一个全屏截图代码,
    测试完全可用。
      

  6.   

    会截出黑图我也遇到过,
    上面的代码如果我放在web页面或windows服务
    就会全黑。只有在win form里面才能正确截到。后来我在windows服务里面勾上“允许服务与桌面交互”
    就不会出黑图的问题,
    估计你也是这个问题。
      

  7.   

    我这个也是win form程序,咱俩的程序好像就差在getdc和createdc上,而getdc我在其他语言中能够正常使用,为什么在c#就不行了?深层次原因是什么啊?