在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(), 0, 0, Control.Width, Control.Height, hdcSrc, 0, 0, Win32.SRCCOPY);
gDest.ReleaseHdc(hdcDest);
Win32.ReleaseDC(hWnd, hdcSrc);
return BmpDrawed;
}
在Vs2005中,執行了MSChart.EditCopy()方法之後再次取用剪貼簿就會報錯,而且是很奇快的記憶體錯誤。於是上MS的Support網站尋求相關的協助,終於搜集到瞭解決的辦法。在這裡向大家推薦一個通用的辦法,可以儲存Control的任何狀態為圖形。
需要增加一個輔助類: