Anchky's Tech Blog
专注.NET 专注BI
尽最大的努力,以求更好!

    在vs2005中,MSchart的图片保存遇到了问题。

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

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

 1/// <summary>
 2    /// 辅助类
 3    /// </summary>

 4    public class Win32
 5    {
 6        [System.Runtime.InteropServices.DllImport("gdi32", EntryPoint = "BitBlt")]
 7        public static extern int BitBlt(int hDestDC, int x, int y, int nWidth, int nHeight, int hSrcDC, int xSrc, int ySrc, int dwRop);
 8        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "GetWindowDC")]
 9        public static extern int GetWindowDC(int hwnd);
10        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "ReleaseDC")]
11        public static extern int ReleaseDC(int hwnd, int hdc);
12        public const int SRCCOPY = 13369376;
13
14    }
      借用这个辅助类生成控件的图形:
 1/// <summary>
 2        /// 绘制整个控件位BitMap
 3        /// </summary>
 4        /// <param name="Control">要绘制的控件</param>
 5        /// <returns></returns>

 6        public static Bitmap CreateBitmap(Control Control)
 7        {
 8            Graphics gDest;
 9            IntPtr hdcDest;
10            int hdcSrc;
11            int hWnd = Control.Handle.ToInt32();
12            Bitmap BmpDrawed = new Bitmap(Control.Width, Control.Height);
13            gDest = Graphics.FromImage(BmpDrawed);
14            hdcSrc = Win32.GetWindowDC(hWnd);
15            hdcDest = gDest.GetHdc();
16            Win32.BitBlt(hdcDest.ToInt32(), 00, Control.Width, Control.Height, hdcSrc, 00, Win32.SRCCOPY);
17            gDest.ReleaseHdc(hdcDest);
18            Win32.ReleaseDC(hWnd, hdcSrc);
19            return BmpDrawed;
20        }

posted on 2006-10-21 12:50  anchky  阅读(3077)  评论(9编辑  收藏  举报