How to load a png image into a TImage

被刻印的时光 ゝ 提交于 2020-07-03 02:36:12

问题


I am trying to load a png image into a TImage with Delphi XE4. The png starts off in a stream: E.g.

  Stream := TMemoryStream.Create;
  try
    Stream.LoadFromFile('c:\file.png');
    Stream.Position := 0;
    Image1.Picture.Graphic.LoadFromStream(Stream);
  finally
    Stream.Free;
  end; 

I get an AV when I run this code. Can anyone tell me what I'm doing wrong?

Thanks.


回答1:


The TImage.Picture.Graphic property is nil until you load a graphic into the Picture.

What you are asking for can be achieved as follows:

  uses pngimage;

  Stream := TMemoryStream.Create;
  try
    // obtain png image, load from file or other..
    ....
    Image := TPngImage.Create;
    try
      Stream.Position := 0;
      Image.LoadFromStream(Stream);
      Image1.Picture.Graphic := Image;
    finally
      Image.Free;
    end;
  finally
    Stream.Free;
  end;


来源:https://stackoverflow.com/questions/33595925/how-to-load-a-png-image-into-a-timage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!