Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a base64 string as an image into a folder on server using C# and Web Api

I am posting a Base64 string via Ajax to my Web Api controller. Code below

Code for converting string to Image

public static Image Base64ToImage(string base64String)
{
    // Convert base 64 string to byte[]
    byte[] imageBytes = Convert.FromBase64String(base64String);
    // Convert byte[] to Image
    using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
    {
        Image image = Image.FromStream(ms, true);
        return image;
    }
}

Controller Code

public bool SaveImage(string ImgStr, string ImgName)
{
    Image image = SAWHelpers.Base64ToImage(ImgStr);
    String path = HttpContext.Current.Server.MapPath("~/ImageStorage"); //Path

    //Check if directory exist
    if (!System.IO.Directory.Exists(path))
    {
        System.IO.Directory.CreateDirectory(path); //Create directory if it doesn't exist
    }

    string imageName = ImgName + ".jpg";

    //set the image path
    string imgPath = Path.Combine(path, imageName);

    image.Save(imgPath, System.Drawing.Imaging.ImageFormat.Jpeg);

    return true;
}

This is always failing with a generic GDI+ error. What am I missing ? Is there a better way to save a specific string as an image on a folder ?

like image 523
w2olves Avatar asked Sep 08 '16 14:09

w2olves


People also ask

How do you convert Base64 to blob?

const base64Response = await fetch(`data:image/jpeg;base64,${base64Data}`); Next, convert the response to a blob: const blob = await base64Response. blob();

Can we save Base64 string to database?

Generally no. Base64 will occupy more space than necessary to store those images. Depending on the size of your images and how frequently they're accessed, it is generally not an optimal stress to put on the database. You should store them on the file system or in something like Azure blob storage.

Is Base64 same as blob?

2 Answers. base64 encoding: A binary-to-text encoding scheme whereby an arbitrary sequence of bytes is converted to a sequence of printable ASCII characters, as described in RFC4648. binary large object (BLOB): A discrete packet of data that is stored in a database and is treated as a sequence of uninterpreted bytes.


1 Answers

In Base64 string You have all bytes of image. You don't need create Image object. All what you need is decode from Base64 and save this bytes as file.

Example

public bool SaveImage(string ImgStr, string ImgName)
{       
    String path = HttpContext.Current.Server.MapPath("~/ImageStorage"); //Path

    //Check if directory exist
    if (!System.IO.Directory.Exists(path))
    {
        System.IO.Directory.CreateDirectory(path); //Create directory if it doesn't exist
    }

    string imageName = ImgName + ".jpg";

    //set the image path
    string imgPath = Path.Combine(path, imageName);

    byte[] imageBytes = Convert.FromBase64String(ImgStr);

    File.WriteAllBytes(imgPath, imageBytes);

    return true;
}
like image 76
BWA Avatar answered Sep 18 '22 13:09

BWA