如何将任意图像从BLOB流加载到TImage?

如何将任意图像从BLOB流加载到TImage?

问题描述:

如果我理解正确, TImage.LoadFromFile 确定文件扩展名中的图片类型。

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

有没有办法从 TBlobStream 自动检测图像类型,其中包含原始图像?

Is there any way to detect the image type automatically from a TBlobStream with a raw image in it?

我目前的代码:

procedure LoadImageFromStream(AImage: TImage; ADataSet: TDataSet);
var
  Stream: TStream;
begin
  Stream := ADataSet.CreateBlobStream(Field, bmRead);
  try
    AImage.Picture.Graphic.LoadFromStream(Stream);
  finally
    Stream.Free;
  end;
end


看到这个 SO答案从标题文件内容检索。

See this SO answer for file content retrieval from header.

或者您可以使用我们的 TSynPicture class ,它将使用Gdi +库处理所有类型的图片(bmp / gif / tiff / jpg / png),在一个类中。所以你的 TPicture 可以是这个独特的类,适用于任何种类的图片。与Jpeg或PNG Delphi单元相比,代码开销较少。

Or you can use our TSynPicture class, which will handle all kind of pictures (bmp/gif/tiff/jpg/png) using Gdi+ library, in one single class. So your TPicture can be this unique class, for any kind of picture. With less code overhead than the Jpeg or PNG Delphi units.

var Pic: TSynPicture;

Pic := TSynPicture.Create;
Pic.LoadFromStream(aStream); // will load bmp/gif/tiff/jpeg/png content
AImage.Picture.Graphic := Pic;
....