0

I need to crop an image specifying coordinates that may exceed the image's bounds. If the coordinates are off, appropriate padding is applied.

Normally:

+===============+
| Source Bitmap |
|   +-------+   |
|   + Crop  +   |
|   +-------+   |
|               |
+===============+

...which works perfectly well with WriteableBitmapEx's Crop() extension. But in my case:

+-----------+
+ Crop      +
+           +
+ +===============+
+ | Source Bitmap |
+ |               |
+ +===============+
+           +
+-----------+

In this case, the bounds exceed the top, left, and bottom. The resulting bitmap need to be:

+-----------+
+           +
+           +
+ +=========+
+ |         + 
+ |         +
+ +=========+
+           +
+-----------+

What's the best (and fastest) way to accomplish this?

Erwin Alva
  • 403
  • 4
  • 17

1 Answers1

1

The easiest would be to create a new WB with the dimension of the final result, then use the Blit() method to copy the region of the source to your new destination bitmap's region.

Nice ASCII art job btw. :)

Rene Schulte
  • 2,962
  • 1
  • 19
  • 26
  • That's exactly what I was expecting. For some reason, blitting causes an AccessViolationException during my tests, but that is most likely about something else. Thanks! :-) – Erwin Alva Mar 20 '13 at 18:49
  • That's most likely because the coordinates you provide are out of the bounds of the WB. – Rene Schulte Mar 20 '13 at 19:06
  • Which is exactly the point (it has to be padded). It seems to be an issue with WriteableBitmapEx because the Clone() method generates the same exception (no bounds are specified). Switching over to WinRTXAMLToolkit's Copy() does not create the same issue. I actually have to use Copy() *then* Blit(). – Erwin Alva Mar 20 '13 at 22:16
  • Please provide a repro solution where I can see the behavior and upload it somewhere. Would love to help you but can't repro it here. – Rene Schulte Mar 21 '13 at 08:01
  • That's OK. You already answered my question. I just mentioned an issue that caused me to question if the approach I was taking was wrong. – Erwin Alva Mar 21 '13 at 16:54