delphi 处理缩放图像
2021-04-30 06:27
标签:eve ice ide pen bit 压缩 font phi ble procedure TTMEImageDeviceIdentifyFrom.DrawText(AImage : TImage; AFile: string); iX := 6; iY := I * iHeight; delphi 处理缩放图像 标签:eve ice ide pen bit 压缩 font phi ble 原文地址:http://www.cnblogs.com/zhangzhifeng/p/7810980.html
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;
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;
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;