无法使用GDI +保存TIF文件 [英] Cannot save TIF file using GDI+

查看:86
本文介绍了无法使用GDI +保存TIF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a vb.net app that selects and loads a JPG file

 LoadedImage = Image.FromFile(InputImageName)

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

then attempts to save the file as a TIF using GDI+

 LoadedImage.Save(TIF_ImageName, ImageFormat.Tiff)

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

There are no syntax errors, but the save fails with

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

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

I have tried using a bitmap instead of image, no joy The image is large (9000x11000 pixels) Is this an encoderparams issue - perhaps there is no default?, and the available samples for that were for a bitonal image.

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

In the end, I will be generating new TIF Tags with location data, so this is an interim step in the development.

推荐答案

尝试使用

Try with the ImageCodecInfo class, which provides means to control the codec parameters used to encode an Image format.

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

In GDI+, you can specify a really limited number of parameters. In practice, just the Compression method and the Quality level, which determines the compression level (0-100, where 0 is the maximum compression).

这些参数在 EncoderParameters 数组> EncoderParameter 对象-用于指定编码器类别-在将它们传递给 Image.Save()重载,该重载接受 ImageCodecInfo EncoderParameters 作为参数.

These parameters are assembled in a EncoderParameters array of EncoderParameter objects - used to specify the Encoder cathegory - before passing them to the Image.Save() overload that accepts the an ImageCodecInfo and EncoderParameters as arguments.

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

Sample method call:
Save a source image to a TIFF format, using the same DPI setting, compression level set to 50%. The image is saved as in 24bpp RGB Format. Modify as required.
The compression type is 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);
            }
        }
    }
}

这篇关于无法使用GDI +保存TIF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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