The MASM Forum

General => The Workshop => Topic started by: TouEnMasm on May 11, 2013, 11:35:18 PM

Title: gdiplus loading a file from a resource
Post by: TouEnMasm on May 11, 2013, 11:35:18 PM

I try to make this without success.
the file in the resource is in any supported formats.
bmp seem possible.
Others not,any idea ?
I have used GdipLoadImageFromStream then GdipSaveImageToStream.
GdipSaveImageToStream failed.
method:
Quote
   invoke GlobalAlloc,GHND,16  ;
   mov Hnewglob,eax   
   
   invoke CreateStreamOnHGlobal,Hnewglob, TRUE, ADDR newstream
   invoke GetEncoderClsid,PUU(szencode,"image/bmp",0),addr CLSID_encoder
   invoke GdipSaveImageToStream,addr gdipImage,newstream,\
            addr CLSID_encoder,NULL

Title: Re: gdiplus loading a file from a resource
Post by: qWord on May 11, 2013, 11:49:38 PM
For bitmaps you can also use: GdipCreateBitmapFromResource(HINSTANCE hInstance, GDIPCONST WCHAR* lpBitmapName/resource ID, GpBitmap** bitmap).
(Remarks that the class Bitmap is derived from Image)
Title: Re: gdiplus loading a file from a resource
Post by: dedndave on May 11, 2013, 11:58:04 PM
Jose Roca has some code....

http://www.jose.it-berater.org/smfforum/index.php?topic=2988.0 (http://www.jose.it-berater.org/smfforum/index.php?topic=2988.0)
Title: Re: gdiplus loading a file from a resource
Post by: TouEnMasm on May 12, 2013, 12:11:03 AM

When i have no problem,it is from a file (on disk) or from a bitmap in resource.
There is problem when it is from a resource.
I try:
copy resource file in global memory,it's work (globalalloc or CoTaskMemAlloc).
I verify it's in memory and then:

;Hglob pointer or Handle
   invoke CreateStreamOnHGlobal, Hglob, TRUE, ADDR ppvIStream
   mov edx,ppvIStream
   invoke GdipCreateBitmapFromStream, ppvIStream,addr gdipImage


Title: Re: gdiplus loading a file from a resource
Post by: ragdog on May 12, 2013, 02:32:30 AM
Hi Yves

I hope it help you ;)

http://www.jose.it-berater.org/smfforum/index.php?topic=4654.0
Title: Re: gdiplus loading a file from a resource
Post by: Vortex on May 12, 2013, 03:22:13 AM
Hi ToutEnMasm,

Here is a GdipSaveImageToStream example for you.
Title: Re: gdiplus loading a file from a resource
Post by: dedndave on May 12, 2013, 04:11:40 AM
you have to use FindResource, LoadResource, LockResource

i think Gwapo (Chris) has a nice example out there someplace
Title: Re: gdiplus loading a file from a resource
Post by: TouEnMasm on May 12, 2013, 04:21:28 AM
After a laborious research,I have mixed informations and get a code who work.
First step is in the resource:
Quote
1200 RCDATA "ShapesR90.jpg"
The RCDATA seems the only one giving a final result.Then in code:

invoke FindResourceW, hModule, 1200, RT_RCDATA
mov hResource, eax
invoke LoadResource,hModule, hResource
invoke LockResource, eax
mov PimageFile, eax
invoke SizeofResource, hModule, hResource
mov file.nFileSizeLow, eax
invoke CoTaskMemAlloc,file.nFileSizeLow
;invoke GlobalAlloc,GHND,file.nFileSizeLow  ;
mov Hglob,eax
mov pmem,eax
;--------- Fill the memory --------------------
.if   step == 1
mov esi,PimageFile
mov edi,eax
mov ecx,file.nFileSizeLow
shr ecx,2 ;divise par 4,ignore reste
rep movsd
mov ecx,file.nFileSizeLow
and ecx,3 ;recupère le reste
.if ecx != 0
rep movsb
.endif
;invoke GlobalUnlock,Hglob ;pas supporté par Istream

;-------------- Hglob image file in memory GlobalAlloc ------
; create a stream for the Ipicture object's creator
mov edx,Hglob
invoke CreateStreamOnHGlobal, Hglob, TRUE, ADDR ppvIStream
mov edx,ppvIStream
invoke GdipCreateBitmapFromStream, ppvIStream,addr gdipImage
invoke GdipCreateHBITMAPFromBitmap,gdipImage,addr Hbmp,-1


The GdipCreateHBITMAPFromBitmap failed if the resource isn't in RCDATA
No differences in memory is visible with the debugger.
Title: Re: gdiplus loading a file from a resource
Post by: Vortex on May 12, 2013, 07:51:02 PM
Hi ToutEnMasm,

This one works with bitmap from resource :

    invoke       GetModuleHandle,0
    invoke       LoadBitmap,eax,100
   
    invoke       GdipCreateBitmapFromHBITMAP,eax,0,ADDR BmpImage


Code converted to Masm32
Title: Re: gdiplus loading a file from a resource
Post by: Vortex on May 12, 2013, 08:21:36 PM
Hi ToutEnMasm,

Another example :

100 RCDATA "image.png"

    invoke  FindResource,0,100,RT_RCDATA
    invoke  LoadResource,0,eax
    invoke  LockResource,eax
    mov     pPNG,eax
       
    invoke  CoTaskMemAlloc,PNG_FILE_SIZE
    mov     pGlobal,eax
    invoke  MemCopy,pPNG,eax,PNG_FILE_SIZE

    invoke  CreateStreamOnHGlobal,pGlobal,TRUE,ADDR pPNGstream
   
    invoke  GdipLoadImageFromStream,pPNGstream,ADDR PngImage
Title: Re: gdiplus loading a file from a resource
Post by: dedndave on May 12, 2013, 11:53:09 PM
a couple links for you to browse, Yves   :P

http://masm32.com/board/index.php?topic=967.0 (http://masm32.com/board/index.php?topic=967.0)
http://masm32.com/board/index.php?topic=309.0 (http://masm32.com/board/index.php?topic=309.0)
Title: Re: gdiplus loading a file from a resource
Post by: TouEnMasm on May 13, 2013, 02:38:44 AM
Thanks at all for help.
I see errors on someone:
Quote
100 RCDATA "image.png"
;--------------------------------------
    invoke  FindResource,0,100,RT_RCDATA ;failed need FindResourceW
Now the more complete sample is here ,gdiplus_dcobject.zip sample
http://masm32.com/board/index.php?topic=1889.msg19576#msg19576 (http://masm32.com/board/index.php?topic=1889.msg19576#msg19576)
Title: Re: gdiplus loading a file from a resource
Post by: Vortex on May 13, 2013, 03:47:07 AM
Hi ToutEnMasm,

invoke  FindResource,0,100,RT_RCDATA

This works for me. My OS is Win XP Sp3.

You need to provide a technical explanation to prove that the above code is wrong.
Title: Re: gdiplus loading a file from a resource
Post by: TouEnMasm on May 13, 2013, 04:45:59 AM
erratum
I have verify,it's work with Ansi and Unicode.
It's a sample on the old forum who put me on the bad way.
Title: Re: gdiplus loading a file from a resource
Post by: Vortex on May 13, 2013, 05:03:25 AM
Hi ToutEnMasm,

OK, no problem. Glad to know that it works fine for you.