在C++中读取图片到屏幕上,首先需要加入这两个头文件
#include <ocidl.h>
#include <olectl.h>
关于render在MSDN中是这样写的
HRESULT Render(
HDC hdc, //Handle of device context on which to render the image
long x, //Horizontal position of image in hdc
long y, //Vertical position of image in hdc
long cx, //Horizontal dimension of destination rectangle
long cy, //Vertical dimension of destination rectangle
OLE_XPOS_HIMETRIC xSrc, //Horizontal offset in source picture
OLE_YPOS_HIMETRIC ySrc, //Vertical offset in source picture
OLE_XSIZE_HIMETRIC cxSrc, //Amount to copy horizontally in source picture
OLE_YSIZE_HIMETRIC cySrc, //Amount to copy vertically in source picture
LPCRECT prcWBounds //Pointer to position of destination for a metafile hdc
);
hdc 是设备环境句柄,
x、y是图片在设备中显示的坐标,
cx、cy是要显示图片的矩形的长度和宽度,注意,用IPicture::get_Width和IPicture::get_Height的得到的数据并不适用于此处,而是要除以2540
比如: pPic->get_Width(&hmWidth);
pPic->get_Height(&hmHeight);
//转换hmWidth和hmHeight为pixels距离,1英寸=25.4毫米
int nWidth = MulDiv(hmWidth,GetDeviceCaps(hDC_Temp,LOGPIXELSX),2540);
int nHeight = MulDiv(hmHeight,GetDeviceCaps(hDC_Temp,LOGPIXELSY),2540);
xSrc,ySrc分别是在源图像上的水平偏移和在源图像上的垂直偏移;
cxSrc是在源图像上水平拷贝的数量,cySrc是在源图像上垂直拷贝的数量;
prcWBounds是指向目标图元设备环境句柄的指针,一般为NULL。
为什么用render函数的时候都是Render(hdc, xPos, yPos, width, height, 0, srcHeight,srcWidth,-srcHeight);
原因是这样的:这个和位图的格式有关,位图文件中最开始的像素数据不是图像左上角这个像素,而是图像中最后一行最左边像素点的数据。所以左顶点是(0,hmHeight)
hmWidth表示宽度,-hmHeight是负数,表示从最后一行读取,并显示在第一行。