UserControl
UserControl

Reputation: 15159

Image cropping in Silverlight 3

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?

Upvotes: 1

Views: 925

Answers (2)

Cristian
Cristian

Reputation: 459

You can use WriteableBitmapEx available as NuGet package.

void Crop(Image source, string imageRelativePath, int x, int y, int width, int heigh)
{
    WriteableBitmap wb = BitmapFactory.New(1, 1)
                            .FromResource(imageRelativePath)
                            .Crop(x, y, width, heigh);
    wb.Invalidate();

    source.Source = wb;
}

Upvotes: 0

KeithMahoney
KeithMahoney

Reputation: 3343

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.

Upvotes: 4

Related Questions