如何在不损失任何质量的情况下缩小图像尺寸 [英] How do I reduce image size without losing any quality

查看:442
本文介绍了如何在不损失任何质量的情况下缩小图像尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有c#桌面应用程序,我创建它来存储SQL服务器数据库中的图像

,根据客户的需求。问题是代码工作正确,图像以不同的方式存储,但有一个大尺寸,所以我试图在保存之前调整图像大小,但我调整大小的方法没有帮助。问题是,

这可能会减小尺寸而不会降低质量



我尝试过:



i have c# desktop application which i created to store images inside SQL server database
,Based on customer's desire .the problem is the code work's correctly and the images stored with different ways, but with a big size,so i tried to resize images before saving then,but my method to resize doesn't help. the question is ,
is this possible to reduce the size without losing quality

What I have tried:

private void barBtnOpenPhotos_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            // there is where the user load's images
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "JPG|*.jpg|JPEG|*.jpeg|PNG|*.png|Tif|*.tif";
            ofd.Multiselect = true;
            ofd.ShowDialog();
            string[] files = ofd.FileNames;
            // here  i create adatagridview to load multiple images 
            DataGridViewImageColumn img = new DataGridViewImageColumn();
            img.HeaderText = "ImageSelect";
            img.Name = "PhotoImage";
            img.ImageLayout = DataGridViewImageCellLayout.Stretch;
            datagridview_Images.Columns.Add(img);
            //here i view images inside 
            datagridview_Images.Rows.Clear();
            foreach (string filename in files)
            {
                Image image = Image.FromFile(filename);
//this is the method that's resize images
                image = reesizeImage(image, new Size(1024, 576)); 
                datagridview_Images.Rows.Add(image);
            }
        }
private static Image reesizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }

推荐答案

is this possible to reduce the size without losing quality



答案是否。



缩小图像意味着减少其中包含的信息。



请查看了解数字图像插值 [ ^ ]



有人说每次执行插值时图像总会丢失一些质量


The answer is "No".

Downsizing an image means reducing the information it contains.

Please have a look at Understanding Digital Image Interpolation[^]

There it is said "an image will always lose some quality each time interpolation is performed"


引用:

'没有丢失任何质量'

这是一个非常严格的要求, TheRealSteveJudge 是对的,你不能满足它。



然而,如果你放松这样的要求并接受失去一些质量,那么就有了看看 Libor Tinka的图像调整大小 - 超越GDI + [ ^ ]。

This is a very strict requirement and TheRealSteveJudge is right, you cannot satisfy it.

However, if you relax such a requirement and accept losing some quality then have a look at Libor Tinka's "Image Resizing - outperform GDI+"[^].


有两种形式的缩小尺寸:文件大小,有时可以减少而不会降低质量;和分辨率,但不能。



文件大小可以减少,如果你加载一个文件并将其保存为不同的格式 - 但目标格式必须是一种无损压缩方法,JPG肯定不是这样:每次编写JPG文件时,都会丢弃信息并显着降低质量。话虽如此,因为JPG无论如何都是有损压缩格式,很有可能无损压缩格式文件实际上可能比JPG输入更大。



分辨率可以在不牺牲质量的情况下减少,因为你必须扔掉信息以降低分辨率。想一想:将图像从4x4缩小到2x2意味着目标图像中的每个像素都是原始像素的四个像素的混合物:

There are two forms of "reduced size": file size, which can - sometimes - be reduced without losing quality; and resolution, which can't.

Files size can be reduced, if you load a file and save it as a different format - but the target format has to be a lossless compression method, which JPG most certainly isn't: every time you write a JPG file you throw away information and degrade quality significantly. Having said that, because JPG is a lossy compressed format anyway, it is quite possible that a lossless compression format file may actually be bigger than the JPG input.

Resolution can't be reduced without sacrificing quality because you have to throw away information to reduce the resolution. Think about it: to reduce an image from 4x4 to 2x2 means that each pixel in the target image is an amalgam of four pixels from the original:
1122  --->>  12
1122         34
3344
3344

无论您在NCIS,CSI中看到什么,无法从结果中的单个像素重新生成源中的信息和刀锋亚军!当你放大它时,它可以通过参考周围像素来近似,但是一旦信息消失,它就会消失。

And the information in the source cannot be regenerated from the single pixel in the result, regardless of what you see in NCIS, CSI, and Blade Runner! It can be approximated by reference to the surrounding pixels when you "enlarge" it, but once that info is gone, it's gone for good.


这篇关于如何在不损失任何质量的情况下缩小图像尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆