Delphi - 如何“就地"裁剪位图? [英] Delphi - how do I crop a bitmap "in place"?

查看:36
本文介绍了Delphi - 如何“就地"裁剪位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I have a TBitmap and I want to obtain a cropped image from this bitmap, can I perform the cropping operation "in place"? e.g. if I have a bitmap that is 800x600, how can I reduce (crop) it so that it contains the 600x400 image at the centre, i.e. the resulting TBitmap is 600x400, and consists of the rectangle bounded by (100, 100) and (700, 500) in the original image?

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

Do I need to go via another bitmap or can this operation be done within the original bitmap?

推荐答案

您可以使用 BitBlt函数

You can use the BitBlt function

试试这个代码.

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;

如果你想使用相同的位图,试试这个版本的功能

If you want use the same bitmap, try this version of the function

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 - 如何“就地"裁剪位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆