Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting base64string to Image type. Image type could not be found?

I'm trying to convert a Base64String back to an Image. I have this code set up in my C# console application.

public Image Base64ToImage(string base64String)
{
    // Convert Base64 String to byte[]
    byte[] imageBytes = Convert.FromBase64String(base64String);
    MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);

    // Convert byte[] to Image
    ms.Write(imageBytes, 0, imageBytes.Length);
    Image image = Image.FromStream(ms, true);
    return image;
}

I'm getting an error each time I use the type Image. It says:

The type or namespace name could not be found.

I'm using:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Net.Mime;
using System.Drawing;

Am I missing a library?

like image 711
BearSkyview Avatar asked Nov 29 '25 20:11

BearSkyview


2 Answers

Yes, if you're writing a console application, your project probably won't include a reference to System.Drawing.dll, which is the assembly containing System.Drawing.Image. Just add the assembly reference, and it should be fine.

like image 54
Jon Skeet Avatar answered Dec 02 '25 09:12

Jon Skeet


In the project window, right click on the references, and choose "Add Reference..." In the .NET Framework, choose System.Drawing.dll.

like image 35
Carlos Landeras Avatar answered Dec 02 '25 08:12

Carlos Landeras