[C#]GDI+中使用BitBlt繪製映像到視窗失敗

來源:互聯網
上載者:User

  代碼如下,將一張圖繪製到視窗上,結果只有一個全黑的矩形:

1 Bitmap bmp = (Bitmap)Bitmap.FromFile(@"C:\Users\Ken\Desktop\Load2.bmp"); 
2 Graphics grDest = Graphics.FromHwnd(pictureBox1.Handle);
3 Graphics grSrc = Graphics.FromImage(bmp);
4 IntPtr hdcDest = grDest.GetHdc();
5 IntPtr hdcSrc = grSrc.GetHdc();
6 BitBlt(hdcDest, 0, 0, pictureBox1.Width, pictureBox1.Height,
7 hdcSrc, 0, 0, (uint)TernaryRasterOperations.SRCCOPY); // 0x00CC0020
8 grDest.ReleaseHdc(hdcDest);
9 grSrc.ReleaseHdc(hdcSrc);

  在這裡,我將Image繪製到Graphics上,不使用DrawImage,是想使用BitBlt的Raster-Operation能力,具體來說,我手裡有兩張圖,其中一張是掩色,需要先And再Or的操作,所以必須使用GDI的BitBlt。

  解決方案: 

 1 using (Bitmap bmp = (Bitmap)Bitmap.FromFile(@"C:\Jason\forest.jpg")) 
2 using (Graphics grDest = Graphics.FromHwnd(pictureBox1.Handle))
3 using (Graphics grSrc = Graphics.FromImage(bmp))
4 {
5 IntPtr hdcDest = IntPtr.Zero;
6 IntPtr hdcSrc = IntPtr.Zero;
7 IntPtr hBitmap = IntPtr.Zero;
8 IntPtr hOldObject = IntPtr.Zero;
9 try
10 {
11 hdcDest = grDest.GetHdc();
12 hdcSrc = grSrc.GetHdc();
13 hBitmap = bmp.GetHbitmap();
14 hOldObject = SelectObject(hdcSrc, hBitmap);
15 if (hOldObject == IntPtr.Zero)
16 throw new Win32Exception();
17 if (!BitBlt(hdcDest, 0, 0, pictureBox1.Width, pictureBox1.Height, hdcSrc, 0, 0, 0x00CC0020U))
18 throw new Win32Exception(); }
19 finally
20 {
21 if (hOldObject != IntPtr.Zero) SelectObject(hdcSrc, hOldObject);
22 if (hBitmap != IntPtr.Zero) DeleteObject(hBitmap);
23 if (hdcDest != IntPtr.Zero) grDest.ReleaseHdc(hdcDest);
24 if (hdcSrc != IntPtr.Zero) grSrc.ReleaseHdc(hdcSrc);
25 }
26 }

  也就是說,在BitBlt之前,先將和Graphics有聯絡的Bitmap選作DC的背景圖。

  猜測,使用Graphics.FromImage的時候,.Net底層並沒有將Bitmap對應的HBitmap選入HDC而只是將兩個.Net對象聯絡起來了,而後在DrawImage中有兩次SelectObject分別選入和選出以在HBitmap上繪圖。所以獨立於.Net進行GDI操作的時候,需要在GDI層面再將兩者聯絡起來,使用SelectObject。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.