Use VC to open BMP jpg or GIF images

Source: Internet
Author: User

I have written an article about opening an image file in VC. It is not very easy to understand. It seems a bit messy in some places. It is easy to understand this version, let's get started. Download source file: showbitmap.rar

1. Create an application with the project name showbitmap

2. Add the following member variables and member functions to the cshowbitmapview class: the header file "atlimage" is required for cimage m_myimage. H "support, or you can use the Wizard to add this variable, then the header file will be automatically added. Here we manually go to cshowbitmapview. add # include "atlimage. H ", remember to add this.

 

// Parameter Definition
Public:
Cstring strpicpath; // name of the opened Image File
Cstring extname; // the extension of the opened Image File
Cbitmap m_bitmap; // creates a bitmap object.
Cimage m_myimage; // used to read and display jpg and other format image files
Void showbitmap (CDC * PDC, cstring strpicpath); // function used to display the specified bitmap
Void showpic (CDC * PDC, cstring picname );


3. Add a response function for the id_file_open message (this function is not described in detail). Write the function (the Code is as follows)

Void cshowbitmapview: onfileopen ()
{
// Cfiledialog DLG (true, _ T ("BMP"), _ T ("*. BMP "), ofn_hidereadonly | ofn_overwriteprompt, _ T (" bitmap file (*. BMP ;*. JPG) | *. BMP ;*. JPG | "));
Cfiledialog DLG (true, null, null, ofn_hidereadonly | ofn_overwriteprompt, _ T ("image file (*. JPG ;*. JPEG ;*. GIF ;*. BMP) | *. JPG ;*. JPEG ;*. GIF ;*. BMP | bitmap file (*. BMP) | *. BMP | "));
If (idok = DLG. domodal ())
{
Strpicpath. Format (_ T ("% s"), DLG. getpathname ());
Extname = DLG. getfileext (); // return the extended File Name of the selected file
Extname. makelower (); // change the suffix to lowercase.
Invalidate ();
}
}

4. Compile the BMP image display function

Void cshowbitmapview: showbitmap (CDC * PDC, cstring strpicpath)
{
Hbitmap = (hbitmap) LoadImage (null, strpicpath, image_bitmap, 0, 0, lr_createdibsection | lr_defaultsize | lr_loadfromfile );
M_bitmap.detach ();
M_bitmap.attach (hbitmap );

Crect rect;
Getclientrect (& rect );

CDC dcimage;
If (! Dcimage. createcompatibledc (PDC ))
{
Return;
}
Bitmap bm;
M_bitmap.getbitmap (& BM );
Dcimage. SelectObject (& m_bitmap );
// PDC-> bitblt (, BM. bmwidth, BM. bmheight, & dcimage, srccopy); // retain the original size of the image
PDC-> stretchblt (0, 0, rect. right, rect. bottom, & dcimage, 0, 0, BM. bmwidth, BM. bmheight, srccopy); // stretch the image to fill the entire customer Zone
}

5. Compile jpg and other image display functions (the code for viewing images in full screen is not listed here. If you need this function, please refer to the relevant information of the draw function)

Void cshowbitmapview: showpic (CDC * PDC, cstring strpicpath)
{
If (! M_myimage.isnull ())
M_myimage.destroy ();
Hresult = m_myimage.load (strpicpath );
Int iwidth = m_myimage.getwidth ();
Int iheight = m_myimage.getheight ();
M_myimage.draw (PDC-> m_hdc, 0, 0, iwidth, iheight );
}

6. Modify the ondraw function to display images.

Void cshowbitmapview: ondraw (CDC * PDC)
{
Cshowbitmapdoc * pdoc = getdocument ();
Assert_valid (pdoc );
If (! Pdoc)
Return;

If (0 = extname. Compare (_ T ("BMP ")))
{
Showbitmap (PDC, strpicpath );
}
Else
{
If (0 = extname. compare (_ T ("jpg") | 0 = extname. compare (_ T ("Jpeg") | 0 = extname. compare (_ T ("GIF ")))
{
Showpic (PDC, strpicpath );
}
}

}

7. Conclusion:

If you are interested, you can combine the above two image display functions and the IF statement in ondraw into a function. I hope this article will help you. please correct me if you have any questions.

8. Supplement:

A small problem in the program is that, when the image is stretched and displayed, it may be distorted. Generally, the image in BMP format is reduced and the JPG file is stretched and distorted, in this case, you need to call the setstretchbltmode function before using the stretchblt function or the draw function. The modification method for the above program is as follows:

1) In the showbitmap (CDC * PDC, cstring strpicpath) Function

PDC-> stretchblt (, rect. Right, rect. Bottom, & dcimage, BM. bmwidth, BM. bmheight, srccopy );

PDC-> setstretchbltmode (stretch_halftone );

2) In the showpic (CDC * PDC, cstring strpicpath) Function

M_myimage.draw (PDC-> m_hdc, rect. Right, rect. Bottom, iwidth, iheight );

: Setstretchbltmode (PDC-> m_hdc, halftone); or

PDC-> setstretchbltmode (stretch_halftone );

The draw here is a little different from the above. This enables the image to fill the entire view, that is, full screen display.

Http://hi.baidu.com/krispan/blog/item/39caa5291f9ee7f898250a23.html

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.