简体   繁体   中英

Is it possible to create a base64 string which has all frames of a multi page tiff file?

Converting a multi page tiff file to base64 string by using known conversion methods seems to contain just a single page of it.

I'm getting the multi page tiff file from local disk:

Image multiPageImage = Image.FromFile(fileName);

Converting it to base64 string:

base64string = ImageToBase64(multiPageImage, ImageFormat.Tiff);

public static string ImageToBase64(Image image, ImageFormat format)
{
    using (MemoryStream ms = new MemoryStream())
    {
        // Convert Image to byte[]
        image.Save(ms, format);
        byte[] imageBytes = ms.ToArray();

        // Convert byte[] to Base64 String
        string base64String = Convert.ToBase64String(imageBytes);

        image.Dispose();

        return base64String;
    }  
}

Then converting base64 to image back and saving it on the local disk to control the result:

public static Image ConvertBase64ToImage(string base64string)
{
    byte[] bytes = Convert.FromBase64String(base64string);

    Image image;

    using (MemoryStream ms = new MemoryStream(bytes))
    {
        image = Image.FromStream(ms);

        image.Save(@"C:\newTiff.tiff", ImageFormat.Tiff);
    }

    return image;
}

But result image has only single frame. That's why I'm asking if it is possible to have all frames in base64 string?

You are doing lot of unnecessary stuff just for reading a file and write it back to disk.

You can read all the content of file like this

var data = File.ReadAllBytes("image.tiff")

and then use Convert.ToBase64String(data) to convert it to a base 64 string.

var data = File.ReadAllBytes("image.tiff");
var result = Convert.ToBase64String(data);

then you can convert it back to it's byte representation and save it to disk.

var bytes = Convert.FromBase64String(result);
File.WriteAllBytes("image2.tiff", bytes);

File.ReadAllBytes()
Convert.ToBase64String()

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