Delphi - 如何裁剪位置“到位”? [英] Delphi - how do I crop a bitmap "in place"?

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

问题描述

如果我有一个TBitmap,并且我想从这个位图获得一个裁剪的图像,我可以执行裁剪操作到位吗?例如如果我有一个800x600的位图,我如何减少(裁剪),以便它包含中心的600x400图像,即生成的TBitmap是600x400,包括由(100,100)和(700 ,500)在原始图像?



我需要通过另一个位图,或者可以在原始位图内完成此操作?

解决方案

您可以使用 BitBlt 功能



尝试此代码。 C> B





$
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);
结束

您可以这样使用

  Var 
Bmp:TBitmap;
begin
Bmp:= TBitmap.Create;
try
CropBitmap(Image1.Picture.Bitmap,Bmp,10,0,150,150);
//使用裁剪后的图片执行某些操作
//Bmp.SaveToFile('Foo.bmp');
finally
Bmp.Free;
结束
结束

如果要使用相同的位图,请尝试此版本的功能

  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;
结束

以这种方式使用

  Var 
Bmp:TBitmap;
begin
Bmp:= Image1.Picture.Bitmap;
CropBitmap(Bmp,10,0,150,150);
//使用Bmp
进行某些操作Image1.Picture.Assign(Bmp);
结束


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?

解决方案

You can use the BitBlt function

try this code.

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;

and you can use in this way

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;

And use in this way

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天全站免登陆