简体   繁体   English

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

[英]C# Simple Image Resize : File Size Not Shrinking

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. 我下面的代码成功运行了一个目录,并将图片的resoultion设置为更小的尺寸。 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. 例如,尺寸为2400x1800且文件大小为1.5MB的图像将缩放为800x600,但800x600图片仍为1.5MB文件大小。 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. 感谢@yetapb显示更清晰的代码版本,但仍然无效。 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. 我的猜测是,因为我没有明确指定图像格式,所以没有相应地处理图像压缩。一个Bitmap刚刚以较小的分辨率保存,并在其上打了一个“.jpg”,并且没有相应地进行压缩。 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++;
            }

Not sure about bitmaps, but for other images you can specify a different compression encoder. 不确定位图,但对于其他图像,您可以指定不同的压缩编码器。 MSDN details here 这里有 MSDN详细信息

You need to set some of the properties on the Graphics object to change the quality of the image. 您需要在Graphics对象上设置一些属性以更改图像的质量。

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

You can also set different compression encodings when saving the file or save it in a different format. 您还可以在保存文件时设置不同的压缩编码,或以不同的格式保存。

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

} }

Interesting implementation detail: flip the image twice, and it will cause the thumbnail to be thrown out and this will decrease the file size. 有趣的实现细节:将图像翻转两次,这将导致缩略图被抛出,这将减小文件大小。

result.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); result.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

Made a couple changes, the following code reduced file sizes as expected (for me). 进行了一些更改,以下代码按预期减少了文件大小(对我而言)。

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)
    {
        Bitmap bmp = new Bitmap( file );
        new Bitmap( bmp, 807, 605 ).Save(
                   @"C:\users\..\..\TempPicHold\Pic" + count.ToString() + ".jpg");
        count++;   
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM