Implementation of image map in PNG format in MFC (2011-07-14-19:10:29) ___ reprinted from Sina
Beginners VC, is doing Gobang, Gobang in the picture format are BMP format, so the map with CBitmap can be very simple implementation. At first, did not care so much, today just put Gobang finished on the code and release version of the EXE sent to my instructor to see, found my file is quite large, so the instructor to me pointed out the BMP format of the picture compared to memory, let me look for a PNG format map.
So I searched the internet a lot of data, see the useful API oleloadpicture to load JPG, GIF format picture , but this method does not support PNG format, and the GIF format can only load the first frame, so decided to abandon this method. Also see can use library Cximage, but think this method is more troublesome, so also decided to give up. After looking at a lot of ways, it's easy to see someone say the CImage class, so try it out.
After reading the online information and methods, finally I successfully loaded a PNG format background image, below I summarize the steps of the map:
(1) First include # include <atlimage.h> in header file stdafx.h
(2) Add the code in the OnDraw function of the drawing view file:
CImage img;
HRESULT ret = img. Load (_t ("res/wuziqi.png")); Wuziqi.png is the name of the picture I want to load, including the path
HBITMAP hbitmap = img. Detach ();
working with images like BMP images, here's how to display pictures
CBitmap CBitmap;
BITMAP BITMAP;
CDC MEMDC;
Cbitmap.attach (HBITMAP);
MEMDC. CreateCompatibleDC (PDC);
MEMDC. SelectObject (HBITMAP);
Cbitmap.getbitmap (&BITMAP);
Pdc->stretchblt (0,0,800,590,&memdc,0,0,800,590,srccopy);
Let's take a look at the StretchBlt function:
function function: The function copies a bitmap from the source rectangle to the target rectangle and, if necessary, stretches or compresses the image according to the mode currently set by the target device.
Function prototypes: BOOL STRETCHBLT (hdc hdcdest, int nxorigindest, int nyorigindest, int nwidthdest, int nheightdest, hdc hdcsrc, int NXO RIGINSRC, int nyoriginsrc, int nwidthsrc, int nheightsrc, DWORD dwrop);
Parameters:
Hdcdest: A handle to the target device environment.
Nxorigindest: Specifies the x-coordinate of the upper-left corner of the destination rectangle, which represents the coordinates in logical units.
Nyorigindest: Specifies the y-coordinate of the upper-left corner of the destination rectangle, which represents the coordinates in logical units.
Nwidthdest: Specifies the width of the target rectangle, in logical units.
Nheightdest: Specifies the height of the target rectangle, in logical units.
HDCSRC: A handle to the source device environment.
NXORIGINSRC: The x-coordinate that points to the upper-left corner of the source rectangle, representing the coordinates in logical units.
NYORIGINSRC: The y-coordinate that points to the upper-left corner of the source rectangle, representing the coordinates in logical units.
NWIDTHSRC: Specifies the width of the source rectangle, in logical units.
NHEIGHTSRC: Specifies the height of the source rectangle, expressed in logical units.
Dwrop: Specifies the raster operation to be performed. Raster opcode defines how the system combines colors in an output operation that includes objects such as brushes, source bitmaps, and target bitmaps.
Because my target window is 800 wide and the height is 590, and my picture size is 800*590, I want the whole picture to fill the window, so the parameter is Pdc->stretchblt (0,0,800,590,&MEMDC, 0,0,800,590,srccopy);
I temporarily only loaded the PNG format of the picture, as to the other format of the loading of the picture can also be fully reference to this step to complete is still to be verified, I see online said using this method GIF can not be displayed dynamically, I hope you see after the solution to find a lot of guidance to me. Here I thank you first.
Implementation of PNG format picture map in MFC