Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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;
        }
Posted
Updated 21-Mar-19 23:42pm
v2

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"
 
Share this answer
 
Comments
Maciej Los 22-Mar-19 5:24am    
5ed!
TheRealSteveJudge 22-Mar-19 6:52am    
Thank you Maciej!
Quote:
'without losing any quality'
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+"[^].
 
Share this answer
 
Comments
Maciej Los 22-Mar-19 5:24am    
5ed!
TheRealSteveJudge 22-Mar-19 6:53am    
5*
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
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.
 
Share this answer
 
Comments
Maciej Los 22-Mar-19 5:24am    
5ed!
TheRealSteveJudge 22-Mar-19 6:53am    
5*
Quote:
How do I reduce image size without losing any quality

The general answer is you can't.
The only case where 'you can' is when you have an uncompressed image (.bmp) and that you change format to a compressed one (.gif, .png ...).
Beyond this, all operations you do are loosing quality.
- Down scaling is by definition removing information, thus loosing quality.
- Reducing color scheme is by definition removing information, thus loosing quality.
- Lousy compression (.jpeg) is by definition removing information, thus loosing quality.

said otherwise, you can't reduce image size to an arbitrary small size without loosing informations and quality.
 
Share this answer
 
Comments
TheRealSteveJudge 22-Mar-19 6:54am    
5*
Patrice T 22-Mar-19 7:07am    
Thank you
Abuamer 24-Mar-19 4:48am    
thank you my friend.could you help me with converting images into jpg frmat before saving them. inside my method above.. thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900