How to Change Size of Image

做~自己de王妃 提交于 2019-12-08 10:50:51

问题


I am getting an image from the CameraCaptureTask, and I would like to be able to make the image much smaller before saving. The width and height are automatically set to the highest resolution, which is much more than what I need. I have been trying to get the image, change the dimensions, and then attempt to save, although I am getting errors.

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

EDIT: new implementation

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:


use the overloaded Bitmap constructor to create a re-sized image, the only thing you were missing was a cast back to the Image data type:

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

yourImage = resizeImage(yourImage, new Size(50,50));


来源:https://stackoverflow.com/questions/18951955/how-to-change-size-of-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!