Jump to content

How to display Jpg in DbImage Field


rencarnacion

Recommended Posts

Zilav shared this

 

"You are writing for a web browser. Just drop an image file somewhere and link to it with <img src=...> tag, thats all. Browser knows about image formats way more than you.
That ShowImage procedure is the worst piece of code for web. So much mess for doing so little.
It's only usefulness is when app needs to work with both web and VCL the same way. If it is not your aim, don't do that.

Don't forget to add some random value at the end of url like "image.jpg?3554356756756" so the image won't be loaded from browser's cache the next time it is refreshed."

 


procedure TMainForm.SetImageUrl(BlobFld: TField; UniImage: TUniImage; fileName: String);
var
   Tmp,TmpFile:String;
begin
   Tmp:= 'Tmp_' +UnitName +IntToStr(BlobFld.DataSet.RecNo) +fileName;
   TmpFile:= UniServerModule.LocalCachePath +Tmp;
   if (not FileExists(PChar(TmpFile))) then
      TBlobField(BlobFld).SaveToFile(PChar(TmpFile));
   try
      UniImage.Url:= '';
      UniImage.Refresh;
      UniImage.Url:= UniServerModule.LocalCacheURL +Tmp;
   except
   end;
end;

 

Link to comment
Share on other sites

//读取数据库图片数据
procedure TuDBGridFrame.UniDBGrid1CellClick(Column: TUniDBGridColumn);// UniGUIAbstractClasses
var
  jpegimage: tjpegimage;
  bmpimage: TBitMap;
  BlobStream: TStream;
  Buffer: WORD;
begin
  with ADOTable1 do
  begin
    if (not fieldbyname('photo').IsNull) then // 字段无内容
    begin
      BlobStream := CreateBlobStream(fieldbyname('photo'), bmRead);
      BlobStream.Position := 0;
      BlobStream.ReadBuffer(Buffer, 2); // 读取文件前2个字节
      BlobStream.Position := 0;
      if Buffer = $4D42 then
// $4D42'BMP'$D8FF'JPEG'$4947 'GIF'$050A'PCX'$5089 'PNG'$4238'PSD'$A659'RAS'$DA01'SGI'$4949'TIFF'
      begin
        bmpimage := TBitMap.Create;
        try
          bmpimage.LoadFromStream(BlobStream);
          UniImage1.Picture.bitmap.Assign(bmpimage);
          UniImage1.Visible := true;
        finally
          bmpimage.Free;
          BlobStream.Free;
        end;
      end;
      if Buffer = $D8FF then // 如果前两个字节是以D8FF[JPG]
      begin
        jpegimage := tjpegimage.Create;
        try
          jpegimage.LoadFromStream(BlobStream);
          UniImage1.Picture.Assign(jpegimage);
          UniImage1.Visible := true;
        finally
          jpegimage.Free;
          BlobStream.Free;
        end;
      end;
    end
    else
      UniImage1.Visible := False;
  end;
end;

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...