Image FromStream GDI+ Error with PictureBox

Tech-Archive recommends: Fix windows errors by optimizing your registry



Howdy All,

I have an object model that stores images for use in a graphics
application. Rather than reading the images and keeping them in memory,
which has a HUGE footprint, I instead read the file into an array of
bytes, convert the array of bytes into a memory stream, then use the
Image.FromStream method to create the image. When I am done with that
image, I dispose of the image object, but keep the memory stream open
so it is quick to access next time. This sort of works and has a much
smaller footprint. However, whenever I try to display the image in a
picture box, I get the following error:


A generic GDI+ error occurred.
Invalid parameter used.

System.ArgumentException: Invalid parameter used.
at System.Drawing.Image.get_Width()
at System.Drawing.Image.get_Size()
at System.Windows.Forms.PictureBox.get_ImageRectangle()
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
at
System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,
Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)


The code that creates the memory stream and image from the stream is as
follows:

Public Sub LoadImageStream()
If m_ImageStream Is Nothing Then
If m_Path.Length > 0 AndAlso System.IO.File.Exists(m_Path) Then
SyncLock m_ImageLockObject
Dim fs As New System.IO.FileStream(m_Path, IO.FileMode.Open,
IO.FileAccess.Read, IO.FileShare.Read)
Dim arr(CInt(fs.Length - 1)) As Byte
fs.Read(arr, 0, CInt(fs.Length))
fs.Close()
m_ImageStream = New System.IO.MemoryStream(arr)
End SyncLock
End If
End If
End Sub

Public ReadOnly Property Image() As System.Drawing.Bitmap
Get
If m_ImageStream Is Nothing Then
LoadImageStream()
DisposeImage()
End If
If m_Image Is Nothing AndAlso Not m_ImageStream Is Nothing Then
m_Image = CType(System.Drawing.Bitmap.FromStream(m_ImageStream,
True), System.Drawing.Bitmap)
End If
Return m_Image
End Get
End Property


The image loads from the stream with no problem. But, when I try to
display the image in the picture box, I get the error.

I have seen tons of posts related to this. But, I have yet to find a
solution to the problem.

Does anyone have a solution, or is there some better method of caching
these images in memory?

Thanks,
Matthew Roberts

mroberts_hm<at>hotmail<dot>com
matthewroberts<at>srcp<dot>com

.



Relevant Pages

  • Re: ForwardOnlyMemoryStream?
    ... since I do large atomic disk reads of the entire ... With 2 worker threads, this works great. ... But with 4 worker threads Out of Memory exceptions ... This would be the same as a memory stream, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: "The object is currently in use elsewhere" exception with prin
    ... Closing the memory stream disposes of the memory. ... Bob Powell ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: ForwardOnlyMemoryStream?
    ... array it's using for storage, so it would have to create a new array and copy the remaining data to it. ... This could cause even more memory problems than you have now... ... I would suggest that you make your own stream reader class that uses a large buffer, that way you reduce the frequency of the disk access while keeping the memory usage at a controllable level. ... that someone else had already done a smarter memory stream. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: ForwardOnlyMemoryStream?
    ... in throughtput versus a standard FileStream. ... file into a memory stream, letting the disk do sequential reads for each file. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Memory Profiler for Heap Analysis
    ... It doesn't matter what other objects point to B, for it will always be referenced from A, thus, is part of A no matter what. ... object model, throw away the DOM parsing and create a SAX parser which directly creates the model. ... I have the heap dump in front of my eyes, I can see all the classes of the DOM, still I can't figure out just how much of the memory they cost in total. ...
    (comp.lang.java.programmer)