HBITMAP UIIMEdit::_LoadAnImage(IN CString filePath) { HANDLE hFile = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); //从指定的路径szImagePath中读取文件句柄 DWORD dwFileSize = GetFileSize(hFile, NULL); //获得图片文件的大小,用来分配全局内存 HGLOBAL hImageMemory = GlobalAlloc(GMEM_MOVEABLE, dwFileSize); //给图片分配全局内存 void *pImageMemory = GlobalLock(hImageMemory); //锁定内存 DWORD dwReadedSize; //保存实际读取的文件大小 ReadFile(hFile, pImageMemory, dwFileSize, &dwReadedSize, NULL); //读取图片到全局内存当中 GlobalUnlock(hImageMemory); //解锁内存 CloseHandle(hFile); //关闭文件句柄 HRESULT hr = NULL; IStream *pIStream = NULL;//创建一个IStream接口指针,用来保存图片流 IPicture *pIPicture = NULL;//创建一个IPicture接口指针,表示图片对象 hr = CreateStreamOnHGlobal(hImageMemory, false, &pIStream); //用全局内存初使化IStream接口指针 ASSERT(SUCCEEDED(hr)); hr = OleLoadPicture(pIStream, 0, false, IID_IPicture, (LPVOID*)&(pIPicture));//用OleLoadPicture获得IPicture接口指针 ASSERT(SUCCEEDED(hr)); HBITMAP hB = NULL; pIPicture->get_Handle((unsigned int*)&hB); // Copy the image. Necessary, because upon p's release, // the handle is destroyed. HBITMAP hBB = (HBITMAP)CopyImage(hB, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG); GlobalFree(hImageMemory); //释放全局内存 pIStream->Release(); //释放pIStream pIPicture->Release(); //释放pIPictur return hBB; }
HBITMAP Timeout::LoadImageBitmap(HGLOBAL hgbl, DWORD size) { HBITMAP hbmp = NULL; CoInitialize(NULL); IStream* stream; HRESULT hr = CreateStreamOnHGlobal(hgbl, FALSE, &stream); if(SUCCEEDED(hr) && stream) { ULARGE_INTEGER ul; ul.LowPart = size; ul.HighPart = 0; stream->SetSize(ul); IPicture* picture; // Load picture from stream hr = OleLoadPicture(stream, 0, 0, IID_IPicture, (void**)&picture); if(SUCCEEDED(hr) && picture) { // Copy picture to a bitmap resource HBITMAP hsrc; picture->get_Handle((OLE_HANDLE *)&hsrc); hbmp = (HBITMAP)CopyImage(hsrc, IMAGE_BITMAP, 0, 0, 0); picture->Release(); } stream->Release(); } CoUninitialize(); return hbmp; }
/** * Load a GIF image. * This will replace the currently-loaded image. * Animated GIFs are not yet supported -- only the first frame of an * animated GIF will be loaded. * @param pGifStream The GIF data buffer (may not be NULL). * @param pStreamLen The length of the buffer. * @return @c true if the load succeeded, @c false otherwise (current * image will still be cleared). */ bool GifDecoder::Decode(const unsigned char *pGifStream, int pStreamLen) { // Delete current image. Clean(); bool retv = false; HGLOBAL buf = GlobalAlloc(GPTR, pStreamLen); memcpy((void*)buf, pGifStream, pStreamLen); IStream *stream = NULL; IPicture *pic = NULL; // We currently don't support animated GIFs, so set big delay for // the first frame. mDelay[0] = 3600000; // Use OleLoadPicture() to convert the GIF stream to an HBITMAP. if (SUCCEEDED(CreateStreamOnHGlobal(buf, false, &stream))) { if (SUCCEEDED(OleLoadPicture(stream, 0, false, IID_IPicture, (void**)&pic))) { HBITMAP hb = NULL; pic->get_Handle((OLE_HANDLE*)&hb); mBitmap[0] = (HBITMAP)CopyImage(hb, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG); retv = true; } } if (pic != NULL) pic->Release(); if (stream != NULL) stream->Release(); GlobalFree(buf); return retv; }
// Function LoadAnImage: accepts a file name and returns a HBITMAP. // On error, it returns 0. HBITMAP LoadAnImage(const char* FileName) { // Use IPicture stuff to use JPG / GIF files IPicture* p; IStream* s; HGLOBAL hG; void* pp; FILE* fp; // Read file into memory fp = fopen(FileName, "rb"); if (!fp) { return NULL; } fseek(fp,0,SEEK_END); int fs = ftell(fp); fseek(fp,0,SEEK_SET); hG = GlobalAlloc(GPTR,fs); if(!hG) { fclose(fp); return NULL; } pp = (void*)hG; fread(pp,1,fs,fp); fclose(fp); // Create an IStream so IPicture can CreateStreamOnHGlobal(hG, false, &s); if(!s) { GlobalFree(hG); return NULL; } OleLoadPicture(s,0,false,IID_IPicture,(void**)&p); if(!p) { s->Release(); GlobalFree(hG); return NULL; } s->Release(); GlobalFree(hG); HBITMAP hB = 0; p->get_Handle((unsigned int*)&hB); // Copy the image. Necessary, because upon p's release, // the handle is destroyed. HBITMAP hBB = (HBITMAP)CopyImage(hB, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG); p->Release(); return hBB; }
// ==> load image with OleLoadPicture HBITMAP loadAnImage(TCHAR* szFileName) { if (!szFileName) { return NULL; } // Read file in memory FILE* file = _tfopen(szFileName, _T("rb")); if (!file) return NULL; long size = getFileSize(file); HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, size); if (!hGlobal) { fclose(file); return NULL; } void* pData = GlobalLock(hGlobal); fread(pData, 1, size, file); fclose(file); GlobalUnlock(hGlobal); // Use IPicture stuff to load JPG / GIF files IPicture* pPicture = NULL; IStream* pStream = NULL; if (CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) != S_OK) { GlobalFree(hGlobal); return NULL; } if (OleLoadPicture(pStream, size, FALSE, IID_IPicture, (LPVOID *)&pPicture) != S_OK) { pPicture = NULL; } pStream->Release(); GlobalFree(hGlobal); if (!pPicture) return NULL; HBITMAP hB = NULL; pPicture->get_Handle((unsigned int*)&hB); // Copy the image // Necessary, because upon pPicture's release, the handle is destroyed HBITMAP hBB = (HBITMAP)CopyImage(hB, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG); pPicture->Release(); return hBB; }
// // Entry point for conversion // UT_Error IE_ImpGraphic_Win32Native::_convertGraphic(UT_ByteBuf * pBB, std::string& mimetype) { IPicture* pPicture = NULL; IStream* stream; HGLOBAL hG; HBITMAP hBitmap; OLE_HANDLE* hB; PBITMAPINFO bi; UT_ByteBuf bBufBMP; UT_Error err; /* If the system has GDI+, use it*/ if (isGDIPlusAvailable()) { m_pBB = new UT_ByteBuf(); return GDIconvertGraphic(pBB, m_pBB, mimetype); } // the code below always writes out PNG's for now; we could update it to support // native JPEG images as well, or just delete it and always use GDI+. mimetype = "image/png"; // We need to store the incoming bytebuffer in a Windows global heap size_t nBlockLen = pBB->getLength(); hG = GlobalAlloc(GPTR, nBlockLen); if (!hG) return UT_IE_NOMEMORY; CopyMemory(hG, pBB->getPointer(0), nBlockLen); // Create a stream from heap HRESULT hr = CreateStreamOnHGlobal(hG,false,&stream); if (!SUCCEEDED(hr) || !stream) { GlobalFree(hG); return UT_IE_NOMEMORY; } hr = OleLoadPicture(stream,0,false,IID_IPicture,(void**)&pPicture); stream->Release(); GlobalFree(hG); if (!SUCCEEDED(hr) || !pPicture) { return UT_IE_UNKNOWNTYPE; } pPicture->get_Handle((unsigned int*)&hB); hBitmap = (HBITMAP)CopyImage(hB,IMAGE_BITMAP,0,0,LR_COPYRETURNORG); HWND hWnd = GetDesktopWindow(); // Create a BMP file from a BITMAP bi = CreateBitmapInfoStruct(hBitmap); CreateBMPFile(hWnd, bBufBMP, bi, hBitmap, GetDC(hWnd)); LocalFree ((HLOCAL)bi); InitializePrivateClassData(); /* Read Header Data */ err = Read_BMP_Header(&bBufBMP); /* It's not a bitmap, then we have to rendered it into a device context and get a bitmap from there. Case wmf graphics */ if (err) { if (err!=UT_IE_BOGUSDOCUMENT) { pPicture->Release(); return err; } long nWidth = 0; long nHeight = 0; long nScaleToWidth= 500; long nScaleToHeight= 500; RECT rc, rect; BYTE *imagedata; HBITMAP hBit; HBITMAP hOld; BITMAPINFO bmi; HDC hWndDC = GetDC(hWnd); HDC hMemDC = CreateCompatibleDC(hWndDC); HBRUSH hBrush = (HBRUSH)GetCurrentObject(hMemDC, OBJ_BRUSH); pPicture->get_Width (&nWidth); pPicture->get_Height(&nHeight); bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi.bmiHeader.biWidth = nScaleToWidth; bmi.bmiHeader.biHeight = nScaleToHeight; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 24; // as we want true-color bmi.bmiHeader.biCompression = BI_RGB; // no compression bmi.bmiHeader.biSizeImage = (((bmi.bmiHeader.biWidth * bmi.bmiHeader.biBitCount + 31) & ~31) >> 3) * bmi.bmiHeader.biHeight; bmi.bmiHeader.biXPelsPerMeter = 0; bmi.bmiHeader.biYPelsPerMeter = 0; bmi.bmiHeader.biClrImportant = 0; bmi.bmiHeader.biClrUsed = 0; // we are not using palette hBit = CreateDIBSection(hMemDC,&bmi,DIB_RGB_COLORS,(void**)&imagedata,0,0); hOld = (HBITMAP) SelectObject(hMemDC, hBit); rect.left = 0; rect.top = nScaleToHeight; rect.right = nScaleToWidth; rect.bottom = 0; FillRect(hMemDC, &rect, hBrush); pPicture->Render(hMemDC, 0,0, nScaleToWidth, nScaleToHeight, 0, nHeight, nWidth, -nHeight, &rc); hBit = (HBITMAP)SelectObject(hMemDC, hOld); bi = CreateBitmapInfoStruct(hBit); CreateBMPFile(hWnd, bBufBMP, &bmi, hBit, hMemDC); LocalFree ((HLOCAL)bi); DeleteDC(hMemDC); DeleteDC(hWndDC); DeleteObject(hBrush); DeleteObject(hBit); err = Read_BMP_Header(&bBufBMP); if (err) { pPicture->Release(); return err; } } pPicture->Release(); if ((err = Initialize_PNG())) { return err; } /* Read Palette, if no palette set Header accordingly */ if(m_iBitsPerPlane < 24) { if ((err = Convert_BMP_Palette(&bBufBMP))) return err; } else { UT_uint16 bitsPerChannel; UT_uint16 colorType; if (m_iBitsPerPlane == 24) { bitsPerChannel = 8; colorType = PNG_COLOR_TYPE_RGB; } else if (m_iBitsPerPlane == 32) { bitsPerChannel = 8; colorType = PNG_COLOR_TYPE_RGB_ALPHA; } else if (m_iBitsPerPlane == 48) { bitsPerChannel = 16; colorType = PNG_COLOR_TYPE_RGB; } else if (m_iBitsPerPlane == 64) { bitsPerChannel = 16; colorType = PNG_COLOR_TYPE_RGB_ALPHA; } else { return UT_ERROR; } png_set_IHDR ( m_pPNG, m_pPNGInfo, m_iWidth, m_iHeight, bitsPerChannel, colorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT ); } if ((err = Convert_BMP(&bBufBMP))) { return err; } /* Clean Up Memory Used */ FREEP(m_pPNGInfo->palette); png_destroy_write_struct(&m_pPNG, &m_pPNGInfo); return UT_OK; }