0

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.");
                }

            }

        }
    }
  • When you're doing wb.SaveJpeg, why not just use smaller width/height values? – Igor Ralic Mar 24 '14 at 16:25
  • Try to use Nokia Image SDK to work with images, it provides a lot off high quality tools to image manipulation – Viacheslav Smityukh Mar 24 '14 at 16:29
  • http://stackoverflow.com/questions/22163959/how-to-reduce-size-of-image-in-windows-phone/22164255#22164255 go with this link, WriteableBitmapExtensions has Resize method that is helpful for you – Jaihind Mar 25 '14 at 05:40

1 Answers1

1

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;
    }
}
Shawn Kendrot
  • 12,425
  • 1
  • 25
  • 41
  • wb.SaveJpeg(stream,width,height,0,100) is not working – Nikhil Sharma Mar 25 '14 at 04:50
  • see I have added two text block to check whether image size is getting reduced or not and for that I first of all for fist textblock I have set source before wb.saveJpeg method i.e TB1.Text = wb.PixelWidth and after that I am using wb.SaveJpeg(stream,width,height,0,100) and then in second textblock I am passing the same value i.e Tb2.Text = wb.PixelWidth then it should show the reduced width in TB2 but insted of that I am getting same value in both Tb – Nikhil Sharma Mar 26 '14 at 04:56
  • The SaveJpeg method saves a new image to the stream in the first parameter. It will not update the current WriteableBitmap. If you want to see the size, create a new Bitmap from the stream, or save the file and open it. – Shawn Kendrot Mar 26 '14 at 14:52