【问题标题】:C# - Converting multi page tiff file to base64 string and converting back results with single imageC# - 将多页 tiff 文件转换为 base64 字符串并使用单个图像转换回结果
【发布时间】:2015-01-29 12:28:47
【问题描述】:

我正在从本地磁盘打开一个 tiff 文件:

Image multiPageImage = Image.FromFile(fileName);

然后将其发送到转换方法:

base64string = ImageToBase64(multiPageImage, ImageFormat.Tiff);

public static string ImageToBase64(Image image, System.Drawing.Imaging.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);
                return base64String;
            }  
        }

最后,我使用以下代码将我的 base64 转换为 tiff 文件:

public static void ConvertBase64ToTiff(string base64string)
    {
        Byte[] bitmapData = new Byte[base64string.Length];

        bitmapData = Convert.FromBase64String(FixBase64ForImage(base64string));

        using (MemoryStream streamBitmap = new System.IO.MemoryStream(bitmapData))
        {
            Bitmap bitImage = new Bitmap((Bitmap)Image.FromStream(streamBitmap));
            bitImage.Save(@"C:\myTiff.tiff");
        }            
    }

    public static string FixBase64ForImage(string base64string)
    {
        System.Text.StringBuilder sbText = new System.Text.StringBuilder(base64string, base64string.Length);

        sbText.Replace("\r\n", String.Empty);
        sbText.Replace(" ", String.Empty);

        return sbText.ToString();
    }

那个修复方法不是我的。其实我不知道这是否正确。但我到处搜索并尝试我找到的东西。

我的测试表明 base64 字符串只有一个图像。

任何帮助将不胜感激!

【问题讨论】:

    标签: c# base64 tiff


    【解决方案1】:

    对于将多页 tiff 文件转换为 base64 或反之亦然有问题的人,您可以参考该链接:

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

    也许这不是完全相同的问题,但答案也会解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-07
      • 2014-07-21
      • 2014-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多