使用WriteableBitmap调整图像大小不起作用

时间:2013-09-24 00:27:51

标签: c# image windows-phone-7 windows-phone-8 writeablebitmap

我正在使用Windows Phone CameraCaptureTask收集图像,然后调整其大小以便在我的应用程序中更好地使用。出于某种原因,无论我尝试过什么,都无法让图像调整大小。

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;
}

无论我做什么,将WriteableBitmap设置为e.ChosenPhotobmi都会强制生成的wb为原始尺寸。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

尝试使用WriteableBitmapExWinPhone.dll

wb = wb.Resize(Convert.ToInt32(pass value of width here), Convert.ToInt32(pass value of height here), WriteableBitmapExtensions.Interpolation.Bilinear);