SQL Delphi加载图像在sql数据库中另存为blob

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26103437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 02:43:06  来源:igfitidea点击:

Delphi load image save as blob in a sql database

sqlimagedelphiblob

提问by user2210837

I'm trying to load a Image control from a image blob saved previously in a sql database.I have testd so many ways and i can't make it work. The image blob is saved as:

我正在尝试从以前保存在 sql 数据库中的图像 blob 加载图像控件。我已经测试了很多方法,但无法使其工作。图像 blob 保存为:

qry.SQL.Text := 'update tbl set pic = :blobVal where id = :idVal';
qry.Parameters.ParamByName('blobVal').LoadFromFile('c:\sample.jpg', ftBlob);
qry.Parameters.ParamByName('idVal').Value := 1;

any suggestion?

有什么建议吗?

回答by bummi

There a a lot of treads here about loading images to as database, but I did not find one with update or insert parameters.

这里有很多关于将图像加载为数据库的步骤,但我没有找到带有更新或插入参数的步骤。

You might simply assign a graphic object to your parameter. If you want to store different graphic types you should add a column keeping the Information which kind of graphic should be stored (e.g. jpeg,bmp,png). to be able to create the needed TGraphic class descendant if you want to retrieve the picture from the database.

您可以简单地为您的参数分配一个图形对象。如果您想存储不同的图形类型,您应该添加一列,保留应存储哪种图形的信息(例如 jpeg、bmp、png)。如果您想从数据库中检索图片,则能够创建所需的 TGraphic 类后代。

uses jpeg, pngimage;

type
 TitTYPES=(itJPG,itPNG,itBMP);

procedure TDEMO.Button1Click(Sender: TObject);
var
 jp:TJpegimage;
 g:TGraphic;
begin

  jp:=TJpegimage.Create;
  try
    ads.Close;
    jp.LoadFromFile('C:\Bilder1\PIC.jpg');
    ads.SQL.Text := 'Insert into IMGBlob (ID,Blob,typ) Values (:ID,:BLOB,:typ)';
    ads.Parameters[0].Value := 1;
    ads.Parameters[1].Assign(jp);
    ads.Parameters[2].Value := itJPG;
    ads.ExecSQL;

    ads.SQL.Text := 'Select * from IMGBlob where ID=:ID';
    ads.Parameters[0].Value := 1;
    ads.Open;
    try
      case TitTYPES(ads.FieldByName('typ').AsInteger) of
           itJPG: g:=TJpegimage.Create;
           itPNG: g:=TPNGImage.Create;
           itBMP: g:=TBitmap.Create;
      end;
    g.Assign(ads.FieldByName('Blob'));
    Image1.Picture.Assign(g);
    finally
      g.Free;
    end;
  finally
    jp.Free;
  end;
end;

回答by Ken White

To load a BLOB field into an image, you need to use TDataSet.CreateBlobStream.

要将 BLOB 字段加载到图像中,您需要使用 TDataSet.CreateBlobStream。

var
  Stream: TStream;
  JPG: TJpegImage;
begin
  JPG := TJpegImage.Create;
  try
    Stream := Qry.CreateBlobStream(Qry.FieldByName('BLOBVAL'), bmRead);
    try
      JPG.LoadFromStream(Stream);
    finally
      BlobStream.Free;
    end;
  finally
    JPG.Free;
  end;
end;

To store the image back, you'll need to do the reverse:

要将图像存储回来,您需要执行相反的操作:

var
  Stream: TBlobStream;
  Jpg: TJpegImage;
begin
  Jpg := TJpegImage.Create;
  try
    Jpg.Assign(Image1.Picture.Graphic);
    // Assign other query parameters here
    Stream := Qry.CreateBlobStream(Qry.FieldByName('BLOBVAL'), bmWrite);
    try
      Jpg.SaveToStream(Stream);
      Qry.ExecSQL;
    finally
      Stream.Free;
    end;
  finally
    Jpg.Free;
  end;
end;

TDBImageis only designed to work with bitmaps (when the field is ftGraphic), so it won't work with JPEG images directly. The easiest thing to do is to load the blob as a JPEG, and assign it to a standard TImage.Picture.Graphicin an event handler for the dataset (such as it's AfterScrollevent).

TDBImage仅设计用于位图(当字段为 时ftGraphic),因此它不能直接用于 JPEG 图像。最简单的方法是将 blob 作为 JPEG 加载,并将其分配给TImage.Picture.Graphic数据集事件处理程序中的标准(例如它的AfterScroll事件)。

回答by Ased Zarif

save to db:

保存到数据库:

var
  ms:tmemorystream;
Begin  
  ms:=tmemorystream.create;
  ms.position:=0;
  image1.picture.bitmap.savetostream(ms);
  ms.position:=0;
  with yourfield as tblobfield do
    loadfromstream(ms);
  freeandnil(ms);
end; 

Load from db:

从数据库加载:

var
  ms:tmemorystream;
Begin  
  ms:=tmemorystream.create;
  ms.position:=0;
  with yourfield as tblobfield do
    savetostream(ms);
  ms.position:=0;
  image1.picture.bitmap.loadfromstream(ms);
  freeandnil(ms);
end; 

回答by user12640260

It does not work with all graphic types like PNG etc.

它不适用于所有图形类型,如 PNG 等。

This one does work on PNG as well:

这个也适用于 PNG:

var mBitmap : TGraphic;
var mStream : TStream;
var mClass  : TGraphicClass;
begin
      mStream := Query.CreateBlobStream(Query.FieldByName('yourBlobfield'), bmRead);
      mClass  := GetGraphicClassForFileExtension(mStream.ReadAnsiString);
      mBitmap := mClass.Create;
      mBitmap.LoadFromStream(mStream);
      Image4.Picture.Assign(mBitmap);
 end;