views:

367

answers:

1

I've found a solution that looks quite elegant but i don't understand its behavior. If i apply the cropping algorithm before adding my image to Canvas.Children collection it does not work. Why? Ideally i need a function like Image Crop(Image source, int X, int Y, int width, int heigh) which returns a new cropped Image instance and does not require source/cropped images to be placed to Canvas. Is it possible?

+3  A: 

I would guess that the problem you are running into is that when you are calling Render on the WriteableBitmap, the image source has not downloaded yet, so the Image renders nothing into the WriteableBitmap.

To workaround this, you will need to wait for the image source to download; you can handle the Image.ImageOpened event to get notified when this happens.

KeithMahoney
Thanks! I've set Build Action for my image to 'Content' and retrieved it using Application.GetResourceStream(). All works fine now!
UserControl
I found the same issue when I wrote the blog post. In the end I opted for the simpler sample code and just added it to the canvas. I have update the blog post with your question. Thanks
John