void DrawImage(HDC hdc) { IImagingFactory *pImgFactory = NULL; IImage *pImage = NULL; RECT rc = { 0, 0, 110, 88};
// Normally you would only call CoInitialize/CoUninitialize // once per thread. This sample calls CoInitialize in this // draw function simply to illustrate that you must call // CoInitialize before calling CoCreateInstance. CoInitializeEx(NULL, COINIT_MULTITHREADED);
// Create the imaging factory. if (SUCCEEDED(CoCreateInstance (CLSID_ImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IImagingFactory, (void **)&pImgFactory))) { // Load the image from the JPG file. if (SUCCEEDED(pImgFactory->CreateImageFromFile( TEXT("//Program Files//Imaging//flower.jpg"), &pImage))) { // Draw the image. pImage->Draw(hdc, &rc, NULL); pImage->Release(); }
pImgFactory->Release(); } CoUninitialize(); } |
IImage::Draw可以绘制透明的PNG图,其原型为:
HRESULT Draw( HDC hdc, const RECT* dstRect, OPTIONAL const RECT* srcRect ); |
dstRect为显示的区域,如果大小和图片的大小不一样的话,图片会有缩放的效果;
srcRect为源图片的显示区域,比如图片大小320*100,我只需要显示其中一部分时可以指定该参数,否则设为NULL。
注:该参数是以0.01mm为单位的,需要把像素进行转换,可以通过GetImageInfo获取图片dpi参数后进行转换((2.54*1000)/MyImageinfo.Xdpi)*像素。
HRESULT GetImageInfo( ImageInfo* imageInfo ); |
原图为透明png格式:
调用Draw后效果: