I am trying to save an image into my database using the following method
public string ImageToBase64(Image image, ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
image.Save(ms, format);
byte[] imageBytes = ms.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
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 am saving it into the follow object,
public class MediaImage
{
[Key]
public Guid Id { get; set; }
[Column(TypeName = "varchar(MAX)")]
public string Image { get; set; }
public string ContentType { get; set; }
}
When I step through it the Base64ToImage is working (I am able to copy/paste the string back into the image).
However when I save the MediaImage object to the database the Image field is NULL even though it had a value prior to saving into the database.
As far as I can see there is no exception being thrown. I am not sure if my data type is incorrect?