title: How to copy part of an image in another one in Delphi without losing transparency
body: <p>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.</p>
  
  <p>I have also this information:</p>
  
  <p>dstX = x-coordinate of destination point.</p>
  
  <p>dstY = y-coordinate of destination point.</p>
  
  <p>srcX = x-coordinate of source point.</p>
  
  <p>srcY = y-coordinate of source point.</p>
  
  <p>srcW = Source width.</p>
  
  <p>srcH = Source height.</p>
  
  <p>I try to user copyRect like this</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>With CopyRect, I have two problems.</p>
  
  <ol>
  <li>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.</li>
  <li>I lose transparencies.</li>
  </ol>
  
  <p>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.</p>
  
  <p>I search about this problem, first to copy the image png inside bg I try this, but not success:</p>
  
  <p><a href="http://www.delphitricks.com/source-code/graphic/copy_part_of_one_image_to_another.html" rel="nofollow noreferrer">http://www.delphitricks.com/source-code/graphic/copy_part_of_one_image_to_another.html</a></p>
  
  <p>Then for the transparency problem I read</p>
  
  <p><a href="https://stackoverflow.com/questions/12717003/alphablend-and-transparentblt">Alphablend and TransparentBlt</a> and <a href="https://stackoverflow.com/questions/25577387/why-am-i-losing-transparency-when-calling-bitblt-or-copyrect">Why am I losing transparency when calling BitBlt or CopyRect?</a></p>
  
  <p>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.</p>