无法使用GDI +保存TIF文件

时间:2019-06-27 19:31:16

标签: vb.net gdi tiff

我有一个vb.net应用,该应用选择并加载JPG文件

 LoadedImage = Image.FromFile(InputImageName)

然后尝试使用GDI +将文件另存为TIF

 LoadedImage.Save(TIF_ImageName, ImageFormat.Tiff)

没有语法错误,但是保存失败

System.InvalidCastException was unhandled
Message=Conversion from string "System.Runtime.InteropServices.E" to type 'Integer' is not valid.

我尝试使用位图而不是图像,没有乐趣 图像较大(9000x11000像素) 这是encoderparams的问题-也许没有默认值吗?并且可用的样本是用于双色调图像的。

最后,我将使用位置数据生成新的TIF标签,因此这是开发的过渡步骤。

1 个答案:

答案 0 :(得分:0)

尝试使用ImageCodecInfo类,该类提供了控制用于编码Image格式的编解码器参数的方法。

在GDI +中,您可以指定数量非常有限的参数。实际上,仅使用Compression方法和Quality级别来确定压缩级别(0-100,其中0是最大压缩率)。

这些参数在EncoderParameters对象的EncoderParameter数组中进行了组合-用于指定Encoder类别-在将它们传递给{{3} }重载,将 ImageCodecInfo EncoderParameters 用作参数。

示例方法调用
使用相同的DPI设置(压缩级别设置为50%)将源图像保存为TIFF格式。图像以24bpp RGB格式保存。根据需要进行修改。
压缩类型为LZW

Dim jpgImage As Image = Image.FromFile("ImageFile.jpg"), True)
SaveTiffImage(jpgImage, jpgImage.VerticalResolution, 50L, "DestinationFile.tif")
jpgImage.Dispose()

编解码器初始化方法

Imports System.Drawing
Imports System.Drawing.Imaging
Imports ImageCodec = System.Drawing.Imaging.Encoder

Private Sub SaveTiffImage(image As Image, resolution As Single, compressionLevel As Long, fileName As String)
    resolution = Math.Max(72, Math.Min(2400, resolution))
    compressionLevel = Math.Max(0, Math.Min(100, compressionLevel))

    Using bitmap = New Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb)
        bitmap.SetResolution(resolution, resolution)
        Using g = Graphics.FromImage(bitmap)
            g.DrawImage(image, New Rectangle(Point.Empty, image.Size),
                               New Rectangle(Point.Empty, image.Size), GraphicsUnit.Pixel)
            Dim codec = ImageCodecInfo.GetImageEncoders().
                        FirstOrDefault(Function(enc) enc.FormatID = ImageFormat.Tiff.Guid)
            Dim qualityParam =
                New EncoderParameter(ImageCodec.Quality, compressionLevel)
            Dim compressionParam =
                New EncoderParameter(ImageCodec.Compression, EncoderValue.CompressionLZW)
            Using codecParms = New EncoderParameters(2)
                codecParms.Param(0) = qualityParam
                codecParms.Param(1) = compressionParam
                bitmap.Save(fileName, codec, codecParms)
            End Using
        End Using
    End Using
End Sub

C#版本

using System.Drawing;
using System.Drawing.Imaging;
using ImageCodec = System.Drawing.Imaging.Encoder;

private void SaveTiffImage(Image image, float resolution, long compressionLevel, string fileName)
{
    resolution = Math.Max(72, Math.Min(2400, resolution));
    compressionLevel = Math.Max(0, Math.Min(100, compressionLevel));

    using (var bitmap = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb))
    {
        bitmap.SetResolution(resolution, resolution);
        using (var g = Graphics.FromImage(bitmap))
        {
            g.DrawImage(image, new Rectangle(Point.Empty, image.Size), 
                               new Rectangle(Point.Empty, image.Size), GraphicsUnit.Pixel);
            var imageCodec = ImageCodecInfo.GetImageEncoders()
                                .FirstOrDefault(enc => enc.FormatID == ImageFormat.Tiff.Guid);
            if (imageCodec == null)
                throw new NotSupportedException("TIFF Conversion not suppoted", 
                      new NullReferenceException("TIFF Codec reference missing"));
            var qualityParam = 
                new EncoderParameter(ImageCodec.Quality, compressionLevel);
            var compressionParam = 
                new EncoderParameter(ImageCodec.Compression, (long)EncoderValue.CompressionLZW);
            using (var codecParms = new EncoderParameters(2))
            {
                codecParms.Param[0] = qualityParam;
                codecParms.Param[1] = compressionParam;
                bitmap.Save(fileName, imageCodec, codecParms);
            }
        }
    }
}