儲存整個控制項為圖片

來源:互聯網
上載者:User

       在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的任何狀態為圖形。
   需要增加一個輔助類:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.