Im trying to display image binary data but its not working.
I have the code below but on the line:

newimage = System.Drawing.Image.FromStream(ms)

i keep getting the error 'Parameter is not valid'

Dim obj As Byte()
        obj = StringToByteArray(ImageString)

        Dim ms As New MemoryStream(obj)
        Dim newimage As System.Drawing.Image

        newimage = System.Drawing.Image.FromStream(ms)

        Response.ContentType = "image/jpeg"
        newimage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
7 Years
Discussion Span
Last Post by ninjaimp

try specifying the size of the memory stream, are you also sure stringtobytearray is doing what it should?

Dim ms As New MemoryStream(obj, 0, obj.Length)

the function is below:

Public Shared Function StringToByteArray(ByVal input As String) As Byte()
        Return Encoding.Unicode.GetBytes(String.Empty + input)
    End Function

the rest of my code is slightly different also, building in your thoughts of defining the size of the stream and it is still the same error!

obj = StringToByteArray(ImageString)
        Dim ms As New MemoryStream(obj)
        ms.Write(obj, 0, obj.Length)

        ms.Position = 0
        Dim newimage As New System.Drawing.Bitmap(ms)

        Response.ContentType = "image/jpeg"
        newimage.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)



try this then, c# but i'm sure you can convert it to vb

ImageConverter imageConverter = new System.Drawing.ImageConverter();
Image image = imageConverter.ConvertFrom(obj) as Image;

if that doesn't work, i'm wondering if its an encoding issue

try swapping out your string to byte array with this

Public Shared Function StringToByteArray(intput As String) As Byte()
   Dim encoding As New System.Text.ASCIIEncoding()
   Return encoding.GetBytes(input)
End Function

try to save the byte array to a file rather than image first, and see if you can open that file up properly

'Get our byte array
obj = StringToByteArray(ImageString)

'Save to a file
Dim oFileStream As System.IO.FileStream
 oFileStream = New System.IO.FileStream("image.jpg", System.IO.FileMode.Create)
 oFileStream.Write(obj, 0, obj.Length)

it saved the image but when trying to view it in windows picture viewer it sqays 'no preview available'

Could it just be that the binary info im pulling in is duff - can it be checked in any way?

Your help is greatly appreciated on this by the way


im afraid im getting a little bit lost with this....

The binary data, as i see it and as it is stored in the export file does not contain any header info but i would like to compare it to another images binary data but everytime i export binary data out of an sql express table it only has the value system byte[] -

do you know how to retrive the actual value so i can use it for testing?

many thanks


we have been provided with a data dump of a postgre sql table into a .psv doc

the data holds informatin on images and the actual image is stored as binary. Now when you open this doc to look at the information i want to see how it compared to that of the data that would be stored in an sql image field.

the reason beimng in that the rouine i have that should open the image from the binary data and save it is failing in the previous errors i have reported and i can only put it down to how the data has been passed to us?

if that makes any sense at all


why can't you just do an import into sql server without going code side?

also are you sure you mean a psv or csv?


yes, its a pipe seperated value list

We have gone that route as we are still not sure if the data is correct. Would opening the data as text change the way that the binary information is displayed?


opening it up wouldn't do anything, as long as you don't save it

have you tried to import inside management studio specifying the pipe, or doing something like this?

    FROM 'c:\mydata.psv' 
        FIELDTERMINATOR = '|', 
        ROWTERMINATOR = '\n' 

im running visual studio 2008 pro - is the management tool included with that?


i've found out the reason - the data i have is in standard escape format and of course i need to convert this back to its binary equivilent.

Do you know of any built in methods that will take this text and convert it back to binary in either vb or c .net


the management tool you need for the import is just sql management studio or even run it through code
did you try the bulk insert?

escape format meaning?


Dont have access to SQL server at the moment and not sure if it would work with the sql express?

the data that has been provided is a table dump of a postgre sql table and the format is plain and uses standard escape sequence similiar to c or perl.

Now i need to convert that back to its binary equivilent.


are you meaning all your double backslashes?

just run a replace on your escaped characters

Dim fileAsString As String = myString.Replace("\\", "\")

i think this is how with vb, more familiar with csharp


not just the back slashes cos as i believ all the numbers after them mean something too. I may have to completely re think this!!!


this is a small segment form the begginning

\\377\\330\\377\\340\\000\\020JFIF\\000\\001\\002\\001\\001,\\001,\\000\\000\\377\\342\\034mICC_PROFILE\\000\\001\\001\\000\\000\\034]Lino\\002\\020\\000\\000mntrRGB XYZ \\007\\316\\000\\002\\000\\011\\000\\006\\0001\\000\\000acspMSFT\\000\\000\\000\\000IEC sRGB\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\366\\326\\000\\001\\000\\000\\000\\000\\323-HP \\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\000\\021cprt\\000\\000\\001P\\000\\000\\0003desc\\000\\000\\001\\203\\000\\000\\000lwtpt\\000\\000\\001\\357\\000\\000\\000\\024bkpt\\000\\000\\002\\003\\000\\000\\000\\024rXYZ\\000\\000\\002\\027\\000\\000\\000\\024gXYZ\\000\\000\\002+\\000\\000\\000\\024bXYZ\\000\\000\\002?\\000\\000\\000\\024dmnd\\000\\000\\002S\\000\\000\\000pdmdd\\000\\000\\002\\303\\000\\000\\000\\210vued\\000\\000\\003K\\000\\000\\000\\206view\\000\\000\\003\\321\\000\\000\\000$lumi\\000\\000\\003\\365\\000\\

the image field is made up of this - this is what is called a standard escape sequence as si believe and there are apparantley ways to get it converted back to its original binary form!


this gave me a glimmer of hope but it still boils down do the data is not even in bytea form. maybe it was when strored in the db but when exported from the db to the psv file it converst it to standard escape sequence. Not sure if there is anything i can do - will have to go back to them and see what we can sort out

many thanks for your all time and effort spent on this it is very much appreciated!


very interesting - it looks like the postgreSQL server parses it and converts it back to its bytea - its that process that removes all the backslashes and converts all the digits back to their octet value, or so i understand! this may be a bit difficult to do it without postgresql!!!

thanks for this


could be done without it, i think you would have to parse and eliminate the extra escape characters then convert it to a byte array using encoding getbytes

i might be wrong, but just a shot


yes but its knowing which are the escape characters and what the octet conversion for numbers 0 through 255 are!

This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.