1

I have a Dot Net Frameworks code that gets a base64 string from an image.

private string ImageToBase64(string value)
    {
        try
        {
            using (Image image = Image.FromFile(value))
            {
                using (MemoryStream m = new MemoryStream())
                {
                    image.Save(m, image.RawFormat);
                    byte[] imageBytes = m.ToArray();

                    // Convert byte[] to Base64 String
                    return $"data:image/jpeg;base64,{Convert.ToBase64String(imageBytes)}";
                }
            }
        }            
        catch(Exception)
        {
            return "";
        }
    }

I'm using .Net 6, so I added the following dll: dotnet add package System.Drawing.Common - version 6.0.0 But the compiler says the code is for Windows only, I would like to know if there is a way to make this code cross-platform?

  • [This](https://stackoverflow.com/a/21325711/11186444) may answer your question for your case (from image to base64) – Burhan Savci Dec 24 '21 at 17:36
  • https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only#recommended-action – Hans Passant Dec 24 '21 at 18:40

0 Answers0