CJC Delphi 技巧馆's Archiver

cjc Posted at 2018-9-20 10:35

Firemonkey JPG/Bitmap/PNG 格式转换

Firemonkey JPG/Bitmap/PNG 格式转换

uses FMX.Surfaces;

procedure TForm1.Button1Click(Sender: TObject);
var
  Surf: TBitmapSurface;
  Stream: TMemoryStream;
begin
  Surf := TBitmapSurface.Create;
  Surf.Assign(Image1.Bitmap);
  Stream := TMemoryStream.Create;
  try
    TBitmapCodecManager.SaveToStream(Stream, Surf, '.jpg');
    Stream.SaveToFile(ExpandFileName('~/Test.jpg'));
  finally
    Stream.Free;
    Surf.Free;
  end;
end;

FMX Bitmap与TBlobField相互读取的两个函数

1.将Bitmap以jpg格式写入TField.
procedure BitmapToField(aBitmap: TBitmap; aField: TField);
var
  MS: TMemoryStream;
  Surf: TBitmapSurface;
begin
  if aBitmap.Width=0 then Exit;
  
  MS := TMemoryStream.Create;
  Surf:=TBitmapSurface.Create;
  try
    Surf.Assign(aBitmap);
    TBitmapCodecManager.SaveToStream(MS,Surf,'.png');//这里换成.png,就变成png格式了。
    MS.Position := 0;
    TBlobField(aField).LoadFromStream(MS);
  finally
    MS.DisposeOf;
    Surf.DisposeOf;
  end;
end;
2.从字段读取Bitmap.
procedure FieldToBitmap(aField: TField; aBitmap: TBitmap);
var
  MS: TMemoryStream;
begin
  MS := TMemoryStream.Create;
  try
    TBlobField(aField).SaveToStream(MS);
    MS.Position := 0;
    if ms.size>0 then
    try
       aBitmap.LoadFromStream(MS);
    except
       ///
    end;
  finally
    MS.DisposeOf;
  end;
end;




procedure SaveImage(ABitmap:TBitmap;AStream:TStream)
var
   Surf:TBitmapSurface;
   AParams:TBitmapCodecSaveParams;
begin
  Surf:=TBitmapSurface.Create;
  try
     Surf.Assign(ABitmap);
     AParams.Quality:=60;//JPEG 画质,60% 在Photoshop中是中等画质
     if not TBitmapCodecManager.SaveToStream(AStream,Surf,'.jpg',@AParams) then
        raise EBitmapSavingFailed.Create('无法保存图片为JPG格式');
  finally
    Surf.DisposeOf;
  end;
end;

Page: [1]
CJC好玩背单词最新版本 - 是目前最好的背单词软件,游戏好玩,功能强大,词库众多,还有真人语音,免费下载,免费试用 !

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.