📄 codeguru going from a jpg-gif-bmp file to a hbitmap file using plain api.htm
字号:
extended implementation</B><BR><BR></TD>
<TD class=bodycopy vAlign=top
align=right></TD></TR>
<TR>
<TD class=bodycopy vAlign=top
colSpan=2>Originally posted by: <A
href="mailto:NOSPAMStingX@mail.ru">StingX</A>
<P><TT><PRE>//This function doesn't return HBITMAP, it returns picture as IPicture interface.
<BR>//You can load resource or file.
<BR>//If szType=NULL, then szID identifies the filename
<BR>HRESULT LoadPicture(LPCSTR szType, LPCSTR szID, char OverrideBPP,LPPICTURE* ppv){
<BR> HRSRC hRes;
<BR> LPVOID pMem;
<BR> HGLOBAL pGlobal;
<BR> LPPICTURE pPic=0;
<BR> LPSTREAM pStream=0;
<BR> HBITMAP hBmp=0,hRetBmp;
<BR> BITMAP bmp;
<BR> HDC hDCDest,hDCSrc;
<BR> DWORD dwSize;
<BR> int file;
<BR> DWORD dwRead,dwTotal;
<BR> PICTDESC pd;
<BR> if (!szID || !ppv) return E_INVALIDARG;
<BR> if (!szType) {
<BR> file=_open(szID,_O_RDONLY | _O_BINARY);
<BR> if (file==-1) return FALSE;
<BR> dwSize=_filelength(file);
<BR> pGlobal=GlobalAlloc(0,dwSize);
<BR> dwRead=0;dwTotal=0;
<BR>
<BR> while (dwTotal<dwSize){
<BR> dwRead=_read(file,((LPBYTE)pGlobal)+dwTotal,512); //Read sector by sector
<BR> if (!dwRead) {_close(file);goto Err;}
<BR> dwTotal+=dwRead;
<BR> }
<BR> _close(file);
<BR> } else {
<BR> hRes=FindResource(NULL,szID,szType);
<BR> if (!hRes) return NULL;
<BR> pMem=LoadResource(NULL,hRes);
<BR> if (!pMem) return NULL;
<BR> dwSize=SizeofResource(NULL,hRes);
<P> pGlobal=GlobalAlloc(0,dwSize);
<BR> memcpy(pGlobal,pMem,dwSize);
<BR> }
<P> if (FAILED(CreateStreamOnHGlobal(pGlobal,FALSE,&pStream))) goto Err;
<BR>
<BR> if (FAILED(OleLoadPicture(pStream,dwSize,TRUE,IID_IPicture,(LPVOID*)&pPic))) goto Err;
<BR>
<BR> pStream->Release(); pStream=0;
<BR> GlobalFree(pGlobal);pGlobal=0;
<BR> if (OverrideBPP<=0) {
<BR> *ppv=pPic;
<BR> } else {
<BR> //Make a copy of bitmap with given Bit per pixel value
<BR> if (FAILED(pPic->get_Handle((OLE_HANDLE*)&hBmp))) goto Err;
<BR> hDCSrc=CreateCompatibleDC(0);
<BR> GetObject(hBmp,sizeof(BITMAP),&bmp);
<BR> hBmp=(HBITMAP)SelectObject(hDCSrc,hBmp);
<BR> hRetBmp=CreateBitmap(bmp.bmWidth,bmp.bmHeight,1,OverrideBPP,NULL);
<BR> hDCDest=CreateCompatibleDC(0);
<BR> hRetBmp=(HBITMAP)SelectObject(hDCDest,hRetBmp);
<BR> BitBlt(hDCDest,0,0,bmp.bmWidth,bmp.bmHeight,hDCSrc,0,0,SRCCOPY);
<BR> SelectObject(hDCSrc,hBmp);
<BR> hRetBmp=(HBITMAP)SelectObject(hDCDest,hRetBmp);
<BR> DeleteDC(hDCSrc);hDCSrc=0;
<BR> DeleteDC(hDCDest);hDCDest=0;
<BR> pPic->Release();pPic=NULL;
<BR> //Create new IPicture interface
<BR> pd.cbSizeofstruct=sizeof(PICTDESC);
<BR> pd.picType=PICTYPE_BITMAP;
<BR> pd.bmp.hbitmap=hRetBmp;
<BR> pd.bmp.hpal=NULL;
<BR> if (FAILED(OleCreatePictureIndirect(&pd,IID_IPicture,TRUE,(LPVOID*)&pPic))) {DeleteObject(hRetBmp); return E_FAIL;}
<BR> }
<BR> return S_OK;
<BR>Err:
<BR> if (pStream) {pStream->Release();}
<BR> if (pGlobal) GlobalFree(pGlobal);
<BR> if (pPic) pPic->Release();
<BR> if (hBmp) DeleteObject(hBmp);
<BR> return E_FAIL;
<BR>}
<P>//For use IPicture you can use
<BR>HBITMAP LoadPictureEx(LPCTSTR szFile) {
<BR>HBITMAP hBmp;
<BR>LPPICTURE pPic=0;
<BR>if (FAILED(LoadPicture(NULL,_T("C:\\somefile.jpg"),0,&pPic))) goto Err;
<BR>if (FAILED(pPic->get_Handle((OLE_HANDLE)&hBmp))) goto Err;
<BR>hBmp=CopyImage(hBmp,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION);
<BR>if (!hBmp) goto Err;
<BR>pPic->Release();
<BR>return hBmp;
<BR>Err:
<BR>if (pPic) pPic->Release();
<BR>}
</P></PRE></TT>
<P></P></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD class=bodycopy vAlign=top>xLinux<BR><BR><FONT
color=#666666><I>05/14/2005<BR>5:25pm</I>
</FONT></TD>
<TD class=bodycopy vAlign=top width="82%">
<TABLE width="100%" border=0>
<TBODY>
<TR>
<TD class=bodycopy vAlign=top><B>Please correct
with this code.</B><BR><BR></TD>
<TD class=bodycopy vAlign=top
align=right></TD></TR>
<TR>
<TD class=bodycopy vAlign=top colSpan=2><PRE>Hi friend, I check the code and have some bugs, please the correct function o method LoadPictureEx(LPCTSTR szFile)is:
HBITMAP CNovaleDlg::LoadPictureEx(LPCTSTR szFile)
{
HBITMAP hBmp;
LPPICTURE pPic=0;
if (FAILED(LoadPicture(NULL,szFile,0,&pPic))) goto Err;
if (FAILED(pPic->get_Handle((OLE_HANDLE *)&hBmp))) goto Err;
hBmp=(HBITMAP)CopyImage(hBmp,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION);
if (!hBmp) goto Err;
pPic->Release();
return hBmp;
Err:
if (pPic) pPic->Release();
}
The best Regards
xLinux</PRE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=3 bgColor=#cccccc border=0>
<TBODY>
<TR>
<TD class=bodycopy colSpan=2>
<HR>
<BR><FONT class=title>Add a Reply: </FONT></TD></TR>
<TR>
<TD class=bodycopy><INPUT type=hidden value=add
name=action>
<CENTER>
<TABLE class=bodycopy>
<TBODY>
<TR vAlign=top>
<TD class=bodycopy align=right><STRONG>Title:
</STRONG></TD>
<TD><INPUT maxLength=256 size=60 name=title></TD></TR>
<TR vAlign=top>
<TD class=bodycopy align=right><STRONG>Comment:
</STRONG></TD>
<TD><TEXTAREA name=subject rows=5 wrap=physical cols=50></TEXTAREA></TD></TR>
<TR vAlign=top>
<TD class=bodycopy
align=right><STRONG>Pre-Formatted: </STRONG></TD>
<TD><INPUT type=checkbox value=on
name=preformatted>Check this if you want the text
to display with the formatting as typed (good for
source code)</TD></TR></TBODY></TABLE><BR><INPUT
type=image height=23 width=73
src="CodeGuru Going from a JPG-GIF-BMP File to a HBITMAP File Using Plain API_files/submit.gif"
value="Add Comment" border=0 name=AddComment>
</CENTER><BR><BR><B>(You must be signed in to comment on
an article. Not a member? <A
href="http://www.codeguru.com/register.php?url=/Cpp/G-M/bitmap/comments.php/c4935/?thread=55124">Click
here</A> to register)</B>
</FORM></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></SPAN></TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD bgColor=#666666 colSpan=2><IMG height=2 alt=""
src="CodeGuru Going from a JPG-GIF-BMP File to a HBITMAP File Using Plain API_files/sp.gif"
width=1 border=0></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=10 width="100%" bgColor=#ffffff border=0>
<TBODY>
<TR>
<TD align=middle>
<TABLE
style="BORDER-RIGHT: #003366 1px solid; BORDER-TOP: #003366 1px solid; BORDER-LEFT: #003366 1px solid; BORDER-BOTTOM: #003366 1px solid"
height=100 cellSpacing=0 cellPadding=5 width=750 bgColor=#ffffff
border=0><TBODY>
<TR height=12>
<TD
style="FONT: bold 14px Arial, Helvetica, sans-serif; BORDER-BOTTOM: #000000 1px solid"
bgColor=#0066cc colSpan=4 link="#000000"><FONT
color=#ffffff>Featured Whitepapers from the HP ProCurve Networking
Resource Center</FONT> </TD></TR>
<TR>
<TD
style="BORDER-RIGHT: #000000 1px solid; FONT: 12px Arial, Helvetica, sans-serif"
vAlign=top width="33%">WHITEPAPER <BR><A
href="http://www.codeguru.com/RealMedia/ads/click_lx.ads/ew/ewsoftware/www.codeguru.com/cpp/g-m/bitmap/comments/c4935/i/1242325699/468x60-2/OasDefault/HP_ProCurve_GEMS_3ff/ExtendingEnterprise.gif/63623963643532363435396231633230?index.php/2831_ExtendingEnterprise"><FONT
color=#000000><B>Extending the Enterprise Network Through
Mobility</B></FONT></A><BR><FONT color=#0066cc>Learn how to reduce
the overall cost of mobile-enabling your enterprise. </FONT></TD>
<TD
style="BORDER-RIGHT: #000000 1px solid; FONT: 12px Arial, Helvetica, sans-serif"
vAlign=top width="34%">CASE STUDY <BR><A
href="http://www.codeguru.com/RealMedia/ads/click_lx.ads/ew/ewsoftware/www.codeguru.com/cpp/g-m/bitmap/comments/c4935/i/1242325699/468x60-2/OasDefault/HP_ProCurve_GEMS_3ff/beeIT.gif/63623963643532363435396231633230?index.php/2831_beeIT"><FONT
color=#000000><B>ProCurve Networking by HP: Bee Information
Technology</B></FONT></A><BR><FONT color=#0066cc>Improve the
performance, reliability, and interoperability of your network. Read
this. </FONT></TD>
<TD style="FONT: 12px Arial, Helvetica, sans-serif" vAlign=top
width="33%">WHITEPAPER <BR><A
href="http://www.codeguru.com/RealMedia/ads/click_lx.ads/ew/ewsoftware/www.codeguru.com/cpp/g-m/bitmap/comments/c4935/i/1242325699/468x60-2/OasDefault/HP_ProCurve_GEMS_3ff/IDM-Tech-Brief.gif/63623963643532363435396231633230?index.php/2831_IDM-Tech-Brief"><FONT
color=#000000><B>IDM Technical Brief</B></FONT></A><BR><FONT
color=#0066cc>Enable your network to adapt to each user
individually, on the basis of business needs and access rights.
</FONT></TD></TD></TR></TBODY></TABLE><IMG height=1
src="G:\DownloadSource\OpenCV\study\CodeGuru Going from a JPG-GIF-BMP File to a HBITMAP File Using Plain API_files\63623963643532363435396231633230(19).gif"
width=1></TD></TR>
<TR>
<TD class=bodycopy align=middle>
<CENTER><BR clear=all>
<CENTER>
<TABLE cellSpacing=0 cellPadding=5 border=0>
<TBODY>
<TR>
<FORM action=http://search.internet.co
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -