C#简单的图像调整尺寸:文件大小不缩水 [英] C# Simple Image Resize : File Size Not Shrinking

查看:124
本文介绍了C#简单的图像调整尺寸:文件大小不缩水的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在问候下面的代码的问题。我有下面的代码通过目录成功运行,并设置图象到一个较小尺寸的resoultion。但是,文件大小不被改变。例如,与1.5MB的文件大小的2400x1800尺寸的图像将被缩小到800 * 600,但为800x600图象仍将是1.5MB文件大小。我想我可能要明确地压缩图片,但我不知道。任何想法?



 私人无效Form1_Load的(对象发件人,EventArgs五)
{
的String []文件= NULL;
诠释计数= 0;
=文件System.IO.Directory.GetFiles(@C:\Users\..\..\ChristmasPicsResized);
的foreach(在文件中字符串的文件)
{
System.Drawing.Bitmap BMP = System.Drawing.Bipmap.FromFile(文件);

ResizeBitmap(BMP,807,605).Save(
@C:\users\..\..\TempPicHold\Pic+ count.ToString( )+.JPG);
计数++;
}
}
公共位图ResizeBitmap(位图B,INT nWidth,nHeight参数INT)
{
位图的结果=新位图(nWidth,nHeight参数);使用(图形G = Graphics.FromImage((图)结果))
g.DrawImage(B,0,0,nWidth,nHeight参数)
;
返回结果;
}


解决方案

发现问题。感谢@yetapb用于显示的代码更清洁的版本,但是这仍然没有奏效。答案的问题是,我需要显式指定该图像将被保存为文件类型的类型。我的猜测是因为我没有明确指定图像格式,图像压缩并没有相应的处理。一个位图只是保存以较小的分辨率掴到它一个名为.jpg,并没有相应的压缩。下面的代码现在的作品。

 文件= System.IO.Directory.GetFiles(@C:\PicFolder); 
为(在文件中字符串的文件)
{
位图tempBmp =新位图(文件);
BMP位图=新位图(tempBmp,807,605);

bmp.Save(
@C:\NewPicFolder\Pic+数+.JPG,
System.Drawing.Imaging.ImageFormat.Jpeg);
计数++;
}


I have a question in regards to the code below. The code I have below successfully runs through a directory, and sets the resoultion of the picture to a smaller size. However, the file size is not changed. For example, an image with dimensions of 2400x1800 with file size of 1.5MB will be scaled to 800x600, but the 800x600 picture will still be 1.5MB file size. I'm think I may have to explicitly compress the picture, but I'm not sure. Any ideas?

private void Form1_Load(object sender, EventArgs e)
        {
            string[] files = null;
            int count = 0;
            files = System.IO.Directory.GetFiles(@"C:\Users\..\..\ChristmasPicsResized");
            foreach (string file in files)
            {
                System.Drawing.Bitmap bmp = System.Drawing.Bipmap.FromFile(file);

                ResizeBitmap(bmp, 807, 605).Save(
                     @"C:\users\..\..\TempPicHold\Pic" + count.ToString() + ".jpg");
                count++;
            }
        }
        public Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
        {
            Bitmap result = new Bitmap(nWidth, nHeight);
            using (Graphics g = Graphics.FromImage((Image)result))
                g.DrawImage(b, 0, 0, nWidth, nHeight);
            return result;
        }

解决方案

Found the problem. Thanks @yetapb for showing a cleaner version of the code, but that still didn't work. The answer to the problem was that I needed to explicity specify the type of file type that the image would be saved as. My guess is that because I did not specify the image format explicitly, the image compression was not handled accordingly.. A Bitmap was just saved with a smaller resolution with a '.jpg' slapped onto it, and not compressed accordingly. The following code now works.

            files = System.IO.Directory.GetFiles(@"C:\PicFolder");
            for (string file in files)
            {
            Bitmap tempBmp = new Bitmap(file);
            Bitmap bmp = new Bitmap(tempBmp, 807, 605);

            bmp.Save(
            @"C:\NewPicFolder\Pic" + count + ".jpg",
            System.Drawing.Imaging.ImageFormat.Jpeg);
            count++;
            }

这篇关于C#简单的图像调整尺寸:文件大小不缩水的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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