Re: Image FromStream GDI+ Error with PictureBox
- From: "Michael Phillips, Jr." <mphillips53@xxxxxxxxxxxxxxx>
- Date: Thu, 27 Oct 2005 10:43:40 -0400
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
>
.
- Follow-Ups:
- Re: Image FromStream GDI+ Error with PictureBox
- From: Matthew
- Re: Image FromStream GDI+ Error with PictureBox
- References:
- Image FromStream GDI+ Error with PictureBox
- From: Matthew
- Image FromStream GDI+ Error with PictureBox
- Prev by Date: EXIF & MarkerNote
- Next by Date: DRAW IMAGE with "double" TRANSPARENCY
- Previous by thread: Image FromStream GDI+ Error with PictureBox
- Next by thread: Re: Image FromStream GDI+ Error with PictureBox
- Index(es):
Relevant Pages
|