如何从TIFF图像创建C#中的世界文件?

时间:2017-01-12 05:24:12

标签: c# tiff libtiff.net

有没有简单的方法从tiff图像中提取C#中的world file

我有一个tiff图像,我需要创建一个没有任何第三方库的世界文件。

1 个答案:

答案 0 :(得分:0)

我找到了路......

    /// <summary>
    /// Create tfw - world file.
    /// </summary>
    /// <param name="filename">tif image full filename</param>
    private void CreateWorldFile(string filename)
    {
        using (var bitmapImage = new Bitmap(filename))
        {
            PropertyItem[] imageProps = bitmapImage.PropertyItems;

            var modelscale = imageProps.First(a => a.Id == GEOTIFF_MODELPIXELSCALETAG);
            var tiepoint = imageProps.First(a => a.Id == GEOTIFF_MODELTIEPOINTTAG);

            double x = BitConverter.ToDouble(tiepoint.Value, 0 + 24);
            double y = BitConverter.ToDouble(tiepoint.Value, 0 + 32);

            double xscale = BitConverter.ToDouble(modelscale.Value, 0);
            double yscale = BitConverter.ToDouble(modelscale.Value, 0 + 8);

            string tfwFileName = Path.GetDirectoryName(filename) + "\\" + Path.GetFileNameWithoutExtension(filename) + ".tfw";

            using (System.IO.StreamWriter file = new System.IO.StreamWriter(tfwFileName))
            {
                file.WriteLine(xscale);
                file.WriteLine("0.0");
                file.WriteLine("0.0");
                file.WriteLine(yscale);
                file.WriteLine(x);
                file.WriteLine(y);
            }
        }
    }

    private readonly int GEOTIFF_MODELPIXELSCALETAG = 33550;
    private readonly int GEOTIFF_MODELTIEPOINTTAG = 33922;