I used a picture with alpha channel and some section of image is not fully opaque. When I draw it on a TImgView32
object that sections get a little color from Clear()
color. Lets show it by a sample picture:
This is the original picture that you normally see in Photoshop:
And this is the result image when I draw it on a new layer in a GR32 object that I cleared it before with Clear($00FF0000);
:
The result is transparent like photohop one but that red halo is the problem.
Or another color with Clear($000000FF);
:
Note that as you know the color I used is fully transparent.
Here is the code I used to create above result:
begin
ImageList.Bitmaps.Add; //ImageList is a TBitmap32List object
LoadPNGintoBitmap32(ImageList.Bitmap[0], ImgPath+'test.png', DummyBool);
BL:= TBitmapLayer.Create(ImgView.Layers);
ImgID:=0;
ScaleFactor:= 1;
LayerPos:= Point(100, 100);
with ImageList.Bitmap[ImgID] do
ImageRect:= Rect(0, 0, Width, Height);
{Auxilary variables}
CanvasWidth:= ImageRect.Right{*2}; //Extending width for two icon.
CanvasHeight:= ImageRect.Bottom;
BL.Location:= FloatRect(
LayerPos.X,
LayerPos.Y,
LayerPos.X + CanvasWidth * ScaleFactor,
LayerPos.Y + CanvasHeight * ScaleFactor
);
with BL.Bitmap do
begin
{Init}
SetSize(CanvasWidth, CanvasHeight);
DrawMode:= dmBlend;
Clear($00FF0000);
{Insert image}
Draw(0, 0, ImageRect, ImageList.Bitmap[ImgID]);
end;
if ScaleFactor<>1 then
TDraftResampler.Create(BL.Bitmap); //BL.Bitmap, because we used "Draw" method
end;
How to avoid this and get a clear result like photoshop one?
2
Answers
I have had a similar problem recently
the TImageList does not support transparency, so I had to use a TPngImageList when I wanted to load transparent png images into controls at runtime.
TPngImageList is available via a google search!
As far as I can see it works as designed, provided you’re using the
cmBlend
combine mode.cmBlend
doesn’t take the alpha of the background into account and, as the documentation states, should not be used to blend bitmaps:Use the
cmMerge
combine mode instead when both the foreground and the background alpha is significant.