C# 简单图像调整大小 : File Size Not Shrinking

标签 c# image resize image-resizing

我对下面的代码有疑问。我在下面的代码成功地运行了一个目录,并将图片的分辨率设置为较小的尺寸。但是,文件大小没有改变。例如,尺寸为 2400x1800 且文件大小为 1.5MB 的图像将缩放为 800x600,但 800x600 图片的文件大小仍为 1.5MB。我想我可能必须明确压缩图片,但我不确定。有什么想法吗?

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

最佳答案

发现问题。感谢@yetapb 展示了更清晰的代码版本,但仍然没有用。问题的答案是我需要明确指定图像将保存为的文件类型。我的猜测是,因为我没有明确指定图像格式,所以图像压缩没有得到相应处理。位图只是以较小的分辨率保存,并在其上打了一个“.jpg”,并没有相应地压缩。下面的代码现在可以工作了。

            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# 简单图像调整大小 : File Size Not Shrinking,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1909523/

相关文章:

java - 如何相对于另一个组件布局组件?

html - 使用 'background-size: 100%'

html - 无法调整图像 HTML/CSS 的大小

c# - 从 NUNIT 测试启动应用程序

c# - 使用 WinSCP .NET 程序集从 FTP 服务器下载除特定文件夹之外的所有文件

css - 图像位于 div 底部但在滚动后保持固定

iOS - 处理大图像

c# - GIT:修改文件并以相同的日期/时间提交

c# - 从文本框中获取数据时捕获异常

Android:如何通过 Intent 实现多选图片?