C#简单图像大小调整:文件大小不缩小

C#简单图像大小调整:文件大小不缩小,c#,image,resize,image-resizing,C#,Image,Resize,Image Resizing,我对下面的代码有一个问题。下面的代码成功地在一个目录中运行,并将图片的资源设置为较小的大小。但是,文件大小没有更改。例如,尺寸为2400x1800、文件大小为1.5MB的图像将缩放为800x600,但800x600图片的文件大小仍为1.5MB。我想我可能必须明确地压缩图片,但我不确定。有什么想法吗 private void Form1_Load(object sender, EventArgs e) { string[] files = null;

我对下面的代码有一个问题。下面的代码成功地在一个目录中运行,并将图片的资源设置为较小的大小。但是,文件大小没有更改。例如,尺寸为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;
        }

不确定位图,但对于其他图像,可以指定不同的压缩编码器。MSDN详细信息

您需要设置图形对象的某些属性以更改图像质量

graphics.CompositingQuality = CompositingQuality.HighSpeed; 
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.DrawImage(photo, 0, 0, width, height);

您还可以在保存文件时设置不同的压缩编码或以不同的格式保存文件。

有趣的实现细节:翻转图像两次,会导致缩略图弹出,从而减小文件大小

result.RotateFlip(系统图.RotateFlipType.Rotate180FlipNone)

result.RotateFlip(系统图.RotateFlipType.Rotate180FlipNone)

做了一些更改,下面的代码按预期减少了文件大小(对我来说)


}找到了问题。感谢@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++;
            }

}

此外,请查看您需要避免的问题。现在,您正在泄漏GDI句柄、内存,并生成低质量的图像。实际上,为什么不使用一个能够避免您遇到的所有GDI错误的库呢?该库是免费的、开源的、受支持的,并且有很好的文档记录。它有一个1行API,大约需要一秒钟的时间才能理解。因此,不要执行
System.Drawing.Bitmap bmp=System.Drawing.Bipmap.FromFile(文件),是否创建新位图?不过,这个版本仍然存在在位图文件末尾打一个
.jpg
的问题。OP似乎想要转换,而不仅仅是重命名为
.jpg
。看看我写的东西和OP写的东西(6年前!),我可以看到我只是编辑了他的代码,使文件大小减少了(对我来说)。他的问题从未明确表示要转换为jpeg格式,但他对自己问题的回答确实如此。另外:
graphics.SmoothingMode=SmoothingMode.HighQuality;graphics.PixelOffsetMode=PixelOffsetMode.HighQuality对逻辑的解释就好了。
            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++;
            }
 private void button4_Click(object sender, EventArgs e)
  {
            String[] files;
            int count = 0;
            files = System.IO.Directory.GetFiles(@"C:/dataset");
            foreach (string file in files)
            {
            Bitmap tempBmp = new Bitmap(file);
            Bitmap bmp = new Bitmap(tempBmp, 200, 200);

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