简体   繁体   中英

Convert List of Images to Single Tiff File Using MagickImage.NET

I'm using the following code to create single tiff file from a list of images using MagickImage.NET Library :

    /// <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
            }
        }
    }

But I'm getting the first page only!.

Here is the line I used to write the image on disk:

Image.FromStream(new MemoryStream(result)).Save(path, System.Drawing.Imaging.ImageFormat.Tiff);

I tried to dig stackoverflow for something similar but I had no luck. Apparently there is no good support for MagickImage.NET Library. If you see this method is useless what are the other available methods beside this and this one

It seems like there is a problem with this line return imagecoll.ToByteArray(); , the image is a multipage tiff image and the length of the byte array returned is too small compared with the images being added to the tiff image. I found an overloaded method which takes image format as a parameter using MagickFormat enum, in my case I used MagickFormat.Tiff .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM