Retrieved GIF image from SNMP Device and its not valid, why? (Fix my GIF please!)

Posted on 2005-05-13
Last Modified: 2010-04-23
I am retrieving the contents of the display panel of a printer in the form of a GIF image.
(More information here )

I am able to retrieve the OID and using GetNext, obtain the remaining chunks of the image in this tree.  I used StreamWriter to directly write the values to a file (each SNMP octet stream is directly written to the filestream)  The first 480 bytes came in the first GET and then an additional 5 bytes in the second GET

But for some reason the image isn't valid.  I looked at the output in a hex editor and there are 4 bytes _before_ the "GIF89a" header.  I tried removing the first 4 bytes of the file but no luck.
The invalid GIF can be found here: << Try saving it locally and looking at it in a hex editor.  

  Does anyone know what's wrong with this GIF?
If you figure out what's wrong with the file, I need to know what portion of the file was changed to make it valid.
Question by:masoncooper
    LVL 96

    Expert Comment

    by:Bob Learned
    How did you write the file?  Did you wait for both parts before you wrote the file to disk?

    LVL 2

    Author Comment

    Here is the code I used to retrieve the values:

    Dim selector As String = ""

                Dim response As SnmpResponse = snmp.GetNext(selector, 3)

                Dim sw As StreamWriter

                sw = New StreamWriter("C:\test.gif")

                While response.ErrorStatus = 0
                    Dim oid As String = response.VariableBindings(0).OID.ToString

                    If selector.Length > oid.Length Or selector <> oid.Substring(0, selector.Length) Then
                        Exit While
                    End If


                    Console.WriteLine("Bytes Written: " & response.VariableBindings(0).Value.Size)
                    response = snmp.GetNext(response.VariableBindings(0).OID.ToString(), 3)

                End While
    LVL 2

    Author Comment

    Ok, I've made some progress here, contrary to what I said earlier, If I delete the leading 4 bytes from the file, it displays in Internet Explorer, minus a tiny sliver (see )

    But the file still isn't valid.  If I try and do an Image.FromFile in VB.Net, it throws an exception "Out of Memory", my guess is that it still isn't a properly formed GIF
    LVL 96

    Expert Comment

    by:Bob Learned
    Is it my imagination, or does that function return an octet string?  I would try using a BinaryWriter, instead of StreamWriter, and write the bytes to the file.

    LVL 2

    Author Comment

    TheLearnedOne, Thanks for the tip, I forgot about that, but yes, the file is still writing the same even after using the binarywriter (my guess is that its because the SNMP object is using the .baseStream object to write the data)

    In the meantime, I _did_ come up with a perfectly valid GIF.
    If you look at the image test2.gif (mentioned in the comments), and remove the leading 4 bytes, then remove 2 bytes that are 5 bytes from the end, they should be 0x04 0x05 and then the file is valid!  What strikes my attention is that the chunks were 480 bytes and 5 bytes, leading me to believe that the first two bytes of the second chunk were garbage.  As far as I can tell, its not so, because applying that theory to other images obtained from this process do not yield the same result.

    Here is another image, this one also came in two chunks but I had it write the streams out as two separate chunks IN ADDITION to the original portion that writes it all as one:
    First Chunk:
    Second Chunk:
    Product of both chunks:
    I wanted to be certian that the streams were being written properly and that no additional data was being appended.
    LVL 2

    Accepted Solution

    Closed, 500 points refunded.
    Friendly Neighbourhood Community Support Admin

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now