Re: Image FromStream GDI+ Error with PictureBox

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



When you create a bitmap from a stream,
The bitmap takes control of the stream and the
stream must remain open for the life of the bitmap.

This is a GDI+ requirement.

When you dispose of the bitmap, the stream is invalid and must
be recreated when you create another bitmap.


"Matthew" <mroberts_hm@xxxxxxxxxxx> wrote in message
news:1130380359.110470.132960@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> 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: why are pixels altered when i construct a bitmap from memory stream?
    ... probably doesn't support the color depth of the image in the stream, ... i wonder if the compact framework Bitmap class is ... // save the worked up bitmap back to memory stream ... this is how i converted an image in indexed pixel format to ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Gdi+ Bitmap/Image Clone() function
    ... Once I closed the bitmap, I was able to delete the file. ... constructor Image(IStream*,...) or by means of function Image::FromStream, and why these two functions do not release the stream and should I store the stream all the time untill Image is use. ... If a Bitmap or Image is created from a stream or file or memory chunk, ... the stream or memory stay's "alive" for the life of the Image or Bitmap. ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Gdi+ Bitmap/Image Clone() function
    ... release the stream and should I store the stream all the time untill Image ... If a Bitmap or Image is created from a stream or file or memory chunk, ... I thought of loading from an IStream to be similar from ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Gdi+ Bitmap/Image Clone() function
    ... release the stream and should I store the stream all the time untill ... If a Bitmap or Image is created from a stream or file or memory chunk, ... the stream or memory stay's "alive" for the life of the Image or Bitmap. ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Gdi+ Bitmap/Image Clone() function
    ... Create the stream. ... Now I can safely delete the IStream I created at point 1 and the bitmap is still valid. ... It just uses the stream to read the bitmap - the stream is not raw pixel data, it needs to be a supported image file stream (which can be in memory or on any other stream type). ... Once the objec is created, I want to be able to delete the memory buffer used to create it but, the bitmap object should stay around longer then the memory used to create it. ...
    (microsoft.public.win32.programmer.gdi)