0

I am using the Windows Phone CameraCaptureTask to gather an image, and then resize it for better use within my application. For some reason, no matter what I've tried I cannot get the image to resize.

MainPage.xaml.cs

private void cameraTask_Completed(object sender, PhotoResult e)
{
    if (e.TaskResult == TaskResult.OK)
    {
        BitmapImage bmi = new BitmapImage();
        bmi.SetSource(e.ChosenPhoto);
        //MessageBox.Show(bmi.PixelWidth.ToString() + "x" + bmi.PixelHeight.ToString());

        var gcd = GCD(bmi.PixelWidth, bmi.PixelHeight);
        var result = string.Format("{0}:{1}", bmi.PixelWidth / gcd, bmi.PixelHeight / gcd);

        WriteableBitmap wb = new WriteableBitmap(bmi);
        //WriteableBitmap wb;
        //Stream stream = new MemoryStream();
        var stream = new MemoryStream();


        switch (result)
        {
            case "3:4":
                //wb = new WriteableBitmap(480, 640);
                //wb.SetSource(e.ChosenPhoto);
                wb.SaveJpeg(stream, 480, 640, 0, 100);
                break;
            case "4:3":
                //wb = new WriteableBitmap(640, 480);
                //wb.SetSource(e.ChosenPhoto);
                wb.SaveJpeg(stream, 640, 480, 0, 100);
                break;
            case "9:16":
                //wb = new WriteableBitmap(448, 800);
                //wb.SetSource(e.ChosenPhoto);
                wb.SaveJpeg(stream, 448, 800, 0, 100);
                break;
            case "16:9":
                //wb = new WriteableBitmap(800, 448);
                //wb.SetSource(e.ChosenPhoto);
                wb.SaveJpeg(stream, 800, 448, 0, 100);
                break;
            default:
                wb = null;
                return;
        }

        stream.Seek(0, SeekOrigin.Begin);
        //stream.Dispose();

        MessageBox.Show(wb.PixelWidth.ToString() + " x " + wb.PixelHeight.ToString());

        var capturedPicture = new CapturedPicture(e.OriginalFileName, stream);
    }
}

public int GCD(int a, int b)
{
    while (a != 0 && b != 0)
    {
        if (a > b)
            a %= b;
        else
            b %= a;
    }
    if (a == 0)
        return b;
    else
        return a;
}

No matter what I do, setting the WriteableBitmap to either e.ChosenPhoto or bmi forces the resulting wb to be the original size. Any ideas?

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
Matthew
  • 3,976
  • 15
  • 66
  • 130
  • You did not resize the WriteableBitmap, you saved a resized copy of it to the stream. Try using the SetSource method or LoadJpeg method after saving it. – Shawn Kendrot Sep 24 '13 at 06:12
  • I'm a bit confused. Since my `CapturedPicture` class requires a file name and image stream as parameters, I thought saving a resized copy of the WriteableBitmap to the stream and using it in this way was correct? How would I resize the WriteableBitmap and use the stream for my CapturedPicture class? I am having trouble figuring out how to best implement your suggestion. – Matthew Sep 24 '13 at 06:49
  • In referencing http://msdn.microsoft.com/EN-US/library/windowsphone/develop/system.windows.media.imaging.extensions.savejpeg(v=vs.105).aspx I was using the `SaveJpeg` to encode a jpeg stream which is required for when the image is saved in the application. I'm using this method elsewhere in my application which works great, but in this case I must resize the images first which is where I am getting stuck. – Matthew Sep 24 '13 at 07:17

1 Answers1

3

try using WriteableBitmapExWinPhone.dll

wb = wb.Resize(Convert.ToInt32(pass value of width here), Convert.ToInt32(pass value of height here), WriteableBitmapExtensions.Interpolation.Bilinear);
Farhan Ghumra
  • 15,180
  • 6
  • 50
  • 115
Archana
  • 376
  • 2
  • 12