I want to convert byte array to image. I searched a lot of posts in StackOverFlow and found the code below.
public Image ByteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms); // <-- Error here
return returnImage;
}
But I get an error in Image.FromStream is 'Image' does not contain a definition for 'FromStream' and couldn't find any info on how to fix this.
There is a user who had the same error as me more than 9 years ago but still no answer.
I found a way is:
public Image ByteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = System.Drawing.Image.FromStream(ms); // <-- Add System.Drawing. here
return returnImage;
}
And a way:
public Image ByteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms); // <-- Add System.Drawing. here
return returnImage; // <-- Error here
}
But both ways gives a new error as: Cannot implicitly convert type 'System.Drawing.Image' to 'Repository.Entities.Image'.
This is my Image file in Repository.Entities:
public partial class Image
{
public int Id { get; set; }
public int? IdObj { get; set; }
public string Url { get; set; }
public sbyte? Thumbnail { get; set; }
public string Type { get; set; }
}
How to fix it? Looking forward to receiving an answer.
Imageclass is totally unrelated to theSystem.Drawing.Imageclass.