简体   繁体   English

如何通过选择/更改图像kb来调整图像大小? 而不是分辨率

[英]How can i resize images by selecting/changing the images kb ? And not resolution

This is how i'm using it: 这就是我的使用方式:

Bitmap bmp1 = new Bitmap(ResizeImages(fileName,100,100));

But now i'm changing the images resolution in this example 100,100 But now i want to change the images size by kb i mean if the file size is 3.64MB or for example 3,227KB and i know that in the site i want to upload the images to the maximum size of single image i can upload is 1024k so i want to be able to change the image size to 1024k. 但现在我要在此示例中更改图像分辨率100,100但现在我想按kb更改图像大小,我的意思是文件大小是3.64MB还是例如3,227KB,我知道在网站上我想上传可以上传的最大图像大小为1024k,因此我希望能够将图像大小更改为1024k。

I want to make something like the ResizeImages but instead adding resolution like 100,100 to add a size like ResizeImages(fileName,1024); 我想做一些像ResizeImages的事情,但是要增加像100,100这样的分辨率来增加像ResizeImages(fileName,1024);这样的大小。

This is the ResizeImages method but maybe it's better to make a new method ? 这是ResizeImages方法,但是最好制作一个新方法?

private static Bitmap ResizeImages(String filename, int maxWidth, int maxHeight)
        {
            using (Image originalImage = Image.FromFile(filename))
            {
                //Caluate new Size
                int newWidth = originalImage.Width;
                int newHeight = originalImage.Height;
                double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;
                if (aspectRatio <= 1 && originalImage.Width > maxWidth)
                {
                    newWidth = maxWidth;
                    newHeight = (int)Math.Round(newWidth / aspectRatio);
                }
                else if (aspectRatio > 1 && originalImage.Height > maxHeight)
                {
                    newHeight = maxHeight;
                    newWidth = (int)Math.Round(newHeight * aspectRatio);
                }
                if (newWidth >= 0 && newHeight >= 0)
                {
                    Bitmap newImage = new Bitmap(newWidth, newHeight);
                    using (Graphics g = Graphics.FromImage(newImage))
                    {
                        //--Quality Settings Adjust to fit your application
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                        g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
                        return newImage;
                    }
                }
                return null;
            }

UPDATE 更新

I found this two methods and it's working but i didn't understand how it's working with the size change: 我找到了这两种方法,并且可以正常工作,但是我不了解它如何随着尺寸变化而工作:

private static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }
        private static void SaveJPGWithCompressionSetting(Image image, string szFileName, long lCompression)
        {
            EncoderParameters eps = new EncoderParameters(1);
            eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, lCompression);
            ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
            image.Save(szFileName, ici, eps);
        }

In the constructor i did: 在构造函数中,我做到了:

Bitmap bmp1 = new Bitmap(fileName);
SaveJPGWithCompressionSetting(bmp1, FilePath, 10L);
bmp1.Dispose();

What is the 10L mean ? 10L是什么意思?

In this case for example the fileName original size is 6.71MB The new reszied file is now 566KB 例如,在这种情况下,fileName的原始大小为6.71MB现在,新的重新解析的文件为566KB

And if i want to make the file size to be 1024k for example ? 并且如果我想使文件大小为例如1024k?

So now you use the following code to crate a jpeg file: 因此,现在您使用以下代码创建jpeg文件:

Bitmap bmp1 = new Bitmap(fileName);
SaveJPGWithCompressionSetting(bmp1, FilePath, 10L);
bmp1.Dispose();

The 10L will be used for Encoder.Quality , which is a number from 0 to 100, where 100 is the highest quality. 10L将用于Encoder.Quality ,这是一个从0到100的数字,其中100是最高质量。 The L in 10L just means it is a long , and not an int . 10LL表示它很long ,而不是int So the example above will write a jpeg with quality=10, ie 10%. 因此,上面的示例将编写一个质量为10(即10%)的jpeg。

As 10% is very low quality, you will probably see that it does not look very good. 由于10%的质量非常低,您可能会发现它看起来不太好。 75% is a more common value. 75%是更常见的值。 But with 10% you reduced the filesize from 6.71MB to 566KB, which is a lot. 但是使用10%时,文件大小从6.71MB减少到566KB,这很多。 When you use 75%, the file will be much larger. 当您使用75%时,文件将更大。

As you require 1024k, you need to find the correct quality which results in that filesize, or a bit smaller, as you cannot go over the limit. 当您需要1024k时,您需要找到正确的质量,该质量会导致文件大小或更小,因为您不能超过限制。 There is no easy way to determine the quality you need. 没有简单的方法来确定您需要的质量。 You just have to try it. 您只需要尝试一下。

So for example, you start with 50%, is the file still too large, you try 25%. 例如,您以50%开始,如果文件仍然太大,则尝试25%。 You continue this way until you find the best value. 您将继续这种方式,直到找到最佳价值。 It would be the best if you could try this in-memory. 如果您可以尝试使用此内存,那将是最好的。 But I'm not sure that is possible. 但是我不确定那是可能的。 Otherwise you just have to write the images to disk, and check the filesize. 否则,您只需要将映像写入磁盘,然后检查文件大小即可。

It also depends on the image how much the filesize will be reduced by a certain quality. 它还取决于图像,文件大小将以某种质量减少多少。 For example a screenshot is much harder to compress (using jpeg) than a nature photograph. 例如,屏幕截图比自然照片更难压缩(使用jpeg)。 So for each picture you will probably have a different quality setting when the filesize if 1024k. 因此,如果文件大小为1024k,则对于每张图片,质量设置可能会有所不同。

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

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