TDBGridEh如何显示图片

再TDBGridEh碰到要显示图片的问题,找到是需要再DrawColumnCell事件里面实现,定义一个TDBImageEh,通过图片路径把图片加载到TDBImageEh里面,只测试过本地图片,如果是局域网的,可以用共享文件实现图片显示,如果是网络图片就需要下载再加载进来了。
下面是加载本地地址图片

procedure TFPackingList.DetaiGridsDrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumnEh; State: TGridDrawState);
var
  jpeg: TDBImageEh;
  FileName: String;
begin
  if (Column.FieldName = 'IMG') and (Column.Field <> NIL) then
  begin
    jpeg := TDBImageEh.Create(self);
    jpeg.PicturePlacement := ipStretchEh;
    try
      if FileExists(ExtractFilePath(ParamStr(0)) + 'IMG/' +
        DetailData.FieldByName('ProNo').AsString + '1.jpg') then
        FileName := ExtractFilePath(ParamStr(0)) + 'IMG/' +
          DetailData.FieldByName('ProNo').AsString + '1.jpg'
      else
        FileName := ExtractFilePath(ParamStr(0)) + 'IMG/WJ.JPG';
      jpeg.Picture.LoadFromFile(FileName);
      DetaiGrids.Canvas.StretchDraw(Rect, jpeg.Picture.Graphic);
    finally
      jpeg.Free;
    end;
  end;
end;

这样表格就会显示图片了,但是图片非常模糊,如果需要显示清晰的图片就需要对图片进行处理了,通过canvas画一个高清的图片再加载进去,图片就会显示高清的图片了,新建一个Unit

unit GridsImg;

interface
uses Classes, SysUtils, Windows, Graphics, GDIPObj, ActiveX, Jpeg;

type
  TMemFileType = (FT_Error, FT_Unknown, FT_Bmp, FT_JPEG, FT_GIF, FT_PCX, FT_PNG,
    FT_PSD, FT_RAS, FT_SGI, FT_TIFF,
    FT_DOC, FT_XLS, FT_PPT);

    {
    创建缩略图
    Stream  ->图片文件内存数据
    imgWidth  ->图宽
    imgHeight ->图高
    ThumbnailPic ->生成的缩略图结果
    }
    function  MakeThumbnailImage (Stream: TMemoryStream;var imgWidth:Integer;var imgHeight:Integer; ThumbnailPic: TPicture;KeepScaling:Boolean):Boolean;overload;
    function  MakeThumbnailImage (FileName:String;var imgWidth:Integer;var imgHeight:Integer; ThumbnailPic: TPicture; KeepScaling:Boolean):Boolean;overload;

implementation

function CheckFileType(Stream: TStream): TMemFileType;
var
  Buffer: Word;

begin
  result  := FT_Error;
  if not Assigned(Stream) then exit;
  if stream.Size < 2  then exit;
  result  := FT_Unknown;
  Stream.Position := 0;
  Stream.ReadBuffer(Buffer, 2); //读取文件的前2个字节,放到Buffer里面
  case Buffer of
    $4D42 :Result := FT_Bmp;
    $D8FF :Result := FT_JPEG;
    $4947 :Result := FT_GIF;
    $050A :Result := FT_PCX;
    $5089 :Result := FT_PNG;
    $4238 :Result := FT_PSD;
    $A659 :Result := FT_RAS;
    $DA01 :Result := FT_SGI;
    $4949 :Result := FT_TIFF;
    $CFD0 :
    begin
      //Result  := CheckDocumentType (stream);
    end;
  end;
end;

procedure GDIDrawStream (Stream: TMemoryStream; Width, Height:Integer; DC: HDC);
var g :TGPGraphics;
    gImg :TGPImage;
    _stream: IStream;
begin
  _stream := TStreamAdapter.Create(Stream);
  gImg  := TGPImage.Create(_stream);
  g := TGPGraphics.Create(DC);
  try
    g.DrawImage(gImg, 0, 0, Width, Height);
  finally
    g.Free;
    gImg.Free;
    _stream := nil;
  end;
end;

function  MakeThumbnailImage (Stream: TMemoryStream;var imgWidth:Integer;var imgHeight:Integer;
  ThumbnailPic: TPicture; KeepScaling:Boolean):Boolean;
var ft:TMemFileType;
    bmp,_Bmp: TBitMap;
    jpg: TJpegImage;
    w, h: Integer;
    dblW, dblH, dbl: Double;
begin
  result  := False;

  if imgWidth<=0 then
    exit;
  if imgHeight<=0 then
    exit;

  ft  := CheckFileType (stream);

  //保持比例缩放,重新计算适合的图片宽度、高度
  if KeepScaling then
  begin
    case ft of

      FT_BMP:
      begin
        _Bmp  := TBitMap.Create;try
        stream.Position := 0;
        _Bmp.LoadFromStream(stream);
        w := _Bmp.Width;
        h := _Bmp.Height;
        dblW  := w/imgWidth;
        dblH  := h/imgHeight;
        if dblW>=dblH then
          dbl := dblW
        else
          dbl := dblH;
        if dbl = 0 then exit;

        imgWidth  := Round(w/dbl);
        imgHeight := Round(h/dbl);
        finally
          _Bmp.Free;
        end;
      end;

      FT_JPEG:
      begin
        jpg := TJpegImage.Create;try
        stream.Position := 0;
        jpg.LoadFromStream(stream);
        w := jpg.Width;
        h := jpg.Height;
        dblW  := w/imgWidth;
        dblH  := h/imgHeight;
        if dblW>=dblH then
          dbl := dblW
        else
          dbl := dblH;
        if dbl = 0 then exit;

        imgWidth  := Round(w/dbl);
        imgHeight := Round(h/dbl);
        finally
          jpg.Free;
        end;
      end;
    end;

  end;

  //生成缩略图
  case ft of
    FT_Bmp, FT_JPEG:
    begin

      bmp := TBitMap.Create;
      try
        bmp.Width := imgWidth;
        bmp.Height  := imgHeight;
        GDIDrawStream (stream, imgWidth, imgHeight, bmp.Canvas.Handle);
        ThumbnailPic.Assign(bmp);
        result  := True;
      finally
        FreeAndNil (bmp);
      end;
    end;

  end;



end;

function  MakeThumbnailImage (FileName:String;var imgWidth:Integer;var imgHeight:Integer;
  ThumbnailPic: TPicture; KeepScaling:Boolean):Boolean;
var stream: TMemoryStream;
begin
  stream  := TMemoryStream.Create;
  try
  stream.LoadFromFile(FileName);
  result  := MakeThumbnailImage (stream, imgWidth, imgHeight, ThumbnailPic, KeepScaling);
  finally
    stream.Free;
  end;
end;

end.

然后Uses这个单元,然后再把DrawColumnCell事件里面的代码调整一下

procedure TFPackingList.DetaiGridsDrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumnEh; State: TGridDrawState);
var
  jpeg: TDBImageEh;
  FileName: String;
  size: Integer;
begin
  if (Column.FieldName = 'IMG') and (Column.Field <> NIL) then
  begin
    jpeg := TDBImageEh.Create(self);
    jpeg.PicturePlacement := ipStretchEh;
    size := 100;
    try
      if FileExists(ExtractFilePath(ParamStr(0)) + 'IMG/' +
        DetailData.FieldByName('ProNo').AsString + '1.jpg') then
        FileName := ExtractFilePath(ParamStr(0)) + 'IMG/' +
          DetailData.FieldByName('ProNo').AsString + '1.jpg'
      else
        FileName := ExtractFilePath(ParamStr(0)) + 'IMG/WJ.JPG';
      MakeThumbnailImage(FileName, size, size, jpeg.Picture, True);
      DetaiGrids.Canvas.StretchDraw(Rect, jpeg.Picture.Graphic);
    finally
      jpeg.Free;
    end;
  end;
end;

显示效果对比
在这里插入图片描述
在这里插入图片描述

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值