How to load an arbitrary image from a BLOB stream into a TImage?

前端 未结 4 1355
攒了一身酷
攒了一身酷 2020-12-28 22:46

If I understand it correctly, TImage.LoadFromFile determines the type of picture from the file extension.

Is there any way to detect the image type auto

4条回答
  •  悲&欢浪女
    2020-12-28 23:12

    Seemingly magic TPicture cunning handling for arbitrary image formats is actually very simple (not to say crude). Loading from files relies on file extension. Loading from clipboard - on clipboard format indicator. See? There is always format tag which instructs TPicture which TGraphicClass to use on the data, and TGraphic base class itself provides no mechanism to identify "own" data streams besided trial-and-error approach. One might be curious how TPicture loads itself from DFM stream, but it is not an exception, here is relevant excerpt from implementation (copyrighted code provided for illustrative purpose only):

    procedure TPicture.ReadData(Stream: TStream);
    var
      {...}
      GraphicClass: TGraphicClass;
      LClassName: string;
      LBytes: TBytes;
      LNameLen: Byte;
    begin
      Stream.Read(LNameLen, 1);
      SetLength(LBytes, LNameLen);
      Stream.Read(LBytes{$IFNDEF CLR}[0]{$ENDIF}, LNameLen);
      LClassName := TEncoding.UTF8.GetString(LBytes);
    
      GraphicClass := FileFormats.FindClassName(LClassName);
    

提交回复
热议问题