Link to home
Start Free TrialLog in
Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Memory stream error

Hello,
I am getting an error 'Parameter is not valid'
   Using ms As New MemoryStream(imageData, 0, imageData.Length)
                            ms.Write(imageData, 0, imageData.Length)
                            Try
                                image = Image.FromStream(ms, True)
                                Dim oldbitmap As Bitmap = (image)
                                clsFrmain.ScaleImage(PicImageFin, oldbitmap)
                                OriginalimageLoaded = oldbitmap
                                PicImageHeight = PicImageFin.Height
                                PicImageWidth = PicImageFin.Width
                            Catch ex As Exception
                            End Try
                        End Using

Open in new window

It usually happens for large files especially PDF.

Any suggestion.

Thanks
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Would be useful to tell us which line fails with that error
Avatar of RIAS

ASKER

Andy:

Thanks :
 image = Image.FromStream(ms, True) ---- Here
>>It usually happens for large files especially PDF.

Erm, Are you certain it is an image you are have in the memory stream?
Avatar of RIAS

ASKER

Andy,
Thanks, its been ages I have been looking for the solution.
I will really appreciate it if you can help.

Thanks
I suspect there isn't a valid image file in the memorystream.
Reset the position of the memory stream before you load it:

Using ms As New MemoryStream(imageData, 0, imageData.Length)
    ms.Write(imageData, 0, imageData.Length)
    ms.Position = 0;

    Try
        image = Image.FromStream(ms, True)

Open in new window

Using ms As New MemoryStream(imageData, 0, imageData.Length)
   ms.Write(imageData, 0, imageData.Length)

Open in new window

This initialized your memory stream and does already filling it. Thus the stream pointer is at the end. So the subsequent and redundant Write is failing.

Thus either drop the redundant write or use an expandable memory stream by using the simple ctor MemoryStream() or MemoryStream(imageData.Length).
For further action you need to move the position in the stream.
But as you said "large files": Is the source stream larger than imageData.Length then it is the non-expandable MemorStream.
Avatar of RIAS

ASKER

Thanks a ton experts! Will try and get back
Avatar of RIAS

ASKER

kaufmed  : Thanks but I tried this code
  If Not imageData Is Nothing Then
                        Using ms As New MemoryStream(imageData, 0, imageData.Length)
                            ms.Write(imageData, 0, imageData.Length)
                            ms.Position = 0
                            Try
                                image = Image.FromStream(ms, True)
                                Dim oldbitmap As Bitmap = (image)
                                clsFrmain.ScaleImage(PicImageFin, oldbitmap)
                                OriginalimageLoaded = oldbitmap
                                PicImageHeight = PicImageFin.Height
                                PicImageWidth = PicImageFin.Width
                            Catch ex As Exception
                            End Try
                        End Using
                    End If

Open in new window


Same error .
Avatar of RIAS

ASKER

ste5an,
Thanks! Can you please suggest the code .
Does your code fail with this error when you process image files (bmp, jpg....).  Remember a pdf file is not an image even though it may contain an image.
I would think, this should do it:

If Not imageData Is Nothing Then
    Using ms As New MemoryStream(imageData, 0, imageData.Length)
        image = Image.FromStream(ms, True)
        Dim oldbitmap As Bitmap = (image)
        clsFrmain.ScaleImage(PicImageFin, oldbitmap)
        OriginalimageLoaded = oldbitmap
        PicImageHeight = PicImageFin.Height
        PicImageWidth = PicImageFin.Width
    End Using
End If

Open in new window

btw, don't hide exceptions. Handle them.
Avatar of RIAS

ASKER

Thanks ste5an!
Will try now and brb!
Avatar of RIAS

ASKER

Ste5an,
Thanks, tried the code but it returns
 image =nothing

Any suggestions?
Avatar of RIAS

ASKER

AndyAinscow,

You are right, most of the PDF and email files are failing.
Any suggestion?

Thanks
They aren't images.  I don't understand what you are attempting but it looks like it wil fail.  

Do you require something in your app to display pdf files?
Avatar of RIAS

ASKER

Hi Andy,
Thanks for coming back.
I am trying o display the image files in picture box which are in Bytes[] format from sql server database table.
Then you need to trap out any 'images' which are not images.
Either use an if statement should you know the byte array is not really an image or embed this code in try..catch block and only display a successful conversion.
Avatar of RIAS

ASKER

Thanks!
Can you suggest a way to process these non images.
Andy I have been after this solution for an year now ...
If you can put some light on, it will be great!!
>>Can you suggest a way to process these non images.

What does that mean?  eg. Display pdf file in a viewer embedded in your app
Avatar of RIAS

ASKER

Yes please,
If you can suggest how to get the non images in form of Bytes[]  to a picturebox
Thank a ton
As your data comes from a database table: You must store the file type as separate column. You need to ensure that only correct data is stored according to the file type at the time of the uploads.

Many image types start with a magic byte sequence. You can do a byte-wise detection your self or use System.Drawing.Imaging.ImageCodecInfo.
Avatar of RIAS

ASKER

Thanks Ste5an!
The sad part is that I have no control over the table design.
I just need to find a work around to display all file types.
crudely using your original code (to show the logic):

try
Using ms As New MemoryStream(imageData, 0, imageData.Length)
                            ms.Write(imageData, 0, imageData.Length)
                            Try
                                image = Image.FromStream(ms, True)
                                Dim oldbitmap As Bitmap = (image)
                                clsFrmain.ScaleImage(PicImageFin, oldbitmap)
                                OriginalimageLoaded = oldbitmap
                                PicImageHeight = PicImageFin.Height
                                PicImageWidth = PicImageFin.Width
                            Catch ex As Exception
                            End Try
                        End Using
     Now display in picture box
catch exception e
  'Not an image - nothing displayed.  Show a message of some kind?
end catch
Avatar of RIAS

ASKER

Andy,
Thanks, but is there any code you suggest for non images
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial