delphi 处理缩放图像

Posted 张志峰的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了delphi 处理缩放图像相关的知识,希望对你有一定的参考价值。

procedure TTMEImageDeviceIdentifyFrom.DrawText(AImage : TImage; AFile: string);
var
I: Integer;
iWidth, iHeight: Integer;
oInfo: TTextInfoAry;
oRect, Rect: TRect;
bmp, dbmp: TBitmap;
iX, iY: Integer;
zoom: double;
NewW,NewH:Integer;
begin
bmp := TBitmap.Create;
dbmp := TBitmap.Create;
try
bmp.LoadFromFile(AFile);
if (bmp.Width > AImage.Width) or (bmp.Height > AImage.Height) then
begin
if bmp.Width > AImage.Width then
zoom := AImage.Width/bmp.Width
else
zoom := AImage.Height / bmp.Height;
NewH:=Round(bmp.Height * zoom);
NewW:=Round(bmp.Width * zoom);
with dbmp do
begin
Width:=NewW ;
Height:=NewH ;
dbmp.PixelFormat:=pfDevice;
SetStretchBltMode(dbmp.Canvas.Handle,COLORONCOLOR);//设置指位图拉伸模式
stretchblt(dbmp.Canvas.Handle,0,0,dbmp.Width,dbmp.Height,bmp.Canvas.Handle,0,0,bmp.Width,bmp.Height,srccopy); //从源矩形中复制一个位图到目标矩形并适当压缩
// Rect.TopLeft:=Point(0,0);
// Rect.BottomRight:=Point(NewW,NewH);
// Canvas.Rectangle(0,0,Width,Height);
// Canvas.StretchDraw(Rect,TGraphic(bmp));
end;
oRect.Left := Round((AImage.ClientRect.Width - dbmp.Width)/2);
oRect.Top := Round((AImage.ClientRect.Height - dbmp.Height)/2);
oRect.Width := dbmp.Width;
oRect.Height:= dbmp.Height;
AImage.Canvas.StretchDraw(oRect, dbmp);
end
else
begin
oRect.Left := Round((AImage.ClientRect.Width - bmp.Width)/2);
oRect.Top := Round((AImage.ClientRect.Height - bmp.Height)/2);
oRect.Width := bmp.Width;
oRect.Height:= bmp.Height;
AImage.Canvas.StretchDraw(oRect, bmp);
end;

iX := 6;
iY := 0;
AImage.Canvas.Font.Size := 14;
AImage.Canvas.Pen.Style := psClear;
AImage.Canvas.Brush.Style := bsClear;
ParseVectorDrawingText(FSelText, oInfo);
for I := Low(oInfo) to High(oInfo) do
begin
iWidth := AImage.Canvas.TextWidth(oInfo[I].Text);
iHeight := Round(AImage.Canvas.TextHeight(oInfo[I].Text));
oRect.Width := oRect.Width + iWidth;
oRect.Height := oRect.Height + iHeight;
if oRect.Left > iWidth then
oRect.Left := oRect.Left - iWidth;

iY := I * iHeight;
oInfo[I].InstPt.X := iX;
oInfo[I].InstPt.Y := iY ;
end;
for I := Low(oInfo) to High(oInfo) do
AImage.Canvas.TextOut(Round(oInfo[I].InstPt.X), Round(oInfo[I].InstPt.Y), oInfo[I].Text);
finally
bmp.Free;
dbmp.Free;
end;
end;

以上是关于delphi 处理缩放图像的主要内容,如果未能解决你的问题,请参考以下文章

Delphi 图像比例缩放问题 高手来啊

Delphi 图像比例缩放问题 高手来啊

在Delphi中实现图片的旋转、缩放

Delphi 缩放图像代码 - 支持PNG透明通道(利用了Windows的windowscodecs.dll)

图片处理-opencv-3.图像缩放、旋转、翻转、平移

Python图像处理丨图像缩放旋转翻转与图像平移