我试图使用System.Drawing.Common将BMP图像转换为tiff映像,在Mac上使用以下C#代码(.NET核心3.1):
public void SaveBitmapAsTiff(string location, Bitmap image)
{
var imageCodecInfo = GetEncoderInfo("image/tiff");
var encoder = Encoder.Compression;
var encoderParameters = new EncoderParameters(1);
var encoderParameter = new EncoderParameter(
encoder,
(long)EncoderValue.CompressionLZW);
encoderParameters.Param[0] = encoderParameter;
location = location.Replace(".bmp", "");
image.Save(location + ".tiff", imageCodecInfo, encoderParameters);
// image.Save(location + ".tiff", ImageFormat.Tiff); Also tried this method overload
}
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for(j = 0; j < encoders.Length; ++j)
{
if(encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}location参数包含要写入的文件路径,image参数包含从磁盘加载的位图。
当我试图运行这段代码时,我会得到以下异常:
Unhandled exception. System.NotImplementedException: Not implemented.
at System.Drawing.SafeNativeMethods.Gdip.CheckStatus(Int32 status)
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at BmpTiffConverter.Services.FileService.SaveBitmapAsTiff(String location, Bitmap image) in /Users/tomcoldenhoff/Documents/ReSoftware/Repositories/BmpTiffConverter/BmpTiffConverter/Services/FileService.cs:line 54
at BmpTiffConverter.Services.ConverterService.ConvertBmpToTiff(String inputPath, String outputPath) in /Users/tomcoldenhoff/Documents/ReSoftware/Repositories/BmpTiffConverter/BmpTiffConverter/Services/ConverterService.cs:line 20我的谷歌搜索表明,我必须安装libgdiplus,而我已经安装了brew install mono-libgdiplus和brew upgrade mono-libgdiplus。
我还通过安装runtime.osx.10.10-x64.CoreCompat.System.Drawing包尝试了@Andy的建议答案,不幸的是,这是行不通的。
有人有别的建议吗?
发布于 2020-09-02 04:41:27
问题是mono-libgdiplus没有实现EncoderValue.CompressionLZW编码器参数。我建议不要使用这个库,而是安装这个nuget包:

runtime.osx.10.10-x64.CoreCompat.System.Drawing
一旦我添加了那个包,您的代码就完美地工作了。
发布于 2020-09-01 15:46:41
不幸的是,我没有Mac需要测试。我用DotNet Core3.1在Windows和Ubuntu 20上测试了您的代码,您的代码看起来很好用。我只能猜测这与您如何创建或修改您的Bitmap有关。
下面是我如何在Linux下创建这个项目的。dotnet add package是我如何添加System.Drawing.Common NuGet包的。我不知道它是如何在Mono上工作的,或者你是否在使用Mono。
> dotnet new console
> dotnet add package System.Drawing.Common
> dotnet restore我编辑了program.cs
> dotnet build
> dotnet run一切都很好。我现在可以在任何图像查看器中打开tiff图像。
从代码风格的角度来看,我建议进行相当多的更改。我知道您遵循了MSDN的代码示例,但就目前情况而言,它们的示例在这个实例中并不十分清楚。我刚刚复制和粘贴了我的整个.cs文件,并在各种修改中添加了注释。编写清晰的代码和编写工作代码一样重要。
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
class Program
{
// made the function more generic Bitmap inherits from Image and Image contains
// the save feature so it might as well be made more general
public static void SaveImageAsTiff(string location, Image image)
{
// fetch tiffCodec
var tiffCodec = ImageCodecInfo
.GetImageEncoders()
.Where(info => info.MimeType == "image/tiff")
.First();
// create encoderParameters
// this structure is much clearer IMO
// (Can you see that there is now a color depth parameter?)
var encoderParameters = new EncoderParameters(2)
{
Param = new EncoderParameter[]{
new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionLZW),
new EncoderParameter(Encoder.ColorDepth, 24L)
}
};
// save image using codec and encoder parameters
// it is common behavior to just save the file with the given location parameter
// and not to try do some extension conversion
image.Save(location, tiffCodec, encoderParameters);
}
static void Main(string[] args)
{
// original bitmap location
// (Yes forward slashes work on Windows so relative file paths are simple to handle)
string bmpLocation = @"C:/Images/test.bmp";
//string bmpLocation = @"~/Images/test.bmp"; // test on Linux
// tidy way to convert file extensions of a path in C#
string tiffLocation = System.IO.Path.ChangeExtension(bmpLocation, "tiff");
// open image and force it to be a bitmap for examples sakes
Bitmap bmpImage = new Bitmap(Image.FromFile(bmpLocation));
// save image as tiff
SaveImageAsTiff(tiffLocation, bmpImage);
}
}https://stackoverflow.com/questions/63619050
复制相似问题