如何更改图片的大小 [英] How to Change Size of Image

查看:196
本文介绍了如何更改图片的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从CameraCaptureTask得到一个形象,我希望能够在保存之前,使图像更小。宽度和高度自动设置为最高分辨率,这比什么,我需要更多。我一直在试图获取图像,更改尺寸,然后尝试保存,但我得到的错误。



原始



MainPage.xaml.cs中

 私人无效cameraTask_Completed(对象发件人,PhotoResult E)
{
如果(e.TaskResult == TaskResult.OK)
{
的BitmapImage BMI =新的BitmapImage();
bmi.SetSource(e.ChosenPhoto);
//MessageBox.Show(bmi.PixelWidth.ToString()+×+ bmi.PixelHeight.ToString());

VAR GCD = GCD(bmi.PixelWidth,bmi.PixelHeight);
VAR的结果=的String.Format({0}:{1},bmi.PixelWidth / GCD,bmi.PixelHeight / GCD);

WriteableBitmap的WB;
涧流;

开关(结果)
{
案3:4:
WB =新的WriteableBitmap的(480640);
中断;
案4:3:
WB =新WriteableBitmap的(640,480);
中断;
案9:16:
WB =新的WriteableBitmap的(448,800);
中断;
案16:9:
WB =新的WriteableBitmap的(800,448);
中断;
默认:
WB = NULL;
的回报;
}
//设置白平衡原来的流?
wb.SetSource(e.ChosenPhoto);

//将WB到流保存
=流新的MemoryStream(wb.ToByteArray());

//需要更换新图像流以下行保存?
// VAR capturedPicture =新CapturedPicture(e.OriginalFileName,e.ChosenPhoto);
变种capturedPicture =新CapturedPicture(e.OriginalFileName,流);

}
}

公众诠释GCD(INT A,INT B)
{
,而(A = 0&安培;!&放;!b = 0)
{
如果(A> b)
A%= b;
,否则
B%= A;
}
如果(A == 0)
回复B;
,否则
返回;
}



编辑:新实施

 私人无效cameraTask_Completed(对象发件人,PhotoResult E)
{
如果(e.TaskResult == TaskResult.OK)
{
BitmapImage的BMI =新的BitmapImage();
bmi.SetSource(e.ChosenPhoto);

VAR GCD = GCD(bmi.PixelWidth,bmi.PixelHeight);
VAR的结果=的String.Format({0}:{1},bmi.PixelWidth / GCD,bmi.PixelHeight / GCD);

WriteableBitmap的WB =新的WriteableBitmap的(BMI);
流流=新的MemoryStream();

开关(结果)
{
案3:4:
wb.SaveJpeg(流,480,640,0,100);
中断;
案4:3:
wb.SaveJpeg(流,640,480,0,100);
中断;
案9:16:
wb.SaveJpeg(流,448,800,0,100);
中断;
案16:9:
wb.SaveJpeg(流,800,448,0,100);
中断;
默认:
WB = NULL;
的回报;
}

stream.Seek(0,SeekOrigin.Begin);

// VAR capturedPicture =新CapturedPicture(e.OriginalFileName,e.ChosenPhoto);
变种capturedPicture =新CapturedPicture(e.OriginalFileName,流);


解决方案

使用重载的位图构造函数创建一个重新大小形象,你唯一缺少的是一个投回的图像数据类型:

 公共静态图像resizeImage(图片imgToResize,尺寸大小)
{
回报(图)(新位图(imgToResize,大小));
}

yourImage = resizeImage(yourImage,新的尺寸(50,50));


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

解决方案

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

这篇关于如何更改图片的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆