[C #] failed to use bitblt in GDI + to draw the image to the window

Source: Internet
Author: User

The Code is as follows. Draw a graph to the window and the result is only a black rectangle:

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);

Here, I want to use the raster-Operation Capability of bitblt to draw an image to graphics without using drawimage. Specifically, I have two images in my hand, one of them is a hidden color and requires and then the OR operation. Therefore, the bitblt of GDI must be used.

Solution:

 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 }

That is to say, before bitblt, select the bitmap associated with graphics as the DC background image.

Guess, use graphics. fromimage ,. at the bottom layer of net, the hbitmap corresponding to bitmap is not selected into HDC, but only two. net object, and then SelectObject is selected and selected twice in drawimage to draw on hbitmap. Therefore, when performing a GDI operation independently of. net, you need to associate the two at the GDI level and use SelectObject.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.