三九宝宝网宝宝绘画儿童画

mfc编程如何设置用图片控件动态显示jpg图像

02月12日 编辑 39baobao.com

jpg显示的步骤:1。打开图片控件的绘图DC。2。使用OleLoadPicture函数读取jpg图像到内存中。(导入olepro32.dll)3。向DC上绘制图像示例如下:CDC *pDC = GetDlgItem(IDC_STATIC_IMG)->GetDC(); //获取控件DCHDC hDC = (HDC)pDC;HANDLE hFile=CreateFile(szImagePath, 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); //关闭文件句柄IStream *pIStream;//创建一个IStream接口指针,用来保存图片流IPicture *pIPicture;//创建一个IPicture接口指针,表示32313133353236313431303231363533e78988e69d8331333337613735图片对象CreateStreamOnHGlobal(hImageMemory, false, &pIStream); //用全局内存初使化IStream接口指针OleLoadPicture(pIStream, 0, false, IID_IPicture, (LPVOID*)&(pIPicture));//用OleLoadPicture获得IPicture接口指针//得到IPicture COM接口对象后,你就可以进行获得图片信息、显示图片等操作OLE_XSIZE_HIMETRIC hmWidth;OLE_YSIZE_HIMETRIC hmHeight;pIPicture->get_Width(&hmWidth); //用接口方法获得图片的宽和高pIPicture->get_Height(&hmHeight);pIPicture->Render(hDC,0,0,100,100,0,hmHeight,hmWidth,-hmHeight,NULL); //在指定的DC上绘出图片GlobalFree(hImageMemory); //释放全局内存pIStream->Release(); //释放pIStreampIPicture->Release(); //释放pIPictureReleaseDC(pDC);

推荐阅读
图文推荐