图片格式大小处理

  • 函数类型: 自定义函数

  • 函数说明:

图片格式大小处理


  • 代码如下:



 function FormatMyImage(FileName: string; FImageWidth: Integer)
  : TMemoryStream; overload;
function FormatMyImage(FileName: string; myFormat: TWICImageFormat)
  : TMemoryStream; overload;
function FormatMyImage(iBitMap: TBitmap; FImageWidth: Integer)
  : TMemoryStream; overload;
function FormatMyImage(iBitMap: TBitmap; myFormat: TWICImageFormat)
  : TMemoryStream; overload;
function FormatMyImage(iBitMap: TBitmap; myFN: string;
  myFormat: TWICImageFormat): Boolean; overload;

function FormatMyImage(iStream: TMemoryStream; FImageWidth: Integer;
  myFormat: TWICImageFormat): TWICImage; overload;


function FormatMyImage(iBitMap: TBitmap; myFN: string;
  myFormat: TWICImageFormat): Boolean;
var
  oWic: TWICImage;
  ratio: Double;
begin
  Result := False;
  oWic := TWICImage.Create;
  oWic.Assign(iBitMap);
  oWic.ImageFormat := myFormat;
  oWic.SaveToFile(myFN);
  oWic.Free;
  Result := True;
end;

function FormatMyImage(iBitMap: TBitmap; myFormat: TWICImageFormat)
  : TMemoryStream;
var
  oWic: TWICImage;
  ratio: Double;
begin
  Result := TMemoryStream.Create;
  oWic := TWICImage.Create;
  oWic.Assign(iBitMap);
  oWic.ImageFormat := myFormat;
  oWic.SaveToStream(Result);
  oWic.Free;
end;

function FormatMyImage(FileName: string; FImageWidth: Integer): TMemoryStream;
// 保持比例只需要一个宽度
var
  oWic: TWICImage;
  bmp: TBitmap;
  ratio: Double;
begin
  Result := TMemoryStream.Create;
  bmp := TBitmap.Create;
  oWic := TWICImage.Create;
  oWic.LoadFromFile(FileName);
  ratio := oWic.Height / oWic.Width;
  bmp.Width := FImageWidth;
  bmp.Height := Round(FImageWidth * ratio);
  bmp.Canvas.StretchDraw(Rect(0, 0, bmp.Width, bmp.Height), oWic);
  oWic.Assign(bmp);
  oWic.ImageFormat := TWICImageFormat.wifJpeg;
  oWic.SaveToStream(Result);
  bmp.Free;
  oWic.Free;
end;

function FormatMyImage(iBitMap: TBitmap; FImageWidth: Integer): TMemoryStream;
var
  oWic: TWICImage;
  bmp: TBitmap;
  ratio: Double;
begin
  Result := TMemoryStream.Create;
  bmp := TBitmap.Create;
  oWic := TWICImage.Create;
  oWic.Assign(iBitMap);
  ratio := oWic.Height / oWic.Width;
  bmp.Width := FImageWidth;
  bmp.Height := Round(FImageWidth * ratio);
  bmp.Canvas.StretchDraw(Rect(0, 0, bmp.Width, bmp.Height), oWic);
  oWic.Assign(bmp);
  oWic.ImageFormat := TWICImageFormat.wifJpeg;
  oWic.SaveToStream(Result);
  bmp.Free;
  oWic.Free;
end;

function FormatMyImage(iStream: TMemoryStream; FImageWidth: Integer;
  myFormat: TWICImageFormat): TWICImage; overload;
var
  oWic: TWICImage;
  bmp: TBitmap;
  ratio: Double;
begin
  bmp := TBitmap.Create;
  oWic := TWICImage.Create;
  oWic.LoadFromStream(iStream);
  ratio := oWic.Height / oWic.Width;
  bmp.Width := FImageWidth;
  bmp.Height := Round(FImageWidth * ratio);
  bmp.Canvas.StretchDraw(Rect(0, 0, bmp.Width, bmp.Height), oWic);
  oWic.Assign(bmp);
  oWic.ImageFormat := myFormat;
  bmp.Free;
  Result := oWic;
end;

function FormatMyImage(FileName: string; myFormat: TWICImageFormat)
  : TMemoryStream;
var
  oWic: TWICImage;
  bmp: TBitmap;
  ratio: Double;
begin
  Result := TMemoryStream.Create;
  bmp := TBitmap.Create;
  bmp.LoadFromFile(FileName);
  oWic := TWICImage.Create;
  oWic.Assign(bmp);
  oWic.ImageFormat := myFormat;
  oWic.SaveToStream(Result);
  bmp.Free;
  oWic.Free;
end;



  • 用法示例:

无 



《学习大师原创文档,请勿转载,侵权必究》





联系电话:
020-00000000
联系电话:
020-00000000
联系电话:
020-12345678