简体   繁体   中英

C# - Converting multi page tiff file to base64 string and converting back results with single image

I'm opening a tiff file from local disk:

Image multiPageImage = Image.FromFile(fileName);

Then sending it to conversion method:

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

Finally I convert my base64 to tiff file with the below code:

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

That fixing method is not mine. Actually I don't know if it is the right way or not. But I searched everwhere and trying what I find.

My tests showed that base64 string has just a single image.

Any help would be greatly appreciated!

For people who have a problem about converting multi page tiff file to base64 and vice versa you can refer to that link:

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

Maybe it is not totally the same question but the answer will solve this one too.

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