【问题标题】:Set DPI value to Tiff Image in C#在 C# 中将 DPI 值设置为 Tiff 图像
【发布时间】:2010-06-22 23:06:33
【问题描述】:

我正在尝试通过代码在 C# 中设置 TIFF 图像的 dpi 值,但不知何故,这些值在保存图像后并未保留。

using (var image = new Bitmap(@"c:\newimage.tif"))
{
    uint[] uintArray = { 300, 1}; //Setting DPI as 300
    byte[] bothArray = ConvertUintArrayToByteArray(uintArray);
    PropertyItem item = image.PropertyItems.Where(p => p.Id == 0x11A).Single();
    var val = BitConverter.ToUInt32(item.Value, 0);
    Console.WriteLine(val);
    item.Id = 0x11A;
    item.Value = bothArray;
    item.Type = 5;
    item.Len = item.Value.Length;
    image.SetPropertyItem(item);
    image.Save(@"c:\newimage1.tif"); //Save image to new File
}

这段代码有什么问题?任何形式的帮助将不胜感激。 TIFF file tag definitions

【问题讨论】:

    标签: c# image-processing tiff


    【解决方案1】:

    同时设置属性值和位图分辨率,然后重新保存图像应该会改变分辨率(它适用于我的示例图像)。我相信原始文件必须存在 X 和 Y 分辨率的标签,如果不存在,不确定 .NET 是否会添加这些标签(必须测试)。

    这是一个使用 .NET 读取和写入 TIFF 图像的 X 和 Y 分辨率的示例:

    int numerator, denominator;
    
    using (Bitmap bmp = (Bitmap)Bitmap.FromFile(@"C:\input.tif"))
    {
        // obtain the XResolution and YResolution TIFFTAG values
        PropertyItem piXRes = bmp.GetPropertyItem(282);
        PropertyItem piYRes = bmp.GetPropertyItem(283);
    
        // values are stored as a rational number - numerator/denominator pair
        numerator = BitConverter.ToInt32(piXRes.Value, 0);
        denominator = BitConverter.ToInt32(piXRes.Value, 4);
        float xRes = numerator / denominator;
    
        numerator = BitConverter.ToInt32(piYRes.Value, 0);
        denominator = BitConverter.ToInt32(piYRes.Value, 4);
        float yRes = numerator / denominator;
    
        // now set the values
        byte[] numeratorBytes = new byte[4];
        byte[] denominatorBytes = new byte[4];
    
        numeratorBytes = BitConverter.GetBytes(600); // specify resolution in numerator
        denominatorBytes = BitConverter.GetBytes(1);
    
        Array.Copy(numeratorBytes, 0, piXRes.Value, 0, 4); // set the XResolution value
        Array.Copy(denominatorBytes, 0, piXRes.Value, 4, 4);
    
        Array.Copy(numeratorBytes, 0, piYRes.Value, 0, 4); // set the YResolution value
        Array.Copy(denominatorBytes, 0, piYRes.Value, 4, 4);
    
        bmp.SetPropertyItem(piXRes); // finally set the image property resolution
        bmp.SetPropertyItem(piYRes);
    
        bmp.SetResolution(600, 600); // now set the bitmap resolution
    
        bmp.Save(@"C:\output.tif"); // save the image
    }
    

    【讨论】:

      【解决方案2】:

      我怀疑位图分辨率会覆盖该属性。使用 Bitmap.SetResolution()。

      【讨论】:

        【解决方案3】:

        免责声明:我为 Atalasoft 工作,该公司生产用于通过 .NET 处理图像的产品。

        您可以使用 TiffDocument 类的 dotImage 来执行此操作,代码如下:

        private void RemoveIfContains(TiffTagCollection tags, int tagID)
        {
            TiffTag tag = tags.LookupTag(tagID);
            if (tag != null)
               tags.Remove(tag);
        }
        
        public void SetTiffResolution(string tiffin, string tiffout, int page, double resolution ) {
            if (tiffin == tiffout)
               throw new ArgumentException(tiffout, "output path must be different from input path");
            TiffFile tf = new TiffFile();
            using (FileStream stm = new FileStream(tiffin, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                tf.Read(stm);
                TiffDirectory image = tf.Images[page];
                RemoveIfContains(image.Tags, TiffTagID.ResolutionX);
                RemoveIfContains(image.Tags, TiffTagID.ResolutionY);
                RemoveIfContains(image.Tags, TiffTagID.ResolutionUnit);
                image.Tags.Add(new TiffTag(TiffTagID.ResolutionX, resolution);
                image.Tags.Add(new TiffTag(TiffTagID.ResolutionY, resolution);
                image.Tags.Add(new TiffTag(TiffTagID.ResolutionUnit, 2)); // 2 == dots per INCH
                tf.Save(tiffout);
            }
        }           
        

        优点是多方面的 - 直接访问标签让您可以按照自己的方式进行设置,而无需重新编码图像。如果您的初始图像在 TIFF 中使用 JPEG 进行压缩,则您不必担心重新编码 JPEG 数据和丢失信息的问题。此外,您不必担心丢失已经存在的 TIFF 标签信息。此代码也适用于多页 TIFF。最后,这可能是一种比解码整个图像来更改标签更快的方法。

        这种方法的缺点是您确实需要了解 TIFF 规范。如果没有这种理解,您可能会创建可能并不总是可解析的文件。例如,DotImage 和 IrfanView 在处理各种损坏的 TIFF 时非常宽容。 Adobe PhotoShop 是出了名的严格。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-18
          相关资源
          最近更新 更多