首先要選用容器控件,將需要的控件加入到容器中。
例如:panel容器
Bitmap newbitmap = new Bitmap(panelW.Width, panelW.Height);
panelW.DrawToBitmap(newbitmap, new Rectangle(0, 0, newbitmap.Width, newbitmap.Height));
newbitmap.Save(@"F:\yellowriver\aa\2011\04\25\test.gif");
保存整個控件為圖片
http://www.cnblogs.com/anchky/archive/2006/10/21/savectlaspic.html
在vs2005中,MSchart的圖片保存遇到了問題。
在vs2003中,可以通過MSChart.EditCopy()方法,再從簡帖板(ClipBoard)獲得已經(jīng)繪制的圖片,然后再進行保存圖片或者打印操作。
在Vs2005中,執(zhí)行了MSChart.EditCopy()方法之后再次取用剪貼板就會報錯,而且是很奇快的內(nèi)存錯誤。于是上MS的Support網(wǎng)站尋求相關(guān)的幫助,終于搜集到了解決的辦法。在這里向大家推薦一個通用的辦法,可以保存Control的任何狀態(tài)為圖形。
需要增加一個輔助類:
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(), 0, 0, Control.Width, Control.Height, hdcSrc, 0, 0, Win32.SRCCOPY);
17
gDest.ReleaseHdc(hdcDest);
18
Win32.ReleaseDC(hWnd, hdcSrc);
19
return BmpDrawed;
20
}