使用Image类损失图像质量在C#(减少颜色量) [英] Losing image quality in c# using Image class (reduces amount of colors)

查看:1510
本文介绍了使用Image类损失图像质量在C#(减少颜色量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有打开的.tif图像,后来提供了保存它的选项C#程序。然而,保存图像时,有总质量的下降。

I have a c# program that opens a .tif image and later offers the option to save it. However, there is always a drop in quality when saving the image.

(编辑:我通过一些参数,同时保存图像,这样的质量是100%,没有压缩,但数实际的独特的色彩去从254到16,即使图像属性显示8bpp)

(EDIT2:本有关图像在每像素8位灰度图像 - 256色/灰度 - 这不,每个我测试,所有的颜色都保留像素的彩色图像的24位发生我开始认为图像类可能只支持16级灰度)

我如何避免这种情况?

下面是用于打开图像的代码:

Here's the code for opening 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;
}

这是我保存图片的方式:

This is the way 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 don't resize or change the image in any ways, I experience a loss in quality. any advice?

推荐答案

System.Drawing中有8位图像支持较弱。当从24或32位图像转换为8位;它会一直使用一个固定的默认调色板。该默认调色板只包含16级灰度,其他条目是不同的颜色。

System.Drawing has poor support for 8-bit images. When converting from 24 or 32-bit images to 8-bit; it'll always use a fixed default color palette. That default color palette only contains 16 shades of grey, the other entries are various colors.

执行为.BMP保存时,你有同样的问题?如果是,则转换为8位格式已经发生了前面,你就必须要弄清楚你的程序确实是并修复问题在那里。
。如果这仅仅是TIFF编码器可以转换为8位,你就必须首先做一个单独的步骤中的8位转换。创建一个8位图像,填写 Image.Palette 带灰度的调色板,然后通过位图数据复制。

Do you have the same problem when saving as '.bmp'? If yes, then the conversion to the 8-bit format already happened earlier, you'll have to figure out where your program does that and fix the issue there. If it's only the tiff encoder that converts to 8-bit, you'll have to do the 8-bit conversion in a separate step first. Create an 8-bit image, fill Image.Palette with a gray-scale palette, and then copy the bitmap data over.

但System.Drawing中有8位图像的支持非常差,和几个方法(如的setPixel )将只是把出现InvalidOperationException 这样图片的时候。你可能将不得不使用不安全的代码(以 LockBits 等)复制位图数据。如果我是你,我想看看是否有替代的图形库,你可以使用。

But System.Drawing has poor support for 8-bit images, and several methods (e.g. SetPixel) will just throw InvalidOperationException when dealing with such images. You will probably have to use unsafe code (with LockBits etc.) to copy the bitmap data. If I were you, I'd look if there are alternative graphics libraries you could use.

这篇关于使用Image类损失图像质量在C#(减少颜色量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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