4

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.

4
  • 1
    Your Image class is totally unrelated to the System.Drawing.Image class. Commented Dec 29, 2021 at 10:58
  • You are confused betweeen System.Drawing.Image <- Generic Type for dealing with (let's say) bitmap data, 'Repository.Entities.Image <- your App specific data structure (without any place to hold a Image data inside, only a Url) So you can´t do it Commented Dec 29, 2021 at 10:59
  • It is unclear what you're trying to achieve. Why are you looking into System.Drawing anyway? Commented Dec 29, 2021 at 11:11
  • 1
    Apologies if my question is rather silly and unclear. I'm confused betweeen System.Drawing.Image and Repository.Entities.Image . The answer below can help me understand and solve this problem. Commented Dec 29, 2021 at 11:58

1 Answer 1

3

In your ByteArrayToImage function definition you are saying that you will return a class instance of type Repository.Entities.Image but within the function body you are returning a class instance of type System.Drawing.Image.

You can add another property to your Repository.Entities.Image as System.Drawing.Image and set it within the function:

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; }
    public System.Drawing.Image MyImage{get; set; }
}

public Repository.Entities.Image ByteArrayToImage(byte[] byteArrayIn)
{
    MemoryStream ms = new MemoryStream(byteArrayIn);
    System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms); 
    return new Repository.Entities.Image {
        MyImage= returnImage
    }
}

Now you can use .MyImage property (which is of type System.Drawing.Image) of your own Image class (which if of type Repository.Entities.Image) in your code.

Additionally, I always prefer to name my classes different than predefined classes (such as Image, Document, Path...etc). If you are planning to have conflicting names, it is a good practice to use full namespaces to prevent confusion of classes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.