static BOOL drawImage(HDC hDC, LPCTSTR lpszFileName) { IImagingFactory* pImageFactory = NULL; BOOL bRet = FALSE; if (SUCCEEDED(CoCreateInstance(CLSID_ImagingFactory, 0, CLSCTX_INPROC_SERVER, IID_IImagingFactory, (void**)&pImageFactory))) { IImage* pImage = NULL; ImageInfo imageInfo; if (SUCCEEDED(pImageFactory->CreateImageFromFile(lpszFileName, &pImage)) && SUCCEEDED(pImage->GetImageInfo(&imageInfo))) { RECT rect = {0, 0, imageInfo.Width, imageInfo.Height}; pImage->Draw(hDC, &rect, 0); pImage->Release(); bRet = TRUE; } pImageFactory->Release(); } return bRet; }
HBITMAP SHLoadImageFile( LPCTSTR pszFileName ) { if ( !pszFileName || !*pszFileName ) return 0; String strFileName = convertToStringA(pszFileName); /*if ( String_endsWith(strFileName, ".bmp") ) { return (HBITMAP)::LoadImage(NULL, pszFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); }*/ if ( !String_endsWith(strFileName, ".png") && !String_endsWith(strFileName, ".bmp") ) return 0; IImagingFactory *pImgFactory = NULL; IImage *pImage = NULL; //CoInitializeEx(NULL, COINIT_MULTITHREADED); HBITMAP hResult = 0; if (SUCCEEDED(CoCreateInstance (CLSID_ImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IImagingFactory, (void **)&pImgFactory))) { ImageInfo imageInfo; if (SUCCEEDED(pImgFactory->CreateImageFromFile(CA2W(strFileName.c_str()), &pImage)) && SUCCEEDED(pImage->GetImageInfo(&imageInfo))) { CWindowDC dc(getMainWnd()); CDC dcBitmap; dcBitmap.CreateCompatibleDC(dc.m_hDC); hResult = CreateCompatibleBitmap(dc.m_hDC, imageInfo.Width, imageInfo.Height); if (hResult) { HBITMAP hOldBitmap = dcBitmap.SelectBitmap(hResult); //dcBitmap.FillSolidRect( 0,0, imageInfo.Width, imageInfo.Height, RGB(255,255,255)); CRect rc(0, 0, imageInfo.Width, imageInfo.Height); COLORREF clrOld = ::SetBkColor(dcBitmap.m_hDC, RGB(255,255,255)); ::ExtTextOut(dcBitmap.m_hDC, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL); ::SetBkColor(dcBitmap.m_hDC, clrOld); pImage->Draw(dcBitmap.m_hDC, rc, NULL); dcBitmap.SelectBitmap(hOldBitmap); } pImage->Release(); } pImgFactory->Release(); } //CoUninitialize(); return hResult; }
HBITMAP LoadImageThumbnailWithImagingApi(const CString &strFileName, int imgWid, int imgHei) { IImagingFactory *pImgFactory = NULL; IImage *pImage = NULL; IImage *pImageThumb = NULL; CoInitializeEx(NULL, COINIT_MULTITHREADED); HBITMAP hResult = 0; if (SUCCEEDED(CoCreateInstance (CLSID_ImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IImagingFactory, (void **)&pImgFactory))) { ImageInfo imageInfo; if (SUCCEEDED(pImgFactory->CreateImageFromFile(strFileName, &pImage)) && SUCCEEDED(pImage->GetThumbnail(imgWid, imgHei, &pImageThumb)) && SUCCEEDED(pImageThumb->GetImageInfo(&imageInfo))) { CWindowDC dc(0); CDC dcBitmap; dcBitmap.CreateCompatibleDC(&dc); hResult = CreateCompatibleBitmap(dc.GetSafeHdc(), imageInfo.Width, imageInfo.Height); if (hResult) { HGDIOBJ hOldBitmap = dcBitmap.SelectObject(hResult); pImage->Draw(dcBitmap.GetSafeHdc(), CRect(0, 0, imageInfo.Width, imageInfo.Height), NULL); dcBitmap.SelectObject(hOldBitmap); } pImageThumb->Release(); pImage->Release(); } pImgFactory->Release(); } CoUninitialize(); return hResult; }
WMAlphaBitmap::WMAlphaBitmap(IImage* img) { #if defined(_WIN32_WCE) IImagingFactory *pImgFactory = NULL; mWidth = 0; mHeight = 0; mImgBuf = NULL; HRESULT co_init_result = CoInitializeEx(NULL, 0/*COINIT_APARTMENTTHREADED*/); if ( (co_init_result == S_OK) || (co_init_result == S_FALSE) ) { msg_out("CoInitializeEx OK"); if (SUCCEEDED(CoCreateInstance (CLSID_ImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IImagingFactory, (void **)&pImgFactory))) { ImageInfo imgInfo; img->GetImageInfo(&imgInfo); mWidth = imgInfo.Width; mHeight = imgInfo.Height; IBitmapImage* pBitmap = 0; if ( SUCCEEDED(pImgFactory->CreateBitmapFromImage( img, mWidth, mHeight, PixelFormat32bppARGB, InterpolationHintDefault, &pBitmap) )) { msg_out("Create Bitmap OK"); RECT rc = { 0, 0, mWidth, mHeight}; BitmapData bitmap_data; if ( SUCCEEDED(pBitmap->LockBits( &rc, ImageLockModeRead, PixelFormatDontCare, &bitmap_data))) { // msg_out("Lock Bits OK"); void* src_buf = bitmap_data.Scan0; int stride = bitmap_data.Stride; int w = bitmap_data.Width; int h = bitmap_data.Height; mImgBuf = new unsigned int[w*h]; if (mImgBuf != 0) { msg_out("Img buffer allocated OK"); // start convert { unsigned int* dst = mImgBuf; unsigned int* src; int x; int y; for (y = 0 ; y < h; y++) { if (stride < 0) { src = (unsigned int*)(((unsigned char*)src_buf) + (h-1-y)*(-stride)); } else { src = (unsigned int*)(((unsigned char*)src_buf) + y*(stride)); } for (x = w; x > 0; x--) { *dst++ = *src++; } } } msg_out("Convert to img buffer finished OK"); // finish convert } else { err_out("Image Buffer not allocated !"); } pBitmap->UnlockBits(&bitmap_data); } else { err_out("Bitmap bits not locked !"); } pBitmap->Release(); } pImgFactory->Release(); } CoUninitialize(); } else { err_out("CoInitializeEx not initialized !"); } #endif //#if defined(_WIN32_WCE) }
void DrawingImageImpl::init(const char* path, void const *p, int size, WMBitmap* bitmap, bool useAlpha) { mID = ++ourDrawingImageID; RHO_MAP_TRACE1("DrawingImage create with ID = %d", mID); #if defined(_WIN32_WCE) IImagingFactory *pImgFactory = NULL; IImage *pImage = NULL; mWidth = 0; mHeight = 0; mBitmap = NULL; if (bitmap != NULL) { mBitmap = bitmap; mBitmap->addRef(); mWidth = bitmap->width(); mHeight = bitmap->height(); return; } HRESULT co_init_result = CoInitializeEx(NULL, 0/*COINIT_APARTMENTTHREADED*/); if ( (co_init_result == S_OK) || (co_init_result == S_FALSE) ) { msg_out("CoInitializeEx OK"); if (SUCCEEDED(CoCreateInstance (CLSID_ImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IImagingFactory, (void **)&pImgFactory))) { HRESULT res = 0; if (p != NULL) { // from buf res = pImgFactory->CreateImageFromBuffer( p, size, BufferDisposalFlagNone, &pImage); } else { // from file msg_out("Create Image Factory OK"); wchar_t wc_filename[2048]; mbstowcs(wc_filename, path, 2048); res = pImgFactory->CreateImageFromFile( wc_filename, &pImage); } if (SUCCEEDED(res)) { IImage* mimage = pImage; ImageInfo imgInfo; mimage->GetImageInfo(&imgInfo); mWidth = imgInfo.Width; mHeight = imgInfo.Height; RHO_MAP_TRACE2("Drawing Image was created with WIDTH = %d, HEIGHT = %d", mWidth, mHeight); mBitmap = new WMBitmap(mimage, useAlpha); mimage->Release(); } else { err_out("Image not created !"); } pImgFactory->Release(); } else { err_out("ImageFactory not created !"); } CoUninitialize(); } else { err_out("CoInitializeEx not initialized !"); } #endif //#if defined(_WIN32_WCE) }