简体   繁体   English

是否可以创建一个base64字符串,其中包含多页tiff文件的所有帧?

[英]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. 使用已知的转换方法将多页tiff文件转换为base64字符串似乎只包含一个页面。

I'm getting the multi page tiff file from local disk: 我从本地磁盘获取多页tiff文件:

Image multiPageImage = Image.FromFile(fileName);

Converting it to base64 string: 将其转换为base64字符串:

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: 然后将base64转换为图像并将其保存在本地磁盘上以控制结果:

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? 这就是为什么我问是否有可能在base64字符串中包含所有帧?

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. 然后使用Convert.ToBase64String(data)将其转换为基本64字符串。

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() File.ReadAllBytes()
Convert.ToBase64String() Convert.ToBase64String()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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