Ibrahim Amer Ibrahim Amer - 21 days ago 6
C# Question

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

Answer

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 usedMagickFormat.Tiff.