在vs2005中,MSchart的图片保存遇到了问题。
在vs2003中,可以通过MSChart.EditCopy()方法,再从简帖板(ClipBoard)获得已经绘制的图片,然后再进行保存图片或者打印操作。
在Vs2005中,执行了MSChart.EditCopy()方法之后再次取用剪贴板就会报错,而且是很奇快的内存错误。于是上MS的Support网站寻求相关的帮助,终于搜集到了解决的办法。在这里向大家推荐一个通用的办法,可以保存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
}
借用这个辅助类生成控件的图形:
/// <summary>2
/// 辅助类3
/// </summary>4
public class Win325
{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(), 0, 0, Control.Width, Control.Height, hdcSrc, 0, 0, Win32.SRCCOPY);
17
gDest.ReleaseHdc(hdcDest);
18
Win32.ReleaseDC(hWnd, hdcSrc);
19
return BmpDrawed;
20
}
/// <summary>2
/// 绘制整个控件位BitMap3
/// </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(), 0, 0, Control.Width, Control.Height, hdcSrc, 0, 0, Win32.SRCCOPY);17
gDest.ReleaseHdc(hdcDest);18
Win32.ReleaseDC(hWnd, hdcSrc);19
return BmpDrawed;20
}
陈国安
杭州三汇数字信息技术有限公司
杭州市文二路385#汽轮大厦7楼,310012
杭州三汇数字信息技术有限公司
杭州市文二路385#汽轮大厦7楼,310012


浙公网安备 33010602011771号