kolek
kolek

Reputation: 699

A generic error occurred in GDI+ on Bitmap Save - Azure

I get "System.Runtime.InteropServices.ExternalException: 'A generic error occurred in GDI+.' (on save) " when running

private void SaveImage(string base64, string imageName)
{
    using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64)))
    {
        using (Bitmap bm2 = new Bitmap(ms))
        {
            bm2.Save(imageName, ImageFormat.Png);
        }
    }
}

on azure. The base64 strig is correct since on https://base64.guru/converter/decode/image it's decoding fine. I'm running it on azure functions.

Upvotes: 0

Views: 61

Answers (1)

kolek
kolek

Reputation: 699

The problem was the path. so the solution is to use Path.Combine and Path.GetTempPath


private void SaveImage(string base64, string imageName)
{
    using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64)))
    {
        using (Bitmap bm2 = new Bitmap(ms))
        {
            bm2.Save(imageName, ImageFormat.Png);
        }
    }
}

var fileName = ...
var filePath = Path.Combine(Path.GetTempPath(), fileName);
SaveImage(base64, filePath);

Upvotes: 0

Related Questions