I have a C # program that opens a .tif image and then offers a save option. However, when you save an image, quality always decreases.
(EDIT: I passed some parameters when saving the image so that the quality is 100% and there is no compression, but the number of real unique colors goes from 254 to 16, although the image properties show 8bpp)
(EDIT2: the image in question is a grayscale image of 8 bits per pixel - 256 colors / shades of gray). This does not happen with the 24-bit color image I tested, where all the colors are saved. I'm starting to think that an image class can only support 16 shades of gray)
How can I avoid this?
Here is the code to open the image:
public Image imageImport() { Stream myStream = null; OpenFileDialog openTifDialog = new OpenFileDialog(); openTifDialog.Title = "Open Desired Image"; openTifDialog.InitialDirectory = @"c:\"; openTifDialog.Filter = "Tiff only (*.tif)|*.tif"; openTifDialog.FilterIndex = 1; openTifDialog.RestoreDirectory = true; if (openTifDialog.ShowDialog() == DialogResult.OK) { try { if ((myStream = openTifDialog.OpenFile()) != null) { using (myStream) { String tifFileName= openTifDialog.FileName; imgLocation = tifFileName; Bitmap tifFile = new Bitmap(tifFileName); return tifFile; } } } catch (Exception ex) { MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); } } return null; }
So I save the image:
private void saveImage(Image img) { SaveFileDialog sf = new SaveFileDialog(); sf.Title = "Select File Location"; sf.Filter = " bmp (*.bmp)|*.bmp|jpeg (*.jpg)|*.jpg|tiff (*.tif)|*.tif"; sf.FilterIndex = 4; sf.RestoreDirectory = true; sf.ShowDialog(); // If the file name is not an empty string open it for saving. if (sf.FileName != "") { // Saves the Image via a FileStream created by the OpenFile method. System.IO.FileStream fs = (System.IO.FileStream)sf.OpenFile(); // Saves the Image in the appropriate ImageFormat based upon the // File type selected in the dialog box. // NOTE that the FilterIndex property is one-based. switch (sf.FilterIndex) { case 1: img.Save(fs, System.Drawing.Imaging.ImageFormat.Bmp); break; case 2: img.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); break; case 3://EDITED -STILL DOES NOT RESOLVE THE ISSUE ImageCodecInfo codecInfo = ImageClass.GetEncoderInfo(ImageFormat.Tiff); EncoderParameters parameters = new EncoderParameters(2); parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,100L); parameters.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionNone); img.Save(fs,codecInfo, parameters); break; } fs.Close(); } }
Even if I do not resize or resize the image in any way, I experience a loss of quality. any advice?
source share