【问题标题】:Resizing an Image using WriteableBitmap is not working使用 WriteableBitmap 调整图像大小不起作用
【发布时间】:2013-09-24 00:27:51
【问题描述】:

我正在使用 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 为原始大小。有什么想法吗?

【问题讨论】:

  • 您没有调整 WriteableBitmap 的大小,而是将调整大小的副本保存到流中。保存后尝试使用 SetSource 方法或 LoadJpeg 方法。
  • 我有点困惑。由于我的CapturedPicture 类需要文件名和图像流作为参数,我认为将调整大小的 WriteableBitmap 副本保存到流中并以这种方式使用它是正确的吗?我将如何调整 WriteableBitmap 的大小并将流用于我的 CapturedPicture 类?我无法弄清楚如何最好地实施您的建议。
  • 在引用msdn.microsoft.com/EN-US/library/windowsphone/develop/… 时,我使用SaveJpeg 对图像保存在应用程序中时所需的jpeg 流进行编码。我在我的应用程序的其他地方使用了这种方法,效果很好,但在这种情况下,我必须先调整图像的大小,这就是我卡住的地方。

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


【解决方案1】:

尝试使用 WriteableBitmapExWinPhone.dll

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 2014-05-13
    • 2014-08-29
    • 2014-05-12
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    相关资源
    最近更新 更多