3

I want to convert the application/octet-stream base 64 string into image in c#. Please help me with this conversion.

Here is the string: ERQVHykzPkpWY3F/jp2tvMnU3ubs8fX5+/z8/Pr39PDq5d/Y0cvCu7Oro5uTjIR9dm9pY11ZVFBMSUZEQT47OTY0MjAuLSopJyUjIR8eHBsZGRcWFRUUExMSEREQDw8ODg4ODg0NDAwLCwoKCgoKCwsLDA0NDQ4ODg4ODw8QERI=

Kit
  • 20,354
  • 4
  • 60
  • 103
Pravin G
  • 31
  • 1
  • 1
  • 5

1 Answers1

3

Your base64 string contains an encoded binary file. The MIME type is application/octet-stream for a binary file. You'll only be able to convert base64 strings of image/octet-stream MIME type files.

Having said that, you can onvert your base64 string of a image/octet-stream MIME type file into a bytes array and then use a MemoryStream to compose an image from it.

using System.Drawing;

public static Image LoadBase64(string base64)
{
    byte[] bytes = Convert.FromBase64String(base64);
    MemoryStream ms = new MemoryStream(bytes)
    Image image = Image.FromStream(ms);
    
    return image;
}

Usage

Image myImage = LoadBase64(base64string);

Alternative

You could use the ImageConverter class which implements the ConvertFrom() method. It allows you to convert a specified object to an image.

public Image byteArrayToImage(byte[] byteArray)
{
    System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
    Image image = (Image)converter.ConvertFrom(byteArray);

    return image;
}
ɐsɹǝʌ ǝɔıʌ
  • 4,440
  • 3
  • 35
  • 56
  • Parameter is not valid. errror is throwing on this line image = Image.FromStream(ms); – Pravin G Jun 07 '21 at 12:03
  • That's why `FromStream()` is expecting data in one of these formats: BMP GIF JPEG PNG TIFF. I suspect that your byte array is not in these and does not have the expected metadata or compression information. Did you try to convert that string by using any [online tool](https://base64.guru/converter/decode/image)? – ɐsɹǝʌ ǝɔıʌ Jun 07 '21 at 13:05
  • Alternatively, you might want to try the `ImageConverter.ConvertFrom()` method. Answer edited – ɐsɹǝʌ ǝɔıʌ Jun 07 '21 at 13:14
  • Hi, Online tool is not able to convert... how to conver now using ImageConverter.ConvertFrom() is there any example ? – Pravin G Jun 07 '21 at 13:25
  • I tried to convert your string and I'm sorry to say you that this is not a valid base64 string for an image. MIME of yours is `application/octet-stream` used for binary files. To be able to convert a base64 string to an image the MIME type should be `image/octet-stream` – ɐsɹǝʌ ǝɔıʌ Jun 07 '21 at 13:40
  • 1
    @ɐsɹǝʌǝɔıʌ In `LoadBase64` method, you shouldn't create the `MemoryStream` in a `using` block. The stream should not be disposed as long as the image is needed. Another way, return a `.Clone();` with the proper cast. – dr.null Jun 07 '21 at 14:51
  • @dr.null Why? The MemoryStream is no longer needed once it's done. At this point the data is fully contained into the Image object. Further, MemoryStream implements IDisposable. When programming in C# you should dispose properly those objects that implements IDisposable by wrapping it into using blocks because they could change its underlying implementation at any time. This way you reduce maintenance issues in the future. – ɐsɹǝʌ ǝɔıʌ Jun 07 '21 at 20:04
  • No. not in this context. the stream here is the _soul_ of the image. Please read the [Remarks](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.image.fromstream?view=net-5.0#System_Drawing_Image_FromStream_System_IO_Stream_). Consider this: `public static Image LoadBase64(string base64) => Image.FromStream(new MemoryStream(Convert.FromBase64String(base64)));`. Disposing the image when it is no longer needed is the right way. Second opinion is [here](https://stackoverflow.com/a/32818491/14171304). – dr.null Jun 07 '21 at 22:06
  • [Third Opinion](https://stackoverflow.com/a/336396/14171304). – dr.null Jun 07 '21 at 22:10
  • 1
    @dr.null You have convinced me. You're right. Thanks for teaching me something that I didn't know. – ɐsɹǝʌ ǝɔıʌ Jun 08 '21 at 11:44