gpt4 book ai didi

c# - 使用 Image 类在 c# 中丢失图像质量(减少颜色数量)

转载 作者:行者123 更新时间:2023-11-30 12:48:56 28 4
gpt4 key购买 nike

我有一个 c# 程序可以打开 .tif 图像,然后提供保存它的选项。但是,保存图像时质量总是下降。

(编辑:我在保存图像时传递了一些参数,以便质量为 100% 并且没有压缩,但实际唯一颜色的数量从 254 到 16,即使图像属性显示 8bpp)

(EDIT2:有问题的图像是每像素 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();
}
}

即使我不以任何方式调整图像大小或更改图像,我也会体验到质量下降。有什么建议吗?

最佳答案

System.Drawing 对 8 位图像的支持很差。从 24 位或 32 位图像转换为 8 位图像时;它将始终使用固定的默认调色板。该默认调色板仅包含 16 种灰色阴影,其他条目是各种颜色。

保存为“.bmp”时是否遇到同样的问题?如果是,那么到 8 位格式的转换已经发生较早,您必须弄清楚您的程序在哪里执行此操作并解决那里的问题。如果只是转换为 8 位的 tiff 编码器,则必须首先在单独的步骤中进行 8 位转换。创建一个 8 位图像,用灰度调色板填充 Image.Palette,然后复制位图数据。

但是 System.Drawing 对 8 位图像的支持很差,一些方法(例如 SetPixel)在处理此类图像时只会抛出 InvalidOperationException。您可能不得不使用不安全的代码(使用 LockBits 等)来复制位图数据。如果我是你,我会看看是否有可供你使用的替代图形库。

关于c# - 使用 Image 类在 c# 中丢失图像质量(减少颜色数量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13195642/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com