VoyForums
[ 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: 04:22:05 09/17/03 Wed
Author: glup
Subject: Re: Resource to Bitmap to Icon
In reply to: Michel 's message, "Re: Resource to Bitmap to Icon" on 20:31:30 09/16/03 Tue

Hi Michel

>What is the exception you get with:
>
>wStream := TResourceStream.Create(HInstance, aResId,
>RT_BITMAP);
>wBmp.LoadFromStream( wStream );

EGmbError: gmb_read_header_failed.
The exception is raised after gbm_read_header in LoadGraphicWithGbm.

>
>I have not tried it, but LoadFromStream should be
>generic. Maybe the stream needs to be reset with
>Seek(0, soFromBeginning) ?

The Stream is at its begin.

>
>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;
>var
> IconSizeX, IconSizeY : integer;
> IconInfo: TIconInfo;
> IconBitmap, MaskBitmap: TBitmap;
> x, y: Integer;
> TransparentColor: TColor;
>begin
> 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:=
>Bitmap.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]:= clBlack;
> IconInfo.fIcon:= True;
> IconInfo.hbmMask:= MaskBitmap.MaskHandle;
> IconInfo.hbmColor:= IconBitmap.Handle;
> Result:= TIcon.Create;
> Result.Handle:= CreateIconIndirect(IconInfo);
> finally
> MaskBitmap.Free;
> IconBitmap.Free;
> end;
>end;
>

Mhhh...
First point the size: My graphic size is not system dependent and for the testcase 16x16. Doesn't Delphi do allready the stretching?
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.

>So I would recommend loading with,
>
>wBmp := TBitmap.Create;
>wBmp.LoadFromResourceID(HInstance, aResId);
>
>then use BitmapToIcon above.
>
[...]

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

Rgds

:glup

>
>>I need to display a bitmap 16x16px with 16 colors
>>loaded from an resource as a form icon with correkt
>>tranparency.
>>
>>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
>resource:
>>
>>wBmp := TBitmapGraphic.Create;
>>wStream := TResourceStream.Create(HInstance, aResId,
>>RT_BITMAP);
>>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;
>>try
>> wIco.Assign( wBmp );
>> self.Icon := wIco;
>>finally
>> FreeAndNil(wIco);
>>end;
>>
>>This icon has on my system (XP, 1600x1200, large font)
>>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 );
>>var
>> wIcon: THandle;
>> wIconInfo: TIconInfo;
>>begin
>> 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;
>>end;
>>
>>
>>This works better; there is no whitespace, but still
>>the transparency is still not recognized even the Mask
>>seams to be correct; I tested this with
>>
>>procedure TForm1.DrawMask( aBmp: TBitmap );
>>var
>> wHelp: TBitmap;
>> wBmp: TBitmap;
>>begin
>> 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;
>>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
>>code).
>>
>>
>>:-glup

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


Replies:


[ 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.