Cedric Reichenbach
Cedric Reichenbach

Reputation: 9319

Crop a given image using PhotoChooserTask

In my Windows Phone app, users can choose photos using the PhotoChooserTask and they're cropped to required dimensions by specifying photoChooserTask.PixelWidth and photoChooserTask.PixelHeight.

However, users can also get to my app through an image's edit button (using the Photos_Extra_Image_Editor extension). The problem is that those images can have arbitrary dimensions, so I'd like to use WP's built-in cropping mechanism here as well. Is it possible to configure the PhotoChooserTask to use one specific image and skip the choosing part? Or is there a task specifically for cropping?

Upvotes: 0

Views: 64

Answers (1)

robwirving
robwirving

Reputation: 1798

The Nokia Imaging SDK has some built in controls for photo cropping.

Here is an example using the CropFilter class:

async void CaptureTask_Completed(object sender, PhotoResult e)
{
    // Create a source to read the image from PhotoResult stream
    using (var source = new StreamImageSource(e.ChosenPhoto))
    {
        // Create effect collection with the source stream
        using (var filters = new FilterEffect(source))
        {
            // Initialize the filter 
            var sampleFilter = new CropFilter(new Windows.Foundation.Rect(0, 0, 500, 500));

            // Add the filter to the FilterEffect collection
            filters.Filters = new IFilter[] { sampleFilter };

            // Create a target where the filtered image will be rendered to
            var target = new WriteableBitmap((int)ImageControl.ActualWidth, (int)ImageControl.ActualHeight);

            // Create a new renderer which outputs WriteableBitmaps
            using (var renderer = new WriteableBitmapRenderer(filters, target))
            {
                // Render the image with the filter(s)
                await renderer.RenderAsync();

                // Set the output image to Image control as a source
                ImageControl.Source = target;
            }
        }
    }
}

Upvotes: 1

Related Questions