1

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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
sina
  • 331
  • 4
  • 12
  • 1
    it might be only me but I think you are trying to save a component "timageenview" and expecting that it will automatically save the label (another component) with it with out any special implementation. please if you want to write something on the image then see this [link](https://stackoverflow.com/questions/360476/writing-transparent-text-on-image?s=1|0.9785) – Nasreddine Galfout May 02 '17 at 13:42
  • 3
    You need to save the image to an intermediate BMP first, then draw the label onto the BMP, then save the BMP to disk in the desired format (PNG, JPG, etc) – Remy Lebeau May 02 '17 at 14:31

1 Answers1

1

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)

Nasreddine Galfout
  • 2,550
  • 2
  • 18
  • 36