-2

Now i'm using .net for the conecction with database(sql server 2008), there the images of all product has saved in a attribute type "Image" and in c# get the data in type byte[]. I searched for a answer, but the only solution for this, is this code, but it dont function maybe the type of data is deprecated in the news versions of c#, i dont know.

using static System.Net.Mime.MediaTypeNames;
using System.IO;

public static Image ImageFromByteArray(byte[] bytes)
{
    using (MemoryStream ms = new MemoryStream(bytes))
    using (Image image = Image.FromStream(ms, true, true))
    {
        return (Image)image.Clone();
    }
}
NineBerry
  • 26,306
  • 3
  • 62
  • 93
  • 4
    Q: What exactly is "wrong"? Q: Do you get an error message? Q: Can you examine the bytes in your byte[] input, and compare them to the bytes in your database? Q: Can you save the bytes to disk, and try a) inspecting the bytes, and/or b) viewing the image? – FoggyDay Nov 29 '19 at 21:21
  • The problem is not the type of matrix I am using, the problem is that the data type "Image" does not seem to work, and I have not found another way to do it without using any function of that class. the mistake that doesn't allow me to use that class is "Image does not contain a definiton for 'FromStream'" – Dayana Fulla Nov 29 '19 at 21:49
  • 1
    "Does not work" has never been a sufficient problem description. You need to give us *something* to work with here. – Christopher Nov 29 '19 at 21:52
  • Sorry Christopher, just i want to know if there are other way to do – Dayana Fulla Nov 29 '19 at 22:06

1 Answers1

1

Replace

using static System.Net.Mime.MediaTypeNames;

with

using System.Drawing;

so that you use the Image type from System.Drawing.

If the namespace cannot be found, make sure the nuget package System.Drawing.Common is installed in the project.

NineBerry
  • 26,306
  • 3
  • 62
  • 93