This is my code, I have a background picture width: 245 and height: 150 and a png picture 128x128, so I copy parts of the png picture inside the background picture.
I have also this information:
dstX = x-coordinate of destination point.
dstY = y-coordinate of destination point.
srcX = x-coordinate of source point.
srcY = y-coordinate of source point.
srcW = Source width.
srcH = Source height.
I try to user copyRect like this
procedure TMyClass.FillImage;
var
bg,png: TPngImage;
dstX,dstY,srcW,srcH,srcX,srcY: Integer;
begin
bg := TPngImage.CreateBlank(COLOR_RGB,8,245,150);
png := TPngImage.Create;
png.LoadFromFile('C:\temp\example.png');
dstX := 10;dstY:=0;srcW:=0;srcH:=118;srcX:=128;srcY:=10;
bg.Canvas.CopyRect(Rect(dstX,dstY,srcW,srcH),png.Canvas,Rect(srcX,srcY,png.Width,png.Height));
//With this values not working, I have the same result as before, should start copy in position 138 of bg.
dstX := 138;dstY:=0;srcW:=0;srcH:=118;srcX:=10;srcY:=10;
bg.Canvas.CopyRect(Rect(dstX,dstY,srcW,srcH),png.Canvas,Rect(srcX,srcY,png.Width,png.Height));
end;
With CopyRect, I have two problems.
- When the destX is bigger than the png picture, it doen't start copy in the right position inside the background picture, bg. This is the main issue.
- I lose transparencies.
The background picture has not to be a png format at the beginning because it is created at the beginning with only the width and height but at the end has to be save as png.
I search about this problem, first to copy the image png inside bg I try this, but not success:
http://www.delphitricks.com/source-code/graphic/copy_part_of_one_image_to_another.html
Then for the transparency problem I read
Alphablend and TransparentBlt and Why am I losing transparency when calling BitBlt or CopyRect?
But in my case is different because I do not use a paintbox as you can see, and and using a png image and I copy parts of this image inside another with the information I put above. But maybe this post can inspire someone to help me.