mwinder
asked on
Out of memory error loading form backgroundimage
Man, I'm starting to feel abused. This shouldn't be hard:
' stub test code to load an image into memory and create a bitmap from it
Dim memoryStream As New System.IO.MemoryStream
Image.FromFile("C:\Anthony .jpg").Sav e(memorySt ream, System.Drawing.Imaging.Ima geFormat.J peg)
Dim Buffer As Byte() = memoryStream.GetBuffer()
Dim picStr As String = Convert.ToBase64String(Buf fer)
memoryStream.Close()
Dim memoryStream2 As New System.IO.MemoryStream(Con vert.FromB ase64Strin g(picStr))
Dim icon As New System.Drawing.Bitmap(memo ryStream2)
memoryStream2.Close()
' Test picture box
PictureBox1.Image = icon
' The real code
Me.BackgroundImage = icon ' New Bitmap("c:\Anthony.jpg")
When I run this code in the form load event I get an out of memory error setting the background image:
An unhandled exception of type 'System.OutOfMemoryExcepti on' occurred in system.windows.forms.dll
Additional information: Out of memory.
If I comment out the background image the picture box displays the same bitmap just fine. So I'm thinking the bitmap is OK. But if I change the background image to use the commented out "New Bitmap..." code the backgroundimage works. So what is it about the combination of backgroundimage and the dynamically created bitmap that is causing the memory error?
' stub test code to load an image into memory and create a bitmap from it
Dim memoryStream As New System.IO.MemoryStream
Image.FromFile("C:\Anthony
Dim Buffer As Byte() = memoryStream.GetBuffer()
Dim picStr As String = Convert.ToBase64String(Buf
memoryStream.Close()
Dim memoryStream2 As New System.IO.MemoryStream(Con
Dim icon As New System.Drawing.Bitmap(memo
memoryStream2.Close()
' Test picture box
PictureBox1.Image = icon
' The real code
Me.BackgroundImage = icon ' New Bitmap("c:\Anthony.jpg")
When I run this code in the form load event I get an out of memory error setting the background image:
An unhandled exception of type 'System.OutOfMemoryExcepti
Additional information: Out of memory.
If I comment out the background image the picture box displays the same bitmap just fine. So I'm thinking the bitmap is OK. But if I change the background image to use the commented out "New Bitmap..." code the backgroundimage works. So what is it about the combination of backgroundimage and the dynamically created bitmap that is causing the memory error?
Hi mwinder,
> Dim memoryStream2 As New System.IO.MemoryStream(Con vert.FromB ase64Strin g(picStr))
From a superficial look, I think this is the line that is causing you grief.
Maybe it will help if you can post the whole code for the form (if it is not to big) so that I can try it out on my system.
Dabas
> Dim memoryStream2 As New System.IO.MemoryStream(Con
From a superficial look, I think this is the line that is causing you grief.
Maybe it will help if you can post the whole code for the form (if it is not to big) so that I can try it out on my system.
Dabas
ASKER
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon tainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents PictureBox1 As System.Windows.Forms.Pictu reBox
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.Pictu reBox
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.Location = New System.Drawing.Point(104, 56)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(160, 192)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(440, 365)
Me.Controls.Add(Me.Picture Box1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' stub test code to load an image into memory and create a bitmap from it
Dim icon As System.Drawing.Bitmap
Try
Dim memoryStream As New System.IO.MemoryStream
Image.FromFile("C:\test.jp g").Save(m emoryStrea m, System.Drawing.Imaging.Ima geFormat.J peg)
Dim Buffer As Byte() = memoryStream.GetBuffer()
Dim picStr As String = Convert.ToBase64String(Buf fer)
memoryStream.Close()
Dim memoryStream2 As New System.IO.MemoryStream(Con vert.FromB ase64Strin g(picStr))
icon = New System.Drawing.Bitmap(memo ryStream2)
memoryStream2.Close()
Catch ex3 As Exception
Console.WriteLine(ex3.ToSt ring)
End Try
' Test picture box
Try
PictureBox1.Image = icon
Catch ex2 As Exception
Console.WriteLine(ex2.ToSt ring)
End Try
' The real code
Try
Me.BackgroundImage = icon ' New Bitmap("c:\test.jpg")
Catch ex As Exception
Console.WriteLine(ex.ToStr ing)
End Try
End Sub
End Class
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents PictureBox1 As System.Windows.Forms.Pictu
<System.Diagnostics.Debugg
Me.PictureBox1 = New System.Windows.Forms.Pictu
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.Location = New System.Drawing.Point(104, 56)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(160, 192)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(440, 365)
Me.Controls.Add(Me.Picture
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' stub test code to load an image into memory and create a bitmap from it
Dim icon As System.Drawing.Bitmap
Try
Dim memoryStream As New System.IO.MemoryStream
Image.FromFile("C:\test.jp
Dim Buffer As Byte() = memoryStream.GetBuffer()
Dim picStr As String = Convert.ToBase64String(Buf
memoryStream.Close()
Dim memoryStream2 As New System.IO.MemoryStream(Con
icon = New System.Drawing.Bitmap(memo
memoryStream2.Close()
Catch ex3 As Exception
Console.WriteLine(ex3.ToSt
End Try
' Test picture box
Try
PictureBox1.Image = icon
Catch ex2 As Exception
Console.WriteLine(ex2.ToSt
End Try
' The real code
Try
Me.BackgroundImage = icon ' New Bitmap("c:\test.jpg")
Catch ex As Exception
Console.WriteLine(ex.ToStr
End Try
End Sub
End Class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: Dabas {http:#9719512}
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
TheLearnedOne
EE Cleanup Volunteer
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: Dabas {http:#9719512}
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
TheLearnedOne
EE Cleanup Volunteer
ASKER
Dabas recreated my problem, but didn't offer a solution. He suggested there might be a conversion problem, but did not point out where. I don't mind if you want to close this, and I don't mind if you want to give Dabas credit for responding (I appreciate the try), but I do not think his answer is correct.
ASKER
My comment came across poorly. Dabas's answer's may be correct, but it did not solve my problem. If there is a bug in my code it would be nice to know where it is. I have seen the same code used in other (working) applications.
If the problem is the form is being recreated, is there a workaround? I could not find one, which is why I posted the question.
If the problem is the form is being recreated, is there a workaround? I could not find one, which is why I posted the question.
ASKER
I tried stepping to the offending code with the debugger and could not. The error appears immediately after leaving the form load function.