如何从 tiff 图像在 C# 中创建世界文件?

How to create world file in C# from a tiff image?

有什么简单的方法可以在 C# 中从 tiff 图像中提取 world file

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

我找到了路...

    /// <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;