【问题标题】:Convert List of Images to Single Tiff File Using MagickImage.NET使用 MagickImage.NET 将图像列表转换为单个 Tiff 文件
【发布时间】:2016-11-17 09:10:24
【问题描述】:

我正在使用以下代码从使用 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);

我试图在 stackoverflow 上挖掘类似的东西,但我没有运气。显然对 MagickImage.NET 库没有很好的支持。如果你看到这个方法没用,除了这个和this one

之外还有什么其他可用的方法

【问题讨论】:

    标签: c# .net imagemagick tiff


    【解决方案1】:

    这行return imagecoll.ToByteArray(); 似乎有问题,图像是多页 tiff 图像,与添加到 tiff 图像的图像相比,返回的字节数组的长度太小。我发现了一个重载方法,它使用MagickFormat枚举将图像格式作为参数,在我的例子中我使用MagickFormat.Tiff

    【讨论】:

    • 当您不指定格式时,它将使用原始图像的格式,即 JPEG,并且该格式不支持多层。这就是为什么你只得到第一张图像。 (ps 可以直接将字节数组写入磁盘)
    猜你喜欢
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多