我有一个C#程序,可以打开.tif格式的图片,并提供保存选项。然而,在保存图片时总是会出现质量下降的情况。
(编辑:我在保存图片时传递了一些参数,使得质量达到了100%且没有压缩,但实际独特颜色的数量从254变为16,尽管图像属性显示为8bpp)
(编辑2:所涉及的图像为灰度图像,每像素8位 - 256种灰度颜色/阴影 - 在我测试的24位每像素彩色图像中不会发生这种情况,其中所有颜色都被保留。我开始认为图像类可能仅支持16个灰度级别)
如何避免这种情况?
以下是打开图像的代码:
这是我保存图像的方法:
即使我没有调整或更改图片的任何方式,我仍然会遇到质量损失。有什么建议吗?
(编辑:我在保存图片时传递了一些参数,使得质量达到了100%且没有压缩,但实际独特颜色的数量从254变为16,尽管图像属性显示为8bpp)
(编辑2:所涉及的图像为灰度图像,每像素8位 - 256种灰度颜色/阴影 - 在我测试的24位每像素彩色图像中不会发生这种情况,其中所有颜色都被保留。我开始认为图像类可能仅支持16个灰度级别)
如何避免这种情况?
以下是打开图像的代码:
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;
}
这是我保存图像的方法:
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();
}
}
即使我没有调整或更改图片的任何方式,我仍然会遇到质量损失。有什么建议吗?