[ Show ]
Support VoyForums
[ Shrink ]
VoyForums Announcement: Programming and providing support for this service has been a labor of love since 1997. We are one of the few services online who values our users' privacy, and have never sold your information. We have even fought hard to defend your privacy in legal cases; however, we've done it with almost no financial support -- paying out of pocket to continue providing the service. Due to the issues imposed on us by advertisers, we also stopped hosting most ads on the forums many years ago. We hope you appreciate our efforts.

Show your support by donating any amount. (Note: We are still technically a for-profit company, so your contribution is not tax-deductible.) PayPal Acct: Feedback:

Donate to VoyForums (PayPal):

Login ] [ Contact Forum Admin ] [ Main index ] [ Post a new message ] [ Search | Check update time | Archives: 12[3]45678910 ]

[ Next Thread | Previous Thread | Next Message | Previous Message ]

Date Posted: 20:42:43 09/17/03 Wed
Author: Michel
Subject: Re: Resource to Bitmap to Icon
In reply to: glup 's message, "Re: Resource to Bitmap to Icon" on 04:22:05 09/17/03 Wed

Hi Glup,

>>What is the exception you get with:
>>wStream := TResourceStream.Create(HInstance, aResId,
>>wBmp.LoadFromStream( wStream );
>EGmbError: gmb_read_header_failed.
>The exception is raised after gbm_read_header in

Is it possible to email me a simple program with the appropriate resource file that is given the error? I don't know if it is the bitmap file that TBitmapGraphic cannot read, or if it is a interaction with the TResourceStream.

>>Converting from a bitmap to an icon is not trivial
>>because of transparency. Here is some code I found on
>>newsgroups that works well but requires a TBitmap:
>>function BitmapToIcon(Bitmap: TBitmap): TIcon;
>> IconSizeX, IconSizeY : integer;
>> IconInfo: TIconInfo;
>> IconBitmap, MaskBitmap: TBitmap;
>> x, y: Integer;
>> TransparentColor: TColor;
>> IconSizeX := GetSystemMetrics(SM_CXICON);
>> IconSizeY := GetSystemMetrics(SM_CYICON);
>> IconBitmap := nil;
>> MaskBitmap := nil;
>> try
>> IconBitmap:= TBitmap.Create;
>> IconBitmap.Width:= IconSizeX;
>> IconBitmap.Height:= IconSizeY;
>> IconBitmap.Canvas.StretchDraw(Classes.Rect(0,
>>0, IconSizeX, IconSizeY), Bitmap);
>> IconBitmap.TransparentColor:=
>> TransparentColor:= IconBitmap.TransparentColor
>>and $FFFFFF;
>> MaskBitmap:= TBitmap.Create;
>> MaskBitmap.Assign(IconBitmap);
>> for y:= 0 to IconSizeY - 1 do
>> for x:= 0 to IconSizeX - 1 do
>> if IconBitmap.Canvas.Pixels[x, y] =
>>TransparentColor then
>> IconBitmap.Canvas.Pixels[x, y]:=
>> IconInfo.fIcon:= True;
>> IconInfo.hbmMask:= MaskBitmap.MaskHandle;
>> IconInfo.hbmColor:= IconBitmap.Handle;
>> Result:= TIcon.Create;
>> Result.Handle:= CreateIconIndirect(IconInfo);
>> finally
>> MaskBitmap.Free;
>> IconBitmap.Free;
>> end;
>First point the size: My graphic size is not system
>dependent and for the testcase 16x16. Doesn't Delphi
>do allready the stretching?

Instead of using GetSystemMetrics(SM_CXICON), you can set the size to the value you need.

>Second creating the Mask: TBitmap creates allready a
>handle for a correct mask when I set Transparent :=
>true. It calls the Graphics internal CopyBitmapAsMask.
>I have displayed that mask and it looks correct.

I am not sure if I understand. I have used the code above to assign an icon to a form successfully.

>But why is TIconGraphic adding this anoying whitspace
>and does not Stretch as it should?

TIconGraphic has a property called BackgroundColor to specify the background when loading the icon. As far as stretching, icons are not usually designed to be stretched.

Best regards,


>>>I need to display a bitmap 16x16px with 16 colors
>>>loaded from an resource as a form icon with correkt
>>>I want to use Envision Components only, since I will
>>>add later transformations and resized TIFFs.
>>>The resource-stored bitmap use clFuchsia as the
>>>transparent color.
>>>First problem is to load the graphic from the
>>>wBmp := TBitmapGraphic.Create;
>>>wStream := TResourceStream.Create(HInstance, aResId,
>>>wBmp.LoadFromStream( wStream );
>>>does not work - I get an exception.
>>>wBmp := TBitmap.Create;
>>>wBmp.LoadFromResourceID(HInstance, aResId);
>>>works without a Problem.
>>>Second issue is to Convert to an icon and set it to a
>>>TForm.Icon property:
>>>wIco := TIconGraphic.Create;
>>> wIco.Assign( wBmp );
>>> self.Icon := wIco;
>>> FreeAndNil(wIco);
>>>This icon has on my system (XP, 1600x1200, large
>>>lots of unwanted whitespace (clWhite). On an old
>>>system (Win95, 640x480, small fonts) it does not
>>>(possibly since the system icon size is 16x16). Also
>>>the transparent color is not recognized.
>>>Following I found somewhere:
>>>procedure TForm1.BitmapToIcon( const aBmp: TBitmap;
>>>const aIcon: TIcon );
>>> wIcon: THandle;
>>> wIconInfo: TIconInfo;
>>> aBmp.TransparentColor := clFuchsia;
>>> aBmp.Transparent := true;
>>> with wIconInfo do begin
>>> fIcon := true;
>>> hbmMask := aBmp.MaskHandle;
>>> hbmColor := aBmp.Handle;
>>> end;
>>> wIcon := CreateIconIndirect( wIconInfo );
>>> if wIcon = 0 then
>>> RaiseLastOsError;
>>> aIcon.Handle := wIcon;
>>>This works better; there is no whitespace, but still
>>>the transparency is still not recognized even the
>>>seams to be correct; I tested this with
>>>procedure TForm1.DrawMask( aBmp: TBitmap );
>>> wHelp: TBitmap;
>>> wBmp: TBitmap;
>>> wHelp := TBitmap.Create;
>>> try
>>> wHelp.Handle := aBmp.MaskHandle;
>>> wHelp.Transparent := false;
>>> wBmp := img1.Picture.Bitmap;
>>> wBmp.Width := 100;
>>> wBmp.Height := 100;
>>> wBmp.PixelFormat := pf8bit;
>>> wBmp.HandleType := bmDIB;
>>> wBmp.Canvas.Draw(20, 20, wHelp);
>>> finally
>>> FreeAndNil(wHelp);
>>> end;
>>>My questions again and explicit:
>>>1. How to load a RT_BITMAP with TBitmapGraphic
>>>2. Hot to convert general a TDibGraphic in 16x16 or
>>>32x32 and 16 to 256 colors to an Icon without
>>>whitespace and with correct transparency. A HICON
>>>would be sufficient, since I could just set
>>>Form1.Icon.Handle to it.
>>>I'm looking for the simplest solution (short and fast

[ Next Thread | Previous Thread | Next Message | Previous Message ]

[ Contact Forum Admin ]

Forum timezone: GMT-5
VF Version: 3.00b, ConfDB:
Before posting please read our privacy policy.
VoyForums(tm) is a Free Service from Voyager Info-Systems.
Copyright © 1998-2019 Voyager Info-Systems. All Rights Reserved.