Win32 GDI + application (iv)---bitmap opening and display

Source: Internet
Author: User

To display bitmaps, you should use GDI + inside the bitmap class or the image class, which all two classes provide a way to open files from a file on the hard disk, creating the corresponding in-memory bitmap object work. You can then use the DrawImage method of the graphics class to draw the bitmap.
The following code initializes GDI +, displays an open file dialog box and creates a bitmap object that displays the bitmap:

Gdiplusstartupinput input; Ulong_ptr Gdiplustoken;if (Gdiplusstartup (&gdiplustoken,&input,null)!=ok) {return-1;} Char filename[200];openfilename openstruct;memset (&openstruct,0,sizeof (openfilename)); openStruct.lStructSize =sizeof (openfilename); openstruct.flags=ofn_explorer;openstruct.lpstrfilter= "Jpeg files (*.jpg) \0*.jpg\0\0"; o Penstruct.lpstrfile=filename;openstruct.nmaxfile=200;if (GetOpenFileName (&openstruct)) {imageFileName= FileName;} HDC Hdc=getdc (Hpicwnd), if (hdc==null) {MessageBox (NULL, "", "", MB_OK);} Graphics *g=new Graphics (HDC); WCHAR tmpfilename[100];size_t numofchars;if (/*mbstowcs_s (&numofchars,tmpfilename,100,filename,199) */ MultiByteToWideChar (Cp_acp,mb_composite,imagefilename.c_str (), 200,tmpfilename,100)/*==-1*/) {MessageBox (NULL, "       Error occured in string convertion! "," error! ", MB_OK); return WParam;} HWND Hpicwnd;rect RC; GetWindowRect (HPICWND,&RC); RECTF DrawRect (rc.left,rc.top,rc.right-rc.left,rc.bottom-rc.top); Bitmap *bmp=new Bitmap (tmpfilename); bmpmain=bmp; G->drawimage (Bmp,drawrect);d elete G; ReleaseDC (HWND,HDC); Gdiplusshutdown (Gdiplustoken);

Reprint Address: http://www.cppblog.com/dingding/archive/2008/06/27/54796.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.