0

I have a HBITMAP which was created from using the CopyPicture method of Excel Interop. For some reason, this puts a grey border on the top and left hand edges of the image even though these are not part of the spreadsheet. Could someone tell me an easy way of removing these borders from the image. The way I thought of is getting the bits in a bytearray, removing the first row and column from this byte array and then converting the array back to a bitmap. Is there an easier or better way? Like just a simple trim function?

Jonnster
  • 3,094
  • 5
  • 33
  • 45

1 Answers1

1

If you know the dimensions of the border, you can crop the bitmap by creating a copy with clone with a new bounding box applied:

int croppedWidth = x;
int croppedHeight = y;
Rectangle r = Rectangle(0, 0, croppedWidth, croppedHeight);
System::Drawing::Imaging::PixelFormat format = src->PixelFormat;
Bitmap^ result = src->Clone(r, format);

This crops the src bitmap with r and places a copy into result.

Sebastian
  • 8,046
  • 2
  • 34
  • 58
  • Except that code looks like C#/.NET. I'm using C++ as stated in the tags (no .NET) – Jonnster Apr 20 '12 at 13:15
  • Yep, you're right. I've made an edit, for reference: [Bitmap::Clone Method (Rectangle, PixelFormat)](http://msdn.microsoft.com/de-de/library/ms141944.aspx#Y0) – Sebastian Apr 20 '12 at 13:18
  • It still uses .NET though. And for the record, that won't crop the left and top of the bitmap anyway as it is always setting the left and top to 0,0). It will actually crop the right and bottom. – Jonnster Apr 20 '12 at 13:30
  • Sorry! There is [CreateCompatibleBitmap](http://msdn.microsoft.com/en-us/library/dd183488(v=vs.85).aspx) and [BitBlt](http://msdn.microsoft.com/en-us/library/dd183370(v=vs.85).aspx) which should be useful. And also: [Crop function BitBlt(…)](http://stackoverflow.com/questions/3671008/crop-function-bitblt). – Sebastian Apr 20 '12 at 13:37