给你一段代码参考
function GetRectByDpi(SRect:TRect;dpi:integer):TRect;
var
fWidth,fHeight:double;
begin
fWidth:=((SRect.Right-SRect.Left)*dpi)/MM_PER_INCH;//MM_PER_INCH = 25.4;
fHeight:=((SRect.Bottom-SRect.Top)*dpi)/MM_PER_INCH;
//由于该函数只在压缩图像时调用 因此将目标区域进一
result:=Rect(0,0,round(fWidth+0.5),round(fHeight+0.5));
end;
//将指定图片转换为指定dpi后另存为新文件
function SaveJPGAsDpi(SRect:TRect;sSource,sDest:string;nDestDPI:integer):boolean;
var
jpgSource,jpgDest:TJpegImage;
bmp:TBitmap;
ResultRect:TRect;
sPath:string;
begin
result:=false;
if not FileExists(sSource) then Exit;
ResultRect:=GetRectByDpi(SRect,nDestDPI);
jpgSource:=TJpegImage.Create;
jpgDest:=TJpegImage.Create;
bmp:=TBitmap.Create;
try
try
sPath:=GetFilePath(sDest);
if not DirectoryExists(sPath) then
if not ForceDirectories(sPath) then Exit;
jpgSource.LoadFromFile(sSource);
bmp.PixelFormat:=pf24bit;
bmp.Width:=ResultRect.Right-ResultRect.Left;
bmp.Height:=ResultRect.Bottom-ResultRect.Top;
bmp.Canvas.StretchDraw(ResultRect,jpgSource);
jpgDest.Assign(bmp);
jpgDest.SaveToFile(sDest);
except
exit;
end;
finally
jpgSource.Free;
jpgDest.Free;
bmp.Free;
end;
result:=true;
end;