VC + + Image control (picture controls) method for displaying resource bitmap (BMP), file Bitmap (BMP), other format file picture (jpg\png\bmp)

Source: Internet
Author: User
Tags bmp image

In VC + + MFC programming, we often use the picture control to display images. Below is a brief summary of several different ways of showing:

(the instance can be downloaded in my CSDN resource: http://download.csdn.net/detail/margin1988/8341525)

The first, the resource bitmap way to display BMP picture

If you want to display a BMP bitmap, you can use the resource bitmap method, the following steps:

(1) Copy the BMP file to the Res directory of the project;

(2) Add a picture control control in the dialog box , for example: ID idc_static1,type is set to bitmap;

(3) In the Project resource view, right-click "Add Resource", now the resource type Bitmap, and then "import" (1) in the file, for example: After the import of the resource ID is idb_bitmap1;


(4) Add the control type variable for the Idc_static1 picture control control, for example: CStatic m_resbmp;

(5) The BMP bitmap Idb_bitmap1 from the resource is displayed on this control , with the following code:

Hbitmap hbitmap;//display images in a resource (bitmap bmp only) Hbitmap = (hbitmap) LoadImage (AfxGetInstanceHandle (), Makeintresource (IDB_  BITMAP1),  image_bitmap,0,0,  lr_loadmap3dcolors);  M_resbmp. SetBitmap (HBITMAP);

Second, file bitmap mode display BMP picture

If you want to display a BMP bitmap, you can also take a file bitmap way, the following steps:

(1) Copy the BMP file to the project executable directory (for example: Debug directory, beixin.bmp file);

(2) in the program to get the full path of the executable directory, the code is as follows:

CString exepath;//exe Path char Filepath[256];char *ppath; GetModuleFileName (AfxGetInstanceHandle (), filepath,256);pP ath = STRRCHR (filepath, ' \ \ '); *ppath = 0;exepath = filepath;
(3)in the dialog box, add athe picture control control, for example: ID idc_static2,type is set to bitmap;

(4) Add the control type variable for the Idc_static2 picture control control, for example: CStatic m_filebmp;

(5) The BMP bitmap beixin.bmpfrom the file is displayed on this control, with the following code:

Displays the image in the file (bitmap bmp only) Hbitmap hbitmap; CString Filebmppath;filebmppath. Format ("%s\\beixin.bmp", exepath);//bitmap file full path Hbitmap = (hbitmap) LoadImage (AfxGetInstanceHandle (), _t (Filebmppath), Image_bitmap,0,0,lr_loadfromfile); m_filebmp. SetBitmap (HBITMAP);

The third kind, the file picture way displays jpg\png\bmp and so on picture

If you want to display a JPG picture, a PNG image, or a BMP image (also) a static picture, such as other formats, you can use the way the file picture, the steps are as follows:

(1) Copy the picture file to the Project executable directory (for example: Debug directory,shoutao.jpg\shoutao.png\shoutao.bmp file );

(2) get the full path of the executable directory in the program, IBID. (second (2));

(3) Add a picture control control in the dialog box , for example: ID idc_static3,type is set to frame;

(4) Add the control type variable for the IDC_STATIC3 picture control control, for example: CStatic m_fileimg;

(5) The jpg\png\bmp picture shoutao.jpg\shoutao.png\shoutao.bmpfrom the file is displayed on this control, and the code is as follows:

The. h file cfilefind finder;void showimg (char* imgpath);//functions that display different format pictures//.cpp file void cpoint34dlg::showimg (char* imgpath) { Displays the image in the file (not limited to the image format) if (finder. FindFile (Imgpath)) {CRect rect;m_fileimg. GetClientRect (&rect); CImage img;img. Load (Imgpath); cdc* PDC = m_fileimg. GETWINDOWDC (); img. Draw (PDC->GETSAFEHDC (), rect); ReleaseDC (PDC);}}

. h file in CString fileimgpath;//.cpp file, call picture to display function Srand ((unsigned) time (NULL)); int rd = rand ()%3;switch (RD) {case 0: Fileimgpath. Format ("%s\\shoutao.bmp", ExePath); Break;case 1:fileimgpath. Format ("%s\\shoutao.png", ExePath); Break;case 2:fileimgpath. Format ("%s\\shoutao.jpg", ExePath); Showimg (Fileimgpath. GetBuffer ());//Call

(6) This is In the picture control control , do not forget to add a window update to the OnPaint () in the dialog box, redraw the image, otherwise, when the dialog box is minimized or obscured, the image is not displayed properly, OnPaint () adds the following code:

Cdialog::onpaint ();//When the dialog is minimized or obscured, the image is not displayed properly, the window needs to be updated, redrawn UpdateWindow (), Showimg (Fileimgpath. GetBuffer ());


As follows:



VC + + Image control (picture controls) method for displaying resource bitmap (BMP), file Bitmap (BMP), other format file picture (jpg\png\bmp)

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.