0
public System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
{
    System.Drawing.Image returnImage = null;

    try
    {
        MemoryStream ms = new MemoryStream(byteArrayIn);
        returnImage = System.Drawing.Image.FromStream(ms); // parameter is invalid

    }
    catch (Exception ex)
    {
        string a = ex.ToString();
        // Response.Write("sfdsfn");
    }

    return returnImage;
}

I did lot of search in Net but i cant get any useful answer for me? any help..thanks in advance

2
  • Given that your question is about ASP.NET, how do you intend to use your Image? You cannot simply put it in an <asp:Image> element, if that's what you were thinking, and the alternative use I can come up with does not require a conversion to System.Drawing.Image. What is your end goal? Commented Nov 10, 2012 at 8:40
  • 1
    What do you mean by parameter is invalid? Can you post the exception you are getting? Commented Nov 10, 2012 at 10:48

3 Answers 3

3

Simple way Image.FromStream:

public Image byteArrayToImage(byte[] imgBytes)
{
    using (MemoryStream imgStream = new MemoryStream(imgBytes))
    {
        return Image.FromStream(imgStream);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use new Bitmap(ms):

Image returnImage = null;
MemoryStream ms = new MemoryStream(byteArrayIn);
returnImage = new Bitmap(ms);

Similarly, you can use Bitmap.FromStream (oddly, I can't find documentation for it).

3 Comments

It's Image.FromStream, but Bitmap.FromStream will work as Bitmap derives from Image.
@hvd - good point. Still, I expect to find it on Bitmap Methods - there are other inherited members. Oh well. Anyway, that also suggests the OP's code should work.
Inherited instance members are listed, but I think it depends on the language whether static members of base classes are accessible in the derived class, and the documentation is for .NET, not C# specifically. I do agree that the current situation, where Bitmap.FromStream compiles but is hard to look up, leaves room for improvement. :)
-1

Took me 2 minutes to find this one :

ImageConverter ic = new ImageConverter();
Image img = (Image)ic.ConvertFrom(byteArray);

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.