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 that 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 to 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 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;