问题描述
我正尝试在Mac OS上使用System.Drawing.Common
和以下C#代码(.NET CORE 3.1)将BMP图像转换为tiff图像:
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
我的Google搜索表明我必须安装已经使用brew install mono-libgdiplus
和brew upgrade mono-libgdiplus
的libgdiplus。
我还通过安装runtime.osx.10.10-x64.CoreCompat.System.Drawing
软件包尝试了@Andy的建议答案,很遗憾,这没有用。
还有其他建议吗?
解决方法
问题是mono-libgdiplus
没有实现EncoderValue.CompressionLZW
编码器参数。我建议不要使用该库,而是安装此nuget包:
runtime.osx.10.10-x64.CoreCompat.System.Drawing
一旦我添加了该软件包,您的代码就会完美运行。
,不幸的是,我没有Mac可以测试。我在Windows和Ubuntu 20上使用DotNet Core 3.1测试了您的代码,您的代码似乎可以正常工作。我只能猜测,这可能与您如何创建或修改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 (using System.Linq)
// Much clearer than that other funny loop code on MSDN
var tiffCodec = ImageCodecInfo.GetImageEncoders().
Where(info => info.MimeType == "image/tiff").First();
//.First() throws an exception if the list is empty
//.FirstOrDefault() returns null if the list is empty
// 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);
}
}