首页 > 解决方案 > 如何从 ActiveX 组件中保存位图

问题描述

尝试保存 AxMsTscAxNotSafeForScripting(Microsoft 终端服务客户端控件)的屏幕,但找不到任何有关它的信息...

我尝试了一个代码,但它只筛选元素字段... https://i.imgur.com/fo6jKDT.png

    [Flags]
    private enum DrawingOptions
    {
        PRF_CHECKVISIBLE = 0x00000001,
        PRF_NONCLIENT = 0x00000002,
        PRF_CLIENT = 0x00000004,
        PRF_ERASEBKGND = 0x00000008,
        PRF_CHILDREN = 0x00000010,
        PRF_OWNED = 0x00000020
    }

    private const uint WM_PAINT = 0xF;


    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr dc, DrawingOptions opts);

    var bm = new Bitmap(rdp.Width, rdp.Height, rdp.CreateGraphics());

        using (Graphics g = Graphics.FromImage(bm))
        {
            IntPtr dc = g.GetHdc();
            try
            {
                SendMessage(rdp.Handle, WM_PAINT, dc,
                DrawingOptions.PRF_CLIENT |
                DrawingOptions.PRF_NONCLIENT |
                DrawingOptions.PRF_CHILDREN);
            }
            finally
            {
                g.ReleaseHdc();
            }
        }
        pictureBox1.Image = bm;

标签: c#bitmapactivex

解决方案


此代码有助于保存表单图像并完美显示 ActiveX。由于该项目是静态的,我将只裁剪图像并获得我想要的屏幕截图。

 [DllImport("user32.dll")]
 public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
 [DllImport("user32.dll")]
 public static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags);

 public static Bitmap PrintWindow(IntPtr hwnd)    
 {       
      RECT rc;        
      GetWindowRect(hwnd, out rc);

      Bitmap bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb);        
      Graphics gfxBmp = Graphics.FromImage(bmp);        
      IntPtr hdcBitmap = gfxBmp.GetHdc();        

      PrintWindow(hwnd, hdcBitmap, 0);  

      gfxBmp.ReleaseHdc(hdcBitmap);               
      gfxBmp.Dispose(); 

      return bmp;   
 }

推荐阅读