如何使用TBlobField中的TDBImage显示图片(jpg)? [英] How to display picture(jpg) with TDBImage from TBlobField?

查看:371
本文介绍了如何使用TBlobField中的TDBImage显示图片(jpg)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示设置为blobdata类型的数据库中的图像。
但是当我与TDBImage配对时显示错误

I want to display image from database that was set to blobdata type. But when i pair with TDBImage show error

任何人都可以帮忙吗?

推荐答案

我一直在使用此代码从数据库中加载任何类型的图像。

I have been using this code to load any kind of image from Database.

uses
  Vcl.Graphics, PNGImage, JPEG;

function GetHeader(const AFile: string; const AByteCount: integer): string;
const
  HEADER_STR = '%s_HEADER: array [0 .. %d] of byte = (%s)';
var
  _HeaderStream: TMemoryStream;
  _FileStream: TMemoryStream;
  _Buf: integer;
  _Ext: string;
  _FullByteStrArr: string;
  _ByteStr: string;
  i: integer;
begin
  Result := '';
  if not FileExists(AFile) then
    Exit;

  _HeaderStream := TMemoryStream.Create;
  _FileStream := TMemoryStream.Create;
  try
    _FileStream.LoadFromFile(AFile);
    _FileStream.Position := 0;
    _HeaderStream.CopyFrom(_FileStream, 5);
    if _HeaderStream.Size > 4 then
    begin
      _HeaderStream.Position := 0;
      _ByteStr := '';
      _FullByteStrArr := '';
      for i := 0 to AByteCount do
      begin
        _HeaderStream.Read(_Buf, 1);
        _ByteStr := IntToHex(_Buf, 2);
        _FullByteStrArr := _FullByteStrArr + ', $' +
          Copy(_ByteStr, Length(_ByteStr) - 1, 2);
      end;
      _FullByteStrArr := Copy(_FullByteStrArr, 3, Length(_FullByteStrArr));

      _Ext := UpperCase(ExtractFileExt(AFile));
      _Ext := Copy(_Ext, 2, Length(_Ext));
      Result := Format(HEADER_STR, [_Ext, AByteCount, _FullByteStrArr]);
    end;
  finally
    FreeAndNil(_FileStream);
    FreeAndNil(_HeaderStream);
  end;
end;

function GetImageFromBlob(const ABlobField: TBlobField): TGraphic;
CONST
  JPG_HEADER: array [0 .. 2] of byte = ($FF, $D8, $FF);
  GIF_HEADER: array [0 .. 2] of byte = ($47, $49, $46);
  BMP_HEADER: array [0 .. 1] of byte = ($42, $4D);
  PNG_HEADER: array [0 .. 3] of byte = ($89, $50, $4E, $47);
  TIF_HEADER: array [0 .. 2] of byte = ($49, $49, $2A);
  TIF_HEADER2: array [0 .. 2] of byte = (77, 77, 00);
  PCX_HEADER: array [0 .. 2] of byte = (10, 5, 1);

var
  _HeaderStream: TMemoryStream;
  _ImgStream: TMemoryStream;
  _GraphicClassName: string;
  _GraphicClass: TGraphicClass;
begin
  Result := nil;

  _HeaderStream := TMemoryStream.Create;
  _ImgStream := TMemoryStream.Create;
  try
    ABlobField.SaveToStream(_ImgStream);
    _ImgStream.Position := 0;
    _HeaderStream.CopyFrom(_ImgStream, 5);
    if _HeaderStream.Size > 4 then
    begin
      if CompareMem(_HeaderStream.Memory, @JPG_HEADER, SizeOf(JPG_HEADER)) then
        _GraphicClassName := 'TJPEGImage'
      else if CompareMem(_HeaderStream.Memory, @GIF_HEADER, SizeOf(GIF_HEADER))
      then
        _GraphicClassName := 'TGIFImage'
      else if CompareMem(_HeaderStream.Memory, @PNG_HEADER, SizeOf(PNG_HEADER))
      then
        _GraphicClassName := 'TPNGImage'
      else if CompareMem(_HeaderStream.Memory, @BMP_HEADER, SizeOf(BMP_HEADER))
      then
        _GraphicClassName := 'TBitmap'
      else if CompareMem(_HeaderStream.Memory, @TIF_HEADER, SizeOf(TIF_HEADER))
      then
        _GraphicClassName := 'TWICImage'
      else if CompareMem(_HeaderStream.Memory, @TIF_HEADER2, SizeOf(TIF_HEADER2))
      then
        _GraphicClassName := 'TWICImage'
      else if CompareMem(_HeaderStream.Memory, @PCX_HEADER, SizeOf(PCX_HEADER))
      then
        _GraphicClassName := 'PCXImage';

      RegisterClasses([TIcon, TMetafile, TBitmap, TJPEGImage, TPngImage,
        TWICImage]);
      _GraphicClass := TGraphicClass(FindClass(_GraphicClassName));
      if (_GraphicClass <> nil) then
      begin
        Result := _GraphicClass.Create; // Create appropriate graphic class
        _ImgStream.Position := 0;
        Result.LoadFromStream(_ImgStream);
      end;
    end;
  finally
    FreeAndNil(_ImgStream);
    FreeAndNil(_HeaderStream);
  end;
end;

{ --- Usage --- }

procedure TForm1.BitBtn5Click(Sender: TObject);
begin
  qImage.Close;
  qImage.ParamByName('prm_id').Value := Edit2.Text;
  qImage.Open;
  Image1.Picture.Assign(GetImageFromBlob(qImageany_kind_image));
  Image1.Picture.SaveToFile('C:\Users\Edijs\Desktop\test.' +
    GraphicExtension(TGraphicClass
    (GetClass(Image1.Picture.Graphic.ClassName))));
end;

这篇关于如何使用TBlobField中的TDBImage显示图片(jpg)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆