delphisave-image

Save ImageEnView and a Label on it as Png in delphi


I Put a TImageEnView on my form and put a Label on the TImageEnView. I want to save this parent and child as one Png or Jpg on my hard drive.

ImageEnView and label on it

I write this code :

    CharLbl.Font.Size := I;
    CharLbl.Top:=22;
    ImageEnIO1.SaveToFile('D:\output2.png'); // Save in thread 2
    ImageEnIO1.WaitThreads(false);
    ShowMessage(inttoStr(I));

But the output is only background with out Label. How can I save the label as well?


Solution

  • Try the following:

    var
    paintbmp:tbitmap;
    
    begin
    paintbmp:=tbitmap.Create;
      try
      paintbmp.Width:=ImageEnIO1.Width;
      paintbmp.Height:=ImageEnIO1.Height;
    
      paintbmp.Canvas.Draw(0,0,ImageEnIO1.Picture.Graphic);
      paintbmp.Canvas.CopyRect(rect(0,0,ImageEnIO1.Width,ImageEnIO1.Height)
                              ,CharLbl.Canvas
                              ,rect(0,0,ImageEnIO1.Width,ImageEnIO1.Height));
    
      paintbmp.SaveToFile('D:\output2.png');
      finally
      paintbmp.Free;
      end;
    end;
    

    Just be careful in order for this to give you what you want the size of the label is to be the same as the image's and the top and left is the same as the image's.

    Note: I would still recommend you to see the link I gave you in comments, because it will aid you to learn a valuable tool that would even enable you to write your own component in the future.

    Note 2: The output image is not a valid PNG it is still a Bitmap so you still need to convert it.(thanks to Kobik)