不知道此过程是否能解决问题:
procedure TFangwu.PicAutoSize(Instream, outstream: Tstream; Width,
height: integer; fillcolor: Tcolor);
var
jpg: TjpegImage;
bmp: TBitmap;
ratio: double;
arect: Trect;
Aheight, AheightOffset: integer;
Awidth, awidthOffset: integer;
begin
if width < 1 then
raise EXception.Create('invaid width');
if height < 1 then
raise Exception.Create('invaid height');
jpg := Tjpegimage.Create;
try
jpg.LoadFromStream(instream);
bmp := Tbitmap.Create;
try
ratio := jpg.Width / jpg.height;
if ratio > 1 then
begin
Aheight := round(width / ratio);
aheightoffset := (height - Aheight) div 2;
awidth := width;
awidthoffset := 0;
end
else
begin
awidth := round(height * ratio);
awidthoffset := (width - awidth) div 2;
aheight := height;
aheightoffset := 0;
end;
bmp.Width := width;
bmp.Height := height;
bmp.Canvas.Brush.Color := fillcolor;
bmp.Canvas.FillRect(rect(0, 0, width, height));
arect := rect(awidthoffset, aheightoffset, awidth + awidthoffset, aheight
+ aheightoffset);
bmp.Canvas.StretchDraw(arect, jpg);
jpg.Assign(bmp);
jpg.SaveToStream(outstream);
finally
bmp.Free;
end;
finally
jpg.Free;
end;
end;