使用delphi缩放图像

时间:2010-06-12 09:36:48

标签: delphi timage

我正在使用delphi。我有TImage,我为其分配了一个位图。

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

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

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

编辑:
我在here找到了一些解决方案。它有效但却削减了我的形象。

1 个答案:

答案 0 :(得分:9)

您所引用的代码设置了从一个坐标空间到另一个坐标空间的转换,我没有注意到会在那里剪切/裁剪图像的任何内容。然而,我不是具有反比例缩放因子,而是易于理解线性缩放。此外,我认为没有理由根据比例因子切换地图模式,我会像这样修改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;

通过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;

<小时/> 编辑:与ScrollBox中的TImage相同的代码;

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;