delphi 16bit - GPF when attempting to stretchdraw bitmap loaded from resource

Quote
sunil gupta wrote:

> I cant seem to solve this one, perhaps someone out there has an answer.

> when I load a bitmap from a resource, I find that I get a GPF if I
> try and StretchDraw it anywhere.

> I used loadbitmap to get the bitmap

>   mybitmap = Tbitmap.Create;
>   deleteobject(mybitmap.handle);
>   mybitmap.handle := loadbitmap(Hinstance,resource_name_pchar);

> and then

>   targetbitmap.stretchdraw(myrect,mybitmap);

> GPFs, even though
>   targetbitmap.draw(0,0,myrect,mybitmap);

> doesnt.

> I used the info in  TI2947 to load a bitmap
> using memory streams, and it got me a better picture
> but still GPFs on stretchdraw.

> Any suggestions

> function make_bitmap_from_resource(resource_name: string):TBitmap;
> var
>   Bmp, new_bmp: TBitmap;
>   BMF: TBitmapFileHeader;
>   HResInfo: THandle;
>   MemHandle: THandle;
>   Stream: TMemoryStream;
>   ResPtr: PByte;
>   ResSize: Longint;
>   resource_pchar: pchar;
> begin
>   BMF.bfType := BM;

>   {Find, Load, and Lock the Resource containing resource_name}
>   resource_pchar := to_pchar(resource_name);
>   HResInfo := FindResource(HInstance, resource_pchar, RT_Bitmap);
>   strdispose(resource_pchar);
>   MemHandle := LoadResource(HInstance, HResInfo);
>   ResPtr := LockResource(MemHandle);

>   {Create a Memory stream, set its size, write out the bitmap
>    header, and finally write out the Bitmap                              }
>   Stream := TMemoryStream.Create;
>   ResSize := SizeofResource(HInstance, HResInfo);
>   Stream.SetSize(ResSize + SizeOf(BMF));
>   Stream.Write(BMF, SizeOf(BMF));
>   Stream.Write(ResPtr^, ResSize);

>   {Free the resource and reset the stream to offset 0}
>   FreeResource(MemHandle);
>   Stream.Seek(0, 0);

>   {Create the TBitmap and load the image from the MemoryStream}
>   Bmp := TBitmap.Create;
>   Bmp.LoadFromStream(Stream);

>   {draw into an ordinary bitmap - redundant}
>   new_bmp := TBitmap.create;
>   new_bmp.width := bmp.width;
>   new_bmp.height := bmp.height;
>   new_bmp.canvas.draw(0,0,bmp);

>   {done with temporary stuff}
>   Stream.Free;
>   bmp.free;

>   {return the bitmap}
>   make_bitmap_from_resource := new_bmp;
>  end;

> --
> http://www.magnetic.demon.co.uk/ home of:
> the HTML dictionary, java no sugar,
> a flash utility for picking lottery numbers
> and some of my painted artwork.
>>>>  deleteobject(mybitmap.handle);

Don't do that!

Joe
--
Joe C. Hecht
(Borland Delphi Developer Support)
Join the Delphi Online Discussion Forum at
http://www.borland.com/techsupport/delphi/