How to display and process images in VC

Source: Internet
Author: User

Method 1: This method actually uses a library for reading bitmap in The Book of classic VC image processing,

Without this function library, there is not much practical value.
This method directly uses the function library for reading and displaying BMP images.
First, you must obtain the location of the area to be displayed:
Cwnd * pwnd = getdlgitem (idc_bmp );
Rect;
Pwnd-> getclientrect (& rect );
CDC * PDC = pwnd-> getdc ();
Then call the function library
// Obtain the DIB image width
Int cxdib = (INT): dibwidth (lpdib );
// Obtain the DIB image height
Int cydib = (INT): dibheight (lpdib );
Finally, the function library is called.
// Call paintdib to output the image
: Paintdib (PDC-> m_hdc, & rect, m_hdib, & rcdib, null );
Release resources
Releasedc (PDC );

--------------------------------------------------------------------------------

Method 2:
This method is to draw a picture directly on the screen. Of course, because it is drawn at, the speed will be slower.
First, you must obtain the location of the area to be displayed:
Cwnd * pwnd = getdlgitem (idc_bmp );
CDC * PDC = pwnd-> getdc ();
Then
PDC-> setpixel (IW, IH, RGB (R, G, B ));
Release resources
Releasedc (PDC );

Bytes ------------------------------------------------------------------------------------

Method 3:
This method is to open up a space in the memory, and then use the setpixel method to write data to the memory, and finally display the data on the screen at a time. Of course, we know from the description that this method is faster than method 2. However, because setpixel is used, writing data at a point or point at a time is also slow.

First, you must obtain the location of the area to be displayed:
Cwnd * pwnd = getdlgitem (idc_bmp );
CDC * PDC = pwnd-> getdc ();
Then
CDC memdc;
Cbitmap m_bitmap, * m_poldbitmap;

Memdc. createcompatibledc (PDC );
M_bitmap.createcompatiblebitmap (PDC, lwidth, lheight );
M_poldbitmap = memdc. SelectObject (& m_bitmap );
Then, you can change the data in the memory.
Memdc. setpixel (IW, lheight-Ih, RGB (nrgb, nrgb, nrgb ));
Display the result
PDC-> stretchblt (, rect. right-rect.left, rect. bottom-rect.top, & memdc,
0, 0, lwidth, lheight, srccopy );
Finally release resources
Memdc. SelectObject (m_poldbitmap );
M_bitmap.deleteobject ();
Releasedc (PDC );

Bytes -----------------------------------------------------------------------------------

Method 4:
This method is quite good. Please take a look at it :)
This should be faster than method 2 and method 3, because it allocates a region directly in the memory and operates it directly using the method of operating the memory area, after the operation is complete, write it to the screen at one time.
First, obtain the area to be displayed.
Cwnd * pwnd = getdlgitem (idc_img );
CDC * thedc = pwnd-> getdc ();
Crect clientrect;
Pwnd-> getclientrect (clientrect );
Then, write the header file
Bitmapinfoheader bmiheader;
Bmiheader. bisize = sizeof (bitmapinfoheader );
Bmiheader. biwidth = m_width;
Bmiheader. biheight = m_height;
Bmiheader. biplanes = 1;
Bmiheader. bibitcount = 24;
Bmiheader. bicompression = bi_rgb;
Bmiheader. bisizeimage = 0;
Bmiheader. bixpelspermeter = 0;
Bmiheader. biypelspermeter = 0;
Bmiheader. biclrused = 0;
Bmiheader. biclrimportant = 0;
Now we can display the image data on the screen.
// Now blast it to the CDC passed in.
// Lines returns the number of lines actually displayed
Int lines = stretchdibits (thedc-> m_hdc,
Left, top,
Bmiheader. biwidth,
Bmiheader. biheight,
0, 0,
Bmiheader. biwidth,
Bmiheader. biheight,
TMP,
(Lpbitmapinfo) & bmiheader,
Dib_rgb_colors,
Srccopy );
Note that the TMP type is byte *, that is, it points to the first address of a memory area, as long as the data in this memory area is in the format of the data area in the BMP bitmap, you can. That is to say, each element in each row is an integer multiple of 32 bit (4 byte.

With this method, you can directly use the memory allocation function to allocate a memory area first, and then use memcpy to copy the content in one memory to another memory, then, it is displayed.

Don't forget to release the resource
Releasedc (thedc );

------------------------------------------------------------------------
In addition, the CDC for the entire dialog box is displayed. Sorry, I don't know what the CDC is.
Cpaintdc DC (this );
CDC * thedc = & DC;

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.