保存整个控件为图片

       在vs2003中,可以通过MSChart.EditCopy()方法,再从简帖板(ClipBoard)获得已经绘制的图片,然后再进行保存图片或者打印操作。

public class Win32
{
[System.Runtime.InteropServices.DllImport("gdi32", EntryPoint = "BitBlt")]
 public static extern int BitBlt   (int hDestDC, int x, int y, int nWidth, int nHeight, int hSrcDC, int xSrc, int ySrc, int dwRop);
        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "GetWindowDC")]
        public static extern int GetWindowDC(int hwnd);
        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "ReleaseDC")]
        public static extern int ReleaseDC(int hwnd, int hdc);
        public const int SRCCOPY = 13369376;
 }
 

 借用这个辅助类生成控件的图形:

/// <summary>
/// 绘制整个控件位BitMap
/// </summary>
/// <param name="Control">要绘制的控件</param>
 /// <returns></returns>

public static Bitmap CreateBitmap(Control Control)
{
            Graphics gDest;
            IntPtr hdcDest;
           int hdcSrc;
            int hWnd = Control.Handle.ToInt32();
            Bitmap BmpDrawed = new Bitmap(Control.Width, Control.Height);
            gDest = Graphics.FromImage(BmpDrawed);
            hdcSrc = Win32.GetWindowDC(hWnd);
            hdcDest = gDest.GetHdc();
            Win32.BitBlt(hdcDest.ToInt32(), 00, Control.Width, Control.Height, hdcSrc, 00, Win32.SRCCOPY);
            gDest.ReleaseHdc(hdcDest);
            Win32.ReleaseDC(hWnd, hdcSrc);
           return BmpDrawed;
}

       在Vs2005中,执行了MSChart.EditCopy()方法之后再次取用剪贴板就会报错,而且是很奇快的内存错误。于是上MS的Support网站寻求相关的帮助,终于搜集到了解决的办法。在这里向大家推荐一个通用的办法,可以保存Control的任何状态为图形。
   需要增加一个辅助类:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值