Crop color borders on a Windows phone

enter image description here

Above is the image I'm using. What I'm trying to achieve is to remove the red part of the border from the image. How can I achieve this programmatically on a Windows phone? I found the WriteableBitmapExtensions.Crop() method, but I'm confused with the arguments (how can I find the position of the image x, y, as well as the size and width?)

Another problem I encountered: I get images with different border sizes, so I can’t hard set the x or y values.
Can someone suggest a solution or help me solve the problem?

+8
c # image windows-phone-7 windows-phone-8 writablebitmap
source share
1 answer

This is not such a trivial thing, and you did not share any code with us, so I can give you some suggestions. Each WriteableBitmap has a width and a height. You must have access to it through

 wb.PixelWidth; wb.PixelHeight; 

where wb is your WriteableBitmap (image)

Having said that, it is trivial to trim WriteableBitmap using the WriteableBitmapEx library

 var croppedBmp = wb.Crop(10, 10, 300, 220); 

If your wb was 320x240 and the border was 10 wide, then the above Crop call will do the trick - you take the inner rectangle, starting at point (10,10) and ending at (310, 230)

Now to your second question - not knowing the width of the border. This will help if you know that

  • The border has the same thickness on all sides of the image.
  • Border is always in one color.

Assuming this is true, you might think of a simple algorithm (which may not always be correct, but you can test and tweak it), which will require a few random points, for example

(0, randNumber <wb.PixelHeight), (randNumber <wb.PixelWidth, 0), (wb.PixelWidth, randNumber <wb.PixelHeight), (randNumber <wb.PixelWidth, wb.PixelHeight)

and then go to the inside of the image if the neighboring pixel has the same color as the starting pixel. The more points you make, the more chances there are for the right decision. The obvious problem is that it can happen that something in the picture is the same color as the border (exactly the same), which will make it look as if the border was bigger than it really is. That is why you should take more points.

If you provided any code, I would be glad to expand the answer.

+3
source share

All Articles