Question

I am creating a project in which I want to compress image so that it can be uploaded easily on windows azure and later can be retrieved easily from windows azure to my application.So can you please help me with how can I do that. I am using BitmapImage right now . Follwoing is the code which I am using to upload image to azure

void photoChooserTask_Completed(object sender, PhotoResult e) {

        if (e.TaskResult == TaskResult.OK)
        {

            BitmapImage bitmap = new BitmapImage();
            bitmap.SetSource(e.ChosenPhoto);
            WriteableBitmap wb = new WriteableBitmap(bitmap);                
            using (MemoryStream stream = new MemoryStream())
            {


                wb.SaveJpeg(stream, wb.PixelWidth, wb.PixelHeight, 0, 0);
                AzureStorage storage = new AzureStorage();
                storage.Account = **azure account**;
                storage.BlobEndPoint = **azure end point**;
                storage.Key = **azure key**;

                string fileName = uid;

                bool error = false;
                if (!error)
                {

                        storage.PutBlob("workerimages", fileName, imageBytes, error);

                }
                else
                {
                    MessageBox.Show("Error uploading the new image.");
                }

            }

        }
    }
Was it helpful?

Solution

Be care using the WriteableBitmap as you may run out of memory if resizing a lot of images. If you only have a few, then pass the size you want saved to the SaveJpeg method. Also make sure you use a value higher than 0 for the quality (last param of SaveJpeg)

var width = wb.PixelWidth/4;
var height = wb.PixelHeight/4;

using (MemoryStream stream = new MemoryStream())
{
    wb.SaveJpeg(stream, width, height, 0, 100);
    ...
    ...
}

You can also use the JpegRenderer from the Nokia Imaging SDK to resize an image.

var width = wb.PixelWidth/4;
var height = wb.PixelHeight/4;
using (var imageProvider = new StreamImageSource(e.ChosenPhoto))
{
    IFilterEffect effect = new FilterEffect(imageProvider);

    // Get the resize dimensions
    Windows.Foundation.Size desiredSize = new Windows.Foundation.Size(width, height);

    using (var renderer = new JpegRenderer(effect))
    {
        renderer.OutputOption = OutputOption.PreserveAspectRatio;

        // set the new size of the image
        renderer.Size = desiredSize;

        IBuffer buffer = await renderer.RenderAsync();
        return buffer;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top