RIAS
asked on
Memory stream error
Hello,
I am getting an error 'Parameter is not valid'
Any suggestion.
Thanks
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
It usually happens for large files especially PDF. Any suggestion.
Thanks
Would be useful to tell us which line fails with that error
ASKER
Andy:
Thanks :
image = Image.FromStream(ms, True) ---- Here
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?
Erm, Are you certain it is an image you are have in the memory stream?
ASKER
Andy,
Thanks, its been ages I have been looking for the solution.
I will really appreciate it if you can help.
Thanks
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)
Using ms As New MemoryStream(imageData, 0, imageData.Length)
ms.Write(imageData, 0, imageData.Length)
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.Len
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.
ASKER
Thanks a ton experts! Will try and get back
ASKER
kaufmed : Thanks but I tried this code
Same error .
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
Same error .
ASKER
ste5an,
Thanks! Can you please suggest the code .
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
btw, don't hide exceptions. Handle them.
ASKER
Thanks ste5an!
Will try now and brb!
Will try now and brb!
ASKER
Ste5an,
Thanks, tried the code but it returns
image =nothing
Any suggestions?
Thanks, tried the code but it returns
image =nothing
Any suggestions?
ASKER
AndyAinscow,
You are right, most of the PDF and email files are failing.
Any suggestion?
Thanks
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?
Do you require something in your app to display pdf files?
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.
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.
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.
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.
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
What does that mean? eg. Display pdf file in a viewer embedded in your app
ASKER
Yes please,
If you can suggest how to get the non images in form of Bytes[] to a picturebox
Thank a ton
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.Ima geCodecInf o.
Many image types start with a magic byte sequence. You can do a byte-wise detection your self or use System.Drawing.Imaging.Ima
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.
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(PicIm ageFin, 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
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(PicIm
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
ASKER
Andy,
Thanks, but is there any code you suggest for non images
Thanks, but is there any code you suggest for non images
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.