C# - ActiveX .DrawToBitmap等价物

时间:2010-11-09 15:49:30

标签: c# activex

简短问题:

如何将隐藏的ActiveX控件的副本绘制到位图?

更长的问题:

我有一个显示图像的ActiveX控件。我想将它隐藏在某些东西后面(例如一个面板),将显示复制到Bitmap以便能够处理它(例如访问像素等)。

1 个答案:

答案 0 :(得分:3)

诀窍是你应该使用WM_PAINT

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

    private void panel1_MouseClick(object sender, MouseEventArgs e)
    {
        using (Bitmap bm = new Bitmap(axChartSpace1.Width, axChartSpace1.Height))
        {
            using (Graphics g = Graphics.FromImage(bm))
            {
                IntPtr dc = g.GetHdc();
                try
                {
                    SendMessage(axChartSpace1.Handle, WM_PRINT, dc,
                    DrawingOptions.PRF_CLIENT |
                    DrawingOptions.PRF_NONCLIENT |
                    DrawingOptions.PRF_CHILDREN);
                }
                finally
                {
                    g.ReleaseHdc();
                }
                bm.Save(@"C:\1.bmp");
            }
        }
    }