如何改变图像的大小

时间:2013-09-23 04:52:09

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

我从CameraCaptureTask获取图像,我希望能够在保存之前使图像更小。宽度和高度自动设置为最高分辨率,远远超过我的需要。我一直试图获取图像,更改尺寸,然后尝试保存,尽管我遇到了错误。

ORIGINAL

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;
            Stream stream;

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

            //Convert the wb to a stream for saving
            stream = new MemoryStream(wb.ToByteArray());

            //Need to replace the following line with the new image stream for saving?
            //var capturedPicture = new CapturedPicture(e.OriginalFileName, e.ChosenPhoto);   
            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;
    }

编辑:新实施

private void cameraTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
            BitmapImage bmi = new BitmapImage();
            bmi.SetSource(e.ChosenPhoto);

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

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

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

            stream.Seek(0, SeekOrigin.Begin);

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

1 个答案:

答案 0 :(得分:0)

使用重载的Bitmap构造函数来创建一个重新调整大小的图像,你唯一缺少的是回转到Image数据类型:

public static Image resizeImage(Image imgToResize, Size size)
{
   return (Image)(new Bitmap(imgToResize, size));
}

yourImage = resizeImage(yourImage, new Size(50,50));
相关问题