Delphi - 如何裁剪位图 "in place"?

标签 delphi bitmap crop tbitmap

如果我有一个 TBitmap 并且我想从该位图中获取裁剪后的图像,我可以“就地”执行裁剪操作吗?例如如果我有一个 800x600 的位图,如何缩小(裁剪)它,使其包含中心的 600x400 图像,即生成的 TBitmap 为 600x400,并且由 (100, 100) 和 (700 , 500) 在原始图像中?

我需要通过另一个位图还是可以在原始位图中完成此操作?

最佳答案

您可以使用BitBlt功能

试试这个代码。

procedure CropBitmap(InBitmap, OutBitMap : TBitmap; X, Y, W, H :Integer);
begin
  OutBitMap.PixelFormat := InBitmap.PixelFormat;
  OutBitMap.Width  := W;
  OutBitMap.Height := H;
  BitBlt(OutBitMap.Canvas.Handle, 0, 0, W, H, InBitmap.Canvas.Handle, X, Y, SRCCOPY);
end;

你可以这样使用

Var
  Bmp : TBitmap;
begin
  Bmp:=TBitmap.Create;
  try
    CropBitmap(Image1.Picture.Bitmap, Bmp, 10,0, 150, 150);
    //do something with the cropped image
    //Bmp.SaveToFile('Foo.bmp');
  finally
   Bmp.Free;
  end;
end;

如果您想使用相同的位图,请尝试此版本的函数

procedure CropBitmap(InBitmap : TBitmap; X, Y, W, H :Integer);
begin
  BitBlt(InBitmap.Canvas.Handle, 0, 0, W, H, InBitmap.Canvas.Handle, X, Y, SRCCOPY);
  InBitmap.Width :=W;
  InBitmap.Height:=H;
end;

并以这种方式使用

Var
 Bmp : TBitmap;
begin
    Bmp:=Image1.Picture.Bitmap;
    CropBitmap(Bmp, 10,0, 150, 150);
    //do somehting with the Bmp
    Image1.Picture.Assign(Bmp);
end;

关于Delphi - 如何裁剪位图 "in place"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9183524/

相关文章:

delphi - 如何在 VCL 项目上获取 Fmx 项目中的 Windows 关闭事件作为 WM_QUERYENDSESSION 和 WM_ENDSESSION ?

delphi - 启用运行时主题时,FTouchManager 会导致 AV

android - 处理大位图

javascript - 裁剪以 html 上传的图像并为其添加水印

c++ - 如何裁剪 QComboBox 中的文本或如何获取其实际宽度?

delphi - "Package Cache"注册表项的用途是什么?

delphi - TIdHTTPServer 在我的 DUnit 测试中激活它时引发 EThread 错误 6

java - 梯度算法..它是如何工作的

C++ 从 8 位位图构建像素数据并访问 bmiColor 表信息

R:使用包 "rgdal"和 "raster"裁剪 GeoTiff 栅格