德尔福 - 我怎么裁剪位图"到位"? [英] Delphi - how do I crop a bitmap "in place"?
问题描述
如果我有一个TBitmap,我想从这个位图获得一个裁剪的图片,我可以就地执行裁切操作?例如如果我有一个位图,它是800×600,我怎么能减少(作物),以便它包含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
试试这个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;
和以这种方式使用
Var
Bmp : TBitmap;
begin
Bmp:=Image1.Picture.Bitmap;
CropBitmap(Bmp, 10,0, 150, 150);
//do somehting with the Bmp
Image1.Picture.Assign(Bmp);
end;
这篇关于德尔福 - 我怎么裁剪位图"到位"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!