是否有可能创建一个具有多页TIFF文件的所有帧一个base64字符串? [英] Is it possible to create a base64 string which has all frames of a multi page tiff file?

查看:185
本文介绍了是否有可能创建一个具有多页TIFF文件的所有帧一个base64字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用已知的转换方法转换多页TIFF文件为Base64字符串似乎包含它只是一个单一的页面。

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

我是从本地磁盘越来越多页TIFF文件:

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

Image multiPageImage = Image.FromFile(fileName);

将其转换为Base64字符串:

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

然后转换的base64图像回并保存在本地磁盘上控制结果:

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

但结果图像只有一个帧。这就是为什么我问是否可以把所有帧的base64字符串?

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")

然后用 Convert.ToBase64String(数据)将其转换为一个基地64字符串。

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);

,那么你可以将其转换回它的字节重presentation并将其保存到磁盘。

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);

文件。 ReadAllBytes()结果
Convert.ToBase64String()

这篇关于是否有可能创建一个具有多页TIFF文件的所有帧一个base64字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆