zoukankan      html  css  js  c++  java
  • 截取控件界面

    介绍:
     做项目中,遇到需要截取界面图形的内容,查找了所有的Graphics函数,
     都没有此功能。只能用GDI32中的BitBlt来解决。
    应用需要调用的函数
      class GDI32
     {
      [DllImport("GDI32.dll")]
      public static extern bool BitBlt(int hdcDest,int nXDest,int nYDest,
       int nWidth,int nHeight,int hdcSrc,
       int nXSrc,int nYSrc,int dwRop);
      [DllImport("GDI32.dll")]
      public static extern int CreateCompatibleBitmap(int hdc,int nWidth,
       int nHeight);
      [DllImport("GDI32.dll")]
      public static extern int CreateCompatibleDC(int hdc);
      [DllImport("GDI32.dll")]
      public static extern bool DeleteDC(int hdc);
      [DllImport("GDI32.dll")]
      public static extern bool DeleteObject(int hObject);
      [DllImport("GDI32.dll")]
      public static extern int GetDeviceCaps(int hdc,int nIndex);
      [DllImport("GDI32.dll")]
      public static extern int SelectObject(int hdc,int hgdiobj);

      
     
      public static void Cleanup(int hBitmap,int hdcSrc,int hdcDest)
      {
       // Release the device context resources back to the system
       User32.ReleaseDC(User32.GetDesktopWindow(),hdcSrc);
       GDI32.DeleteDC(hdcDest);
       GDI32.DeleteObject(hBitmap); 
      }
     
      public static void SaveImageAs(int hBitmap,string fileName,ImageFormat imageFormat)
      {
       // Create a bitmap from the Windows handle
       Bitmap image =
        new Bitmap(Image.FromHbitmap(new IntPtr(hBitmap)),
        Image.FromHbitmap(new IntPtr(hBitmap)).Width,
        Image.FromHbitmap(new IntPtr(hBitmap)).Height);
       image.Save(fileName,imageFormat);
      }    

     
     }
     class User32
     {
      [DllImport("User32.dll")]
      public static extern int GetDesktopWindow();
      [DllImport("User32.dll")]
      public static extern int GetWindowDC(int hWnd);
      [DllImport("User32.dll")]
      public static extern int ReleaseDC(int hWnd,int hDC);
     }
    具体实现的方式
         和C++中的代码类似
            int hdcSrc = User32.GetWindowDC((int)this.control.Handle), // 获得控件的Handle
         hdcDest = GDI32.CreateCompatibleDC(hdcSrc), // 创建兼容DC
         hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc,
       this.control.Width,this.control.Height);//创建
         GDI32.SelectObject(hdcDest,hBitmap); // 选择
         GDI32.BitBlt(hdcDest,0,0,this.control.Width,
       this.control.Height,
       hdcSrc,0,0,0x00CC0020);//绘制图形
         Bitmap image = new Bitmap(Image.FromHbitmap(new IntPtr(hBitmap)),
       Image.FromHbitmap(new IntPtr(hBitmap)).Width,
       Image.FromHbitmap(new IntPtr(hBitmap)).Height);//转换为.net的Image类型
        GDI32.Cleanup(hBitmap,hdcSrc,hdcDest);//清除句柄

  • 相关阅读:
    AndroidStudio开发体温上报系统------问题总结
    AndroidStudio--app是如何运行的
    sqlite操作
    sqlite数据库
    Android Service
    echart自定义主题
    vue监听数组变化
    Django:数据库驱动安装
    pycharm链接mysql报错: Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property manually.
    Django2.2:UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: illegal multibyte sequence
  • 原文地址:https://www.cnblogs.com/wanghualiang/p/320709.html
Copyright © 2011-2022 走看看