4 Replies - 5516 Views - Last Post: 07 August 2012 - 02:17 AM Rate Topic: -----

#1 ChloeThompson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-June 08

New Bitmap from MemoryStream - Error: Parameter is not Valid

Posted 03 August 2012 - 10:45 AM

I'm receiving this 255,216,255,224,0,16,74,70,73,70,0 as a string in an xml file.
Converting it to a byte array with code at bottom.

I then try to turn it into a bitmap file with this.
        Dim arbyImage As Byte()
        Dim msX As MemoryStream
        Dim bmpOrig As Bitmap
        Dim imgformatOrig As Imaging.ImageFormat
'Other code
            arbyImage = Nothing : msX = Nothing : bmpOrig = Nothing : imgformatOrig = Nothing 
            bolOK = fnStringtoByteArray(strPht, arbyImage)
            If bolOK Then
                Try
                    msX = New MemoryStream(arbyImage)
                Catch ex As Exception
                    clsEH.prAddError("fnSavePhotos - Failed to generate MemoryStream\n" & strSaveTo & strNewFile)
                    Exit Function
                End Try
                msX.Position = 0
                Try
                    bmpOrig = New Bitmap(msX)'*********Fails Here
                Catch ex As Exception
                    clsEH.prAddError("fnSavePhotos - Failed to make bmp from MemoryStream\n" & strSaveTo & strNewFile)
                    Exit Function
                End Try
                Try
                    imgformatOrig = bmpOrig.RawFormat
                Catch ex As Exception
                    clsEH.prAddError("fnSavePhotos - Failed to get Original Format of bmpOrig\n" & strSaveTo & strNewFile)
                    Exit Function
                End Try
                Try
                    bmpOrig.Save(strSaveTo & strNewFile, imgformatOrig)
                Catch ex As Exception
                    clsEH.prAddError("fnSavePhotos - Failed to save photo\n" & strSaveTo & strNewFile)
                    Exit Function
                End Try
            End If


The error I get is System.ArgumentException: Parameter is not valid. at System.Drawing.Bitmap..ctor(Stream stream)

I've made sure the memorystream is at the beginning - msX.Position = 0
I've tried using Image instead of bitmap
(error message changes to at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) - no other change)

'Either
            Dim imgX As Image = Nothing
            imgX = Image.FromStream(msX)
            bmpOrig = New Bitmap(imgX)
'Or
            Dim imgX As Image = Nothing
            imgX = Image.FromStream(New MemoryStream(arbyImage))
            bmpOrig = New Bitmap(imgX)




 Private Function fnStringtoByteArray(ByVal strPhoto As String, ByRef arbyImage As Byte()) As Boolean
        Dim arstrBytes() As String
        Dim intByCnt As Byte
        Dim strByte As String
        fnStringtoByteArray = False
        arstrBytes = Nothing : arbyImage = Nothing
        Try
            arstrBytes = strPhoto.Split(",")
        Catch ex As Exception
            Exit Function
        End Try
        intByCnt = 0
        For Each strByte In arstrBytes
            If IsNumeric(strByte) Then
                If Val(strByte) >= 0 And Val(strByte) <= 255 Then
                    ReDim Preserve arbyImage(intByCnt)
                    arbyImage(intByCnt) = CByte(strByte)
                    intByCnt += 1
                Else
                    Exit Function
                End If
            Else
                Exit Function
            End If
        Next
        fnStringtoByteArray = True
    End Function



Is This A Good Question/Topic? 0
  • +

Replies To: New Bitmap from MemoryStream - Error: Parameter is not Valid

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: New Bitmap from MemoryStream - Error: Parameter is not Valid

Posted 05 August 2012 - 07:22 AM

Quote

I'm receiving this 255,216,255,224,0,16,74,70,73,70,0 as a string in an xml file.


Q1: Is it your code making the XML file?
Q2: As a string? Are you really converting a bitmap to a string before writing your XML file? Why?
Q3: Are those few number the entire bitmap data? Because its not near enough to be anything significant. At best it is just a few pixels:
[255,216,255,224] ,[0,16,74,70] ,73,70,0
it doesn't break down correctly for RGB or RGBA

Let's see your serialization code of the bitmap.

I think you need to just serialize the bitmap as an object.

[*]Q: ... save data, save properties, save environmental variables, serialize my data/class?
A:
Was This Post Helpful? 0
  • +
  • -

#3 ChloeThompson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-June 08

Re: New Bitmap from MemoryStream - Error: Parameter is not Valid

Posted 06 August 2012 - 12:55 AM

I'm afraid I am not creating the XML file, just receiving it, so I'm sorry I can't give you the code which creates the xml file.
I'll have a look at the links you've kindly given me to explore other routes of passing an image through a web service along with other data which the image is tied to.
The point you raised in Q3, questioning whether it's a valid image I'm receiving I'm certainly going to pursue.
Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: New Bitmap from MemoryStream - Error: Parameter is not Valid

Posted 06 August 2012 - 04:27 AM

I'd you didn't make the XML then how do you know what it contains? Is it a uncompressed bitmap or a compressed jpg? Or is it a complex object with a path string, additional information and a bitmap? Is it 'clear' or encrypted?

I'd you don't know the actual contents and the protocol used to make the file then it's a lot of guesswork to reverse engineer it back to usable data.
Was This Post Helpful? 0
  • +
  • -

#5 ChloeThompson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-June 08

Re: New Bitmap from MemoryStream - Error: Parameter is not Valid

Posted 07 August 2012 - 02:17 AM

I kept a copy of it, as its just development data.
Here's an extract from it.
<ARQE><QEID>18</QEID><ARQERs><ARQER><RGID>3</RGID><DT>03 Aug 2012 15:55:54</DT><Pass>0</Pass><FDs><FD><FRID>6</FRID><CAID>11</CAID><Det/><Pht>255,216,255,224,0,16,74,70,73,70,0</Pht></FD></FDs></ARQER></ARQERs></ARQE>
It's not info about the image, its info which the image is about.

I however am going to assume from your line of questioning that there isn't anything obviously wrong with the code which is generating the error, and that the likely problem is in the data, so I'm going back to the person who sent it & wrote the code which produces it querying if there's a problem at his end.

Thank you for your time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1