使用delphi缩放图像

发布于 2024-09-05 18:27:52 字数 415 浏览 5 评论 0原文

我正在与德尔福合作。我有 TImage,我为其分配了一个位图。

imgmain.Picture.Bitmap := bmpMain;
imgmain.Picture.Bitmap.PixelFormat := pf24bit;

imgmain 是 TImage 的对象,bmpMain 是 TBitmap 的对象

我想缩放图像。我的表单上有一个轨迹栏,当我单击轨迹栏时,图像应该会缩放。我该怎么办?
谢谢。

编辑:
我在此处找到了一些解决方案,它有效,但它削弱了我的形象。

I am working with delphi. I have TImage, to which I assign a bitmap.

imgmain.Picture.Bitmap := bmpMain;
imgmain.Picture.Bitmap.PixelFormat := pf24bit;

imgmain is object of TImage and bmpMain is object of TBitmap

I want to zoom my image. I have one trackbar on my form and as I click on trackbar the image should get zoom. What should I do?
Thank You.

Edit :
I found some solution at here It works but it cut my image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骑趴 2024-09-12 18:27:52

您引用的代码设置了从一个坐标空间到另一个坐标空间的转换,我没有注意到任何会在那里剪切/裁剪您的图像的东西。然而,我宁愿使用易于理解的线性缩放,而不是成反比的缩放系数。另外,我认为没有理由根据比例因子切换地图模式,我会像这样修改 SetCanvasZoomFactor

procedure SetCanvasZoomPercent(Canvas: TCanvas; AZoomPercent: Integer);
begin
  SetMapMode(Canvas.Handle, MM_ISOTROPIC);
  SetWindowExtEx(Canvas.Handle, 100, 100, nil);
  SetViewportExtEx(Canvas.Handle, AZoomPercent, AZoomPercent, nil);
end;

一个简化的(无错误检查)工作示例,其中将位图加载到 TImage,通过 TrackBar 进行缩放,如下所示。请注意,上述函数内联在 TrackBar 的 OnChange 事件中。

type
  TForm1 = class(TForm)
    imgmain: TImage;
    TrackBar1: TTrackBar;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    bmpmain: TBitmap;
  [..]

[...]
procedure TForm1.FormCreate(Sender: TObject);
begin
  bmpmain := TBitmap.Create;
  bmpmain.LoadFromFile(ExtractFilePath('samplebitmap.bmp');
  bmpmain.PixelFormat := pf32bit; // No significance, just seems faster here than pf24bit

  TrackBar1.Min := 10;
  TrackBar1.Max := 200;
  TrackBar1.Frequency := 10;
  TrackBar1.PageSize := 10;
  TrackBar1.Position := 100; // Fires OnChange
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 bmpmain.Free;
end;

procedure TForm1.TrackBar1Change(Sender: TObject);
var
  Zoom, x, y: Integer;
begin
  Zoom := TrackBar1.Position;
  if not (Visible or (Zoom = 100)) or (Zoom = 0) then
    Exit;

  SetMapMode(imgmain.Canvas.Handle, MM_ISOTROPIC);
  SetWindowExtEx(imgmain.Canvas.Handle, 100, 100, nil);
  SetViewportExtEx(imgmain.Canvas.Handle, Zoom, Zoom, nil);
  x := imgmain.Width * 50 div Zoom - bmpmain.Width div 2;
  y := imgmain.Height * 50 div Zoom - bmpmain.Height div 2;
  imgmain.Canvas.Draw(x, y, bmpmain);
  if (x > 0) or (y > 0) then begin
    imgmain.Canvas.Brush.Color := clWhite;
    ExcludeClipRect(imgmain.Canvas.Handle, x, y, x + bmpmain.Width, y + bmpmain.Height);
    imgmain.Canvas.FillRect(imgmain.Canvas.ClipRect);
  end;

  Label1.Caption := 'Zoom: ' + IntToStr(TrackBar1.Position) + '%';
end;


edit: same code with a TImage in a ScrollBox;

type
  TForm1 = class(TForm)
    TrackBar1: TTrackBar;
    Label1: TLabel;
    ScrollBox1: TScrollBox;
    imgmain: TImage;
    procedure FormCreate(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    bmpmain: TBitmap;
  [...]
[...]

const
  FULLSCALE = 100;

procedure TForm1.FormCreate(Sender: TObject);
begin
  imgmain.Left := 0;
  imgmain.Top := 0;

  bmpmain := TBitmap.Create;
  bmpmain.LoadFromFile(ExtractFilePath(Application.ExeName) + '610x.bmp');
  bmpmain.PixelFormat := pf32bit;

  TrackBar1.Min := FULLSCALE div 10;   // %10
  TrackBar1.Max := FULLSCALE * 2;      // %200
  TrackBar1.PageSize := (TrackBar1.Max - TrackBar1.Min) div 19;
  TrackBar1.Frequency := TrackBar1.PageSize;
  TrackBar1.Position := FULLSCALE;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  bmpmain.Free;
end;

procedure TForm1.TrackBar1Change(Sender: TObject);
var
  Zoom: Integer;
begin
  Zoom := TrackBar1.Position;
  if not (Visible or (Zoom = FULLSCALE)) or (Zoom = 0) then
    Exit;

  SetMapMode(imgmain.Canvas.Handle, MM_ISOTROPIC);
  SetWindowExtEx(imgmain.Canvas.Handle, FULLSCALE, FULLSCALE, nil);
  SetViewportExtEx(imgmain.Canvas.Handle, Zoom, Zoom, nil);

  imgmain.Width := Round(bmpmain.Width * Zoom / FULLSCALE);
  imgmain.Height := Round(bmpmain.Height * Zoom / FULLSCALE);
  if Assigned(imgmain.Picture.Graphic) then begin
    imgmain.Picture.Graphic.Width := imgmain.Width;
    imgmain.Picture.Graphic.Height := imgmain.Height;
  end;
  imgmain.Canvas.Draw(0, 0, bmpmain);

  Label1.Caption := 'Zoom: ' +
      IntToStr(Round(TrackBar1.Position / FULLSCALE * 100)) + '%';
end;

The code you refer to sets up a transformation from one coordinate space to another, I didn't notice anything that would cut/crop your image there. However, instead of having an inversely proportional zoom factor I'd rather have, easy to understand, linear scaling. Also, I see no reason switching map modes depending on the scaling factor, I would modify the SetCanvasZoomFactor like this;

procedure SetCanvasZoomPercent(Canvas: TCanvas; AZoomPercent: Integer);
begin
  SetMapMode(Canvas.Handle, MM_ISOTROPIC);
  SetWindowExtEx(Canvas.Handle, 100, 100, nil);
  SetViewportExtEx(Canvas.Handle, AZoomPercent, AZoomPercent, nil);
end;

A simplified (no error checking) working example with a bitmap loaded to a TImage, scaled via a TrackBar could be like the below. Note that the above function is inlined in the TrackBar's OnChange event.

type
  TForm1 = class(TForm)
    imgmain: TImage;
    TrackBar1: TTrackBar;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    bmpmain: TBitmap;
  [..]

[...]
procedure TForm1.FormCreate(Sender: TObject);
begin
  bmpmain := TBitmap.Create;
  bmpmain.LoadFromFile(ExtractFilePath('samplebitmap.bmp');
  bmpmain.PixelFormat := pf32bit; // No significance, just seems faster here than pf24bit

  TrackBar1.Min := 10;
  TrackBar1.Max := 200;
  TrackBar1.Frequency := 10;
  TrackBar1.PageSize := 10;
  TrackBar1.Position := 100; // Fires OnChange
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 bmpmain.Free;
end;

procedure TForm1.TrackBar1Change(Sender: TObject);
var
  Zoom, x, y: Integer;
begin
  Zoom := TrackBar1.Position;
  if not (Visible or (Zoom = 100)) or (Zoom = 0) then
    Exit;

  SetMapMode(imgmain.Canvas.Handle, MM_ISOTROPIC);
  SetWindowExtEx(imgmain.Canvas.Handle, 100, 100, nil);
  SetViewportExtEx(imgmain.Canvas.Handle, Zoom, Zoom, nil);
  x := imgmain.Width * 50 div Zoom - bmpmain.Width div 2;
  y := imgmain.Height * 50 div Zoom - bmpmain.Height div 2;
  imgmain.Canvas.Draw(x, y, bmpmain);
  if (x > 0) or (y > 0) then begin
    imgmain.Canvas.Brush.Color := clWhite;
    ExcludeClipRect(imgmain.Canvas.Handle, x, y, x + bmpmain.Width, y + bmpmain.Height);
    imgmain.Canvas.FillRect(imgmain.Canvas.ClipRect);
  end;

  Label1.Caption := 'Zoom: ' + IntToStr(TrackBar1.Position) + '%';
end;


edit: same code with a TImage in a ScrollBox;

type
  TForm1 = class(TForm)
    TrackBar1: TTrackBar;
    Label1: TLabel;
    ScrollBox1: TScrollBox;
    imgmain: TImage;
    procedure FormCreate(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    bmpmain: TBitmap;
  [...]
[...]

const
  FULLSCALE = 100;

procedure TForm1.FormCreate(Sender: TObject);
begin
  imgmain.Left := 0;
  imgmain.Top := 0;

  bmpmain := TBitmap.Create;
  bmpmain.LoadFromFile(ExtractFilePath(Application.ExeName) + '610x.bmp');
  bmpmain.PixelFormat := pf32bit;

  TrackBar1.Min := FULLSCALE div 10;   // %10
  TrackBar1.Max := FULLSCALE * 2;      // %200
  TrackBar1.PageSize := (TrackBar1.Max - TrackBar1.Min) div 19;
  TrackBar1.Frequency := TrackBar1.PageSize;
  TrackBar1.Position := FULLSCALE;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  bmpmain.Free;
end;

procedure TForm1.TrackBar1Change(Sender: TObject);
var
  Zoom: Integer;
begin
  Zoom := TrackBar1.Position;
  if not (Visible or (Zoom = FULLSCALE)) or (Zoom = 0) then
    Exit;

  SetMapMode(imgmain.Canvas.Handle, MM_ISOTROPIC);
  SetWindowExtEx(imgmain.Canvas.Handle, FULLSCALE, FULLSCALE, nil);
  SetViewportExtEx(imgmain.Canvas.Handle, Zoom, Zoom, nil);

  imgmain.Width := Round(bmpmain.Width * Zoom / FULLSCALE);
  imgmain.Height := Round(bmpmain.Height * Zoom / FULLSCALE);
  if Assigned(imgmain.Picture.Graphic) then begin
    imgmain.Picture.Graphic.Width := imgmain.Width;
    imgmain.Picture.Graphic.Height := imgmain.Height;
  end;
  imgmain.Canvas.Draw(0, 0, bmpmain);

  Label1.Caption := 'Zoom: ' +
      IntToStr(Round(TrackBar1.Position / FULLSCALE * 100)) + '%';
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文