954,577 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Parameter is no valid

Hi

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)
ninjaimp
Junior Poster
130 posts since Apr 2008
Reputation Points: 22
Solved Threads: 9
 

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)
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

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)


thanks

ninjaimp
Junior Poster
130 posts since Apr 2008
Reputation Points: 22
Solved Threads: 9
 

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
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

still giving the same error

i think it could be becuase the image binary information im importing is wrong/duff - its from a dump of an postgreSQL table image field.

I have started another thread trying to find out how to chekc if it is valid here http://www.daniweb.com/forums/thread185639.html

ninjaimp
Junior Poster
130 posts since Apr 2008
Reputation Points: 22
Solved Threads: 9
 

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)
oFileStream.Close()
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

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

ninjaimp
Junior Poster
130 posts since Apr 2008
Reputation Points: 22
Solved Threads: 9
 

this here in your file, shows that it is supposed to be a jpeg
\\377\\330\\377\\340

here's a c# sample to check a stream
http://stackoverflow.com/questions/210650/validate-image-from-file-in-c

dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

it is/was a jpeg - ill give that page a look now - many thanks

ninjaimp
Junior Poster
130 posts since Apr 2008
Reputation Points: 22
Solved Threads: 9
 

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

ninjaimp
Junior Poster
130 posts since Apr 2008
Reputation Points: 22
Solved Threads: 9
 

i'm not sure what you are meaning

the actual value? of what?
and what are you wanting to use for testing?

dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

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

ninjaimp
Junior Poster
130 posts since Apr 2008
Reputation Points: 22
Solved Threads: 9
 

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?

dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

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?

ninjaimp
Junior Poster
130 posts since Apr 2008
Reputation Points: 22
Solved Threads: 9
 

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?

BULK INSERT MyTableName
    FROM 'c:\mydata.psv' 
    WITH 
    ( 
        FIELDTERMINATOR = '|', 
        ROWTERMINATOR = '\n' 
    )
dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

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

ninjaimp
Junior Poster
130 posts since Apr 2008
Reputation Points: 22
Solved Threads: 9
 

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

ninjaimp
Junior Poster
130 posts since Apr 2008
Reputation Points: 22
Solved Threads: 9
 

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?

dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

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.

ninjaimp
Junior Poster
130 posts since Apr 2008
Reputation Points: 22
Solved Threads: 9
 

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

dickersonka
Veteran Poster
1,175 posts since Aug 2008
Reputation Points: 130
Solved Threads: 143
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You