System.Drawing.Image.FromStream and the “Parameter is not valid.” argument exception (Alternate Title: Testing the reconstitution of images from byte arrays)

This is my first post to contain VB.NET sample code (I am working in VB.NET at work these days).
I struggled with this for quite some time. I figured out that, when you have an empty byte array, that the
Image.FromStream method will throw an ArgumentException stating "Parameter is not valid.". The following code illustrates successful creation of an in-memory image, converting it to a byte array, and reconstituting it into an image.

<Test()> _
Public Sub ShouldBeAbleToReconstituteImage()
     Dim imageArray As Byte() = CreateDummyImageByteArray()
     Dim reconstitutedImage As Image
          Using stream As MemoryStream = New MemoryStream(imageArray, 0, imageArray.Length)
               stream.Write(imageArray, 0, imageArray.Length)
               reconstitutedImage = Image.FromStream(stream)
          End Using
     Catch argumentException As ArgumentException
          Assert.Fail(String.Format("Unable to reconstitute image. The following exception occurred: ‘{0}’", argumentException.Message))
     End Try
End Sub

Public Shared Function CreateDummyImageByteArray() As Byte()
    Dim imageArray As Byte()
    Dim originalImage As Image = New Bitmap(1, 1)
    Using stream As MemoryStream = New MemoryStream()
        originalImage.Save(stream, ImageFormat.Jpeg)
        imageArray = stream.ToArray
    End Using
    Return imageArray
End Function


13 thoughts on “System.Drawing.Image.FromStream and the “Parameter is not valid.” argument exception (Alternate Title: Testing the reconstitution of images from byte arrays)

  1. Thank You …

  2. Aleluia!!!!Thank you so much… xD

  3. thank u so much from YEMEN

  4. Juan Carlos

    Thanks for your post!!!

  5. One of my image was corrupted, so i tried the above code. Instead of recovering the image, the code produces 1×1 jpg image.

    Can you help me in this issue. I want to get that image back.

    • The code above won’t help you to recover your image. It sounds like you’re in an unfortunate situation where you need to recover your image from backup which I suspect you would have already done if you had a backup. Unfortunately it may be the case where you’re going to need to recreate your image.

  6. This saved me some time. Thank you. Image manipulation with GDI+ & .NET can be tricky at times. The wierd thing with this error is, the same line of code works for most TIFs that I am processing but randomly fails on some.

  7. Hi… I am just wondering how can I call the ShouldBeAbleToReconstituteImage()
    and another question is how to I replace the value of the imageformat.jpeg in
    originalImage.Save(stream, ImageFormat.Jpeg)
    when am reading from the database.

  8. I would use a test runner like to execute the method.

  9. Just wondering what would you recomment for the Thanks.

  10. Anyone guide me how it handle in c# code???
    plz urgent.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s