使用 MagickImage.NET 将图像列表转换为单个 Tiff 文件
Convert List of Images to Single Tiff File Using MagickImage.NET
我正在使用以下代码从使用 MagickImage.NET Library 的图像列表创建单个 tiff 文件:
/// <summary>
/// Create single tiff file from a list of base64String images
/// </summary>
/// <param name="pages">A list of base64String images</param>
/// <returns>Byte array of the created tiff file</returns>
public static byte[] CreateSingleTiff(List<string> pages)
{
MagickImage pageImage;
using (MemoryStream byteStream = new MemoryStream())
{
using (MagickImageCollection imagecoll = new MagickImageCollection())
{
for (int i = 0; i < pages.Count; i++)
{
byte[] newBytes = Convert.FromBase64String(pages[i].Replace("data:image/Jpeg;base64,", ""));
pageImage = new MagickImage(newBytes);
imagecoll.Add(pageImage);
}
return imagecoll.ToByteArray();//The problem occurs right here
}
}
}
但我只得到第一页!。
这是我用来将图像写入磁盘的行:
Image.FromStream(new MemoryStream(result)).Save(path, System.Drawing.Imaging.ImageFormat.Tiff);
我试图在 Whosebug 上挖掘类似的东西,但我没有运气。显然对 MagickImage.NET 库没有很好的支持。如果你发现这个方法没用,除了这个和 this one
还有什么其他可用的方法
这行似乎有问题return imagecoll.ToByteArray();
,图像是一个多页的tiff图像,返回的字节数组的长度与添加到tiff图像中的图像相比太小了。我找到了一个重载方法,它使用 MagickFormat
枚举将图像格式作为参数,在我的例子中我使用了 MagickFormat.Tiff
.
我正在使用以下代码从使用 MagickImage.NET Library 的图像列表创建单个 tiff 文件:
/// <summary>
/// Create single tiff file from a list of base64String images
/// </summary>
/// <param name="pages">A list of base64String images</param>
/// <returns>Byte array of the created tiff file</returns>
public static byte[] CreateSingleTiff(List<string> pages)
{
MagickImage pageImage;
using (MemoryStream byteStream = new MemoryStream())
{
using (MagickImageCollection imagecoll = new MagickImageCollection())
{
for (int i = 0; i < pages.Count; i++)
{
byte[] newBytes = Convert.FromBase64String(pages[i].Replace("data:image/Jpeg;base64,", ""));
pageImage = new MagickImage(newBytes);
imagecoll.Add(pageImage);
}
return imagecoll.ToByteArray();//The problem occurs right here
}
}
}
但我只得到第一页!。
这是我用来将图像写入磁盘的行:
Image.FromStream(new MemoryStream(result)).Save(path, System.Drawing.Imaging.ImageFormat.Tiff);
我试图在 Whosebug 上挖掘类似的东西,但我没有运气。显然对 MagickImage.NET 库没有很好的支持。如果你发现这个方法没用,除了这个和 this one
还有什么其他可用的方法这行似乎有问题return imagecoll.ToByteArray();
,图像是一个多页的tiff图像,返回的字节数组的长度与添加到tiff图像中的图像相比太小了。我找到了一个重载方法,它使用 MagickFormat
枚举将图像格式作为参数,在我的例子中我使用了 MagickFormat.Tiff
.