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

发布于 2024-08-15 18:11:44 字数 1022 浏览 3 评论 0原文

我对下面的代码有疑问。我下面的代码成功地运行了一个目录,并将图片的分辨率设置为较小的尺寸。但是,文件大小不会改变。例如,尺寸为 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;
        }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ゞ记忆︶ㄣ 2024-08-22 18:11:45

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

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++;
            }
醉城メ夜风 2024-08-22 18:11:45

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

Not sure about bitmaps, but for other images you can specify a different compression encoder. MSDN details here

爱人如己 2024-08-22 18:11:45

您需要设置 Graphics 对象的一些属性来更改图像的质量。

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

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

You need to set some of the properties on the Graphics object to change the quality of the image.

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.

如果没结果 2024-08-22 18:11:45

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

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

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

落花随流水 2024-08-22 18:11:45

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

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

}

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

}

淡水深流 2024-08-22 18:11:45
 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++;
            }  

}

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

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文