gpt4 book ai didi

delphi - Delphi中缩放 Canvas 区域

转载 作者:行者123 更新时间:2023-12-03 14:43:23 27 4
gpt4 key购买 nike

我制作了一些看起来像 Delphi 中的 Paint 的东西。我找到了如何制作缩放功能:

procedure SetCanvasZoomFactor(Canvas: TCanvas; AZoomFactor: Integer);
var
i: Integer;
begin
if AZoomFactor = 100 then
SetMapMode(Canvas.Handle, MM_TEXT)
else
begin
SetMapMode(Canvas.Handle, MM_ISOTROPIC);
SetWindowExtEx(Canvas.Handle, AZoomFactor, AZoomFactor, nil);
SetViewportExtEx(Canvas.Handle, 100, 100, nil);
end;
end;



procedure TMainForm.btnZoomPlusClick(Sender: TObject);
var
bitmap: TBitmap;
begin

bitmap := TBitmap.Create;
if(zoomVal < 1000) then
zoomVal:=zoomVal+zoomConst; //zoomVal = 100 by default; zoomConst = 150;
try
bitmap.Assign(MainForm.imgMain.Picture.Bitmap);
SetCanvasZoomFactor(bitmap.Canvas, zoomVal);
Canvas.Draw(MainForm.imgMain.Left,MainForm.imgMain.Top, bitmap);
finally
bitmap.Free
end;
end;

但是,问题是 - 它仅缩放图像的左上角区域。

示例缩放前: enter image description here缩放后: enter image description here

我希望能够在所有图片区域中移动,即使在缩放后也是如此。我怎样才能做到这一点?

最佳答案

您可以使用SetWorldTransform对于每个 DC。一个示例实现可能如下所示:

Procedure SetCanvasZoomAndRotation(ACanvas: TCanvas; Zoom: Double;
Angle: Double; CenterpointX, CenterpointY: Double);
var
form: tagXFORM;
rAngle: Double;
begin
rAngle := DegToRad(Angle);
SetGraphicsMode(ACanvas.Handle, GM_ADVANCED);
SetMapMode(ACanvas.Handle, MM_ANISOTROPIC);
form.eM11 := Zoom * Cos(rAngle);
form.eM12 := Zoom * Sin(rAngle);
form.eM21 := Zoom * (-Sin(rAngle));
form.eM22 := Zoom * Cos(rAngle);
form.eDx := CenterpointX;
form.eDy := CenterpointY;
SetWorldTransform(ACanvas.Handle, form);
end;

Procedure ResetCanvas(ACanvas: TCanvas);
begin
SetCanvasZoomAndRotation(ACanvas, 1, 0, 0, 0);
end;

您可以在绘画之前定义所需 Canvas 的缩放、XY 偏移和旋转。在您的情况下,您将选择“缩放”,绘制到 Canvas ,然后滚动/减小 X 和/或 Y 的值,然后再次使用相同的缩放调用该过程并绘制图形。

编辑展示如何使用该过程。这段代码

procedure TForm2.PaintBox1Paint(Sender: TObject);
var
i, w, h: Integer;
C: TCanvas;
begin
C := TPaintBox(Sender).Canvas;
w := TPaintBox(Sender).Width;
h := TPaintBox(Sender).Height;
for i := 0 to 9 do
begin
SetCanvasZoomAndRotation(C, 1 + i / 5, i * 36, w div 2, h div 2);
C.Draw(0, 0, Image1.Picture.Graphic);
C.Brush.Style := bsClear;
C.TextOut(50, 0, Format('Hi this is an example %d', [i]));
end;
end;

用于显示以下结果: enter image description here

作为对您的评论的回应,如何将其与轨迹栏一起使用,您实现了类似的东西

procedure TForm2.FormCreate(Sender: TObject);
begin
DoubleBuffered := true;
end;

procedure TForm2.PaintBox1Paint(Sender: TObject);
var // a Paintbox aligned alClient
C:TCanvas;
begin
TrackBarHorz.Max := Round(Image1.Picture.Graphic.Width * SpinEditZoomInPercent.Value / 100 - TPaintBox(Sender).Width);
TrackBarVert.Max := Round(Image1.Picture.Graphic.Height * SpinEditZoomInPercent.Value / 100 - TPaintBox(Sender).Height);
C := TPaintBox(Sender).Canvas;
SetCanvasZoomAndRotation(c , SpinEditZoomInPercent.Value / 100, 0
, - TrackBarHorz.Position
, - TrackBarVert.Position);
C.Draw(0,0,Image1.Picture.Graphic);
end;

procedure TForm2.SpinEditZoomInPercentChange(Sender: TObject);
begin
PaintBox1.Invalidate;
end;

procedure TForm2.BothTrackbarsEvent(Sender: TObject);
begin
PaintBox1.Invalidate;
end;

关于delphi - Delphi中缩放 Canvas 区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19469125/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com