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: 20:31:30 09/16/03 Tue
Author: Michel
Subject: Re: Resource to Bitmap to Icon
In reply to: glup 's message, "Resource to Bitmap to Icon" on 11:52:30 09/16/03 Tue


Hi Glup,

There are two things here. Loading the bitmap and converting to icon.

What is the exception you get with:

wStream := TResourceStream.Create(HInstance, aResId, RT_BITMAP);
wBmp.LoadFromStream( wStream );

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

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;

So I would recommend loading with,

wBmp := TBitmap.Create;
wBmp.LoadFromResourceID(HInstance, aResId);

then use BitmapToIcon above.

Best regards,

Michel



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