MFC dialog box pictures control (picture controls) static and dynamic display BMP picture

Source: Internet
Author: User
Tags bmp image

.
A. Static display BMP picture
1. Create an MFC dialog-based program that adds a button1 and picture control with the edit picture control ID in Resource view: Idc_static_pic.
2. In the resource view (if not, the view can be called out) in the right mouse dialog, select Add Resource. In the dialog box, select Bitmap, click "Import", select the local BMP picture (the proposed bitmap is placed in the Res folder) . It adds a    Zhang Idb_bitmap1 pictures .

3. Click the Button1 button to add the following code to the generated function:
voidCshowbmpdlg::onbnclickedbutton1 () {  //Load a picture from a resource  CBitmap bitmap;  //load refers to the location map resource BMP image ID  Bitmap. LoadBitmap (IDB_BITMAP1);  //Gets the handle of the dialog box picture control IDCStatic *p= (CStatic *) GetDlgItem (idc_static_pic);  //Set the static control window style to center the bitmap display  P->modifystyle (0xf, ss_bitmap| Ss_centerimage);  //Set the picture on the pictures control  P->setbitmap (bitmap); }

4. The result of the operation is as follows:

5. Summary
Through the above we can find: This method is through the resource ID to get the BMP picture, so I call it static method. At the same time, you may have seen a method that modifies its type to bitmap at the time the image control is added. But this is done by code, And the function of the ModifyStyle function is equivalent to setting its properties.
~ ~ It's a very obvious reaction. When the picture is displayed, if the picture is larger than the size of the control, only a portion of the picture is displayed. Method I only describe this, need you to try. When a BMP picture is displayed in a single document, it is overloaded with the OnPaint event, GetClientRect Get Dimension call StretchBlt (Image stretchable compression) implementation (refer to Previous Article).
in fact, the LOADBITMAP function has been replaced by LoadImage, which is used to load the icon \ cursor \ bitmap. The same effect can be achieved in the following code, where Rockies.bmp is the same as the local file, the same folder as the program.

voidCshowbmpdlg::onbnclickedbutton1 () {  //Get handle to the dialog slice control  cstatic* pWnd = (cstatic*) GetDlgItem (idc_static_pic);  //Set the static control window style to center the bitmap display  Pwnd->modifystyle (0xf, ss_bitmap| Ss_centerimage);  //Show Pictures  Pwnd->setbitmap ((HBITMAP):: LoadImage (NULL,    _t ("Rockies.bmp"),//resource number or local file name    Image_bitmap,//Mount bitmap image_cursor cursor image_icon icon        0,//Width 0 is the default size        0,//Height pixel unitslr_createdibsection | Lr_defaultsize | Lr_loadfromfile));}

two. Dynamically loading BMP images        
             
Dynamically add the same method, just add the code as follows (this is done with my previous code). The main step is:        
                 1. Define the Open File dialog box to get the BMP picture, which dlg. The Domal () ==idok indicates that the confirmation button is pressed to achieve a later operation. If you've learned C #, you'll find that its OpenFileDialog control is somewhat similar to this .       

                2. Then, Gets the path and suffix of the read file, ensures that it reads the picture information when it is BMP, and in the "        2.mfc-bmp picture Read save", I have described in detail how to read the BMP picture, This is also a binary read file, read its file header (Bitmapfileheader), Information header (Bitmapinfoheader), get its bitmap data .       
                3. The image is finally displayed in the picture control, and the GetClientRect function gets the rectangular area, displaying the image .     

voidCshowbmpdlg::onbnclickedbutton1 () {  //Open File Definition Four format file bmp gif jpg tiff  CString filter;  Filter="All Files (*.bmp,*.jpg,*.gif,*tiff) |*.bmp;*.jpg;*.gif;*.tiff| BMP (*.bmp) |*.bmp| JPG (*.jpg) |*.jpg| GIF (*.gif) |*.gif| TIFF (*.tiff) |*.tiff| | ";  CFileDialog dlg (true,null,null,ofn_hidereadonly,filter,null);  //Press the OK button to dlg. DoModal () Function Display dialog box  if(DLG. DoModal () = = IDOK)  {    //Open dialog box to get image information    CString bmpname = dlg. GetPathName ();//Get file path name such as D:\pic\abc.bmp    CString entname = dlg. Getfileext ();//Get file name extension    Entname.makelower ();//Convert the file name extension to a lowercase character    if(Entname.compare (_t ("BMP")) ==0)    {      //define variables to store picture information      Bitmapinfo *pbmpinfo;//Record image details      BYTE *pbmpdata;//Image Data      Bitmapfileheader Bmpheader;//File header      Bitmapinfoheader Bmpinfo;//Information header      CFile Bmpfile;//Record open file      //Open a file in a read-only way to read the BMP image of each part of BMP file header information data      if(!bmpfile.open (Bmpname, cfile::moderead| Cfile::typebinary))        return;      if(Bmpfile.read (&bmpheader,sizeof(Bitmapfileheader)) !=sizeof(Bitmapfileheader))        return;      if(Bmpfile.read (&bmpinfo,sizeof(Bitmapinfoheader)) !=sizeof(Bitmapinfoheader))        return;      Pbmpinfo = (Bitmapinfo *)New Char[sizeof(Bitmapinfoheader)];      //Request space for image data      memcpy (Pbmpinfo,&bmpinfo,sizeof(Bitmapinfoheader));      DWORD databytes = bmpheader.bfsize-bmpheader.bfoffbits;      Pbmpdata = (byte*)New Char[Databytes];      Bmpfile.read (pbmpdata,databytes);      Bmpfile.close ();      //Display image      CWnd *pwnd=getdlgitem (idc_static_pic);//Get Handle to pictrue control window      CRect rect;      Pwnd->getclientrect (&rect);//Get the rectangular area where the Pictrue control is located      CDC *pdc=pwnd->getdc ();//Get DC for Pictrue control      Pdc->setstretchbltmode (Coloroncolor);      StretchDIBits (PDC->GETSAFEHDC (),0,0, Rect. Width (), Rect. Height (),0,0,        Bmpinfo.biwidth,bmpinfo.biheight,pbmpdata,pbmpinfo,dib_rgb_colors,srccopy);    }  }}

The running program displays the results as shown, click the button to open the picture and display the picture.

Summary: Through this program you can obviously see the dynamic loading of a picture because it is displayed by the picture control rectangle, the image is stretched. And the code is a good response to how to obtain BMP image method. It is described by both static and dynamic methods.

MFC dialog box pictures control (picture controls) static and dynamic display BMP picture

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.