Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Out of memory error loading form backgroundimage

Posted on 2003-11-09
8
Medium Priority
?
1,518 Views
Last Modified: 2007-12-19
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").Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg)
            Dim Buffer As Byte() = memoryStream.GetBuffer()
            Dim picStr As String = Convert.ToBase64String(Buffer)
            memoryStream.Close()
            Dim memoryStream2 As New System.IO.MemoryStream(Convert.FromBase64String(picStr))
            Dim icon As New System.Drawing.Bitmap(memoryStream2)
            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.OutOfMemoryException' 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?

0
Comment
Question by:mwinder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
8 Comments
 
LVL 1

Author Comment

by:mwinder
ID: 9718611
I wrapped try catch blocks around my code and the error actually happens AFTER the form load function returns.  The forms begins to display on the screen and the "Out of memory" error appears.  

I tried stepping to the offending code with the debugger and could not.  The error appears immediately after leaving the form load function.
0
 
LVL 27

Expert Comment

by:Dabas
ID: 9718722
Hi mwinder,
>             Dim memoryStream2 As New System.IO.MemoryStream(Convert.FromBase64String(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
0
 
LVL 1

Author Comment

by:mwinder
ID: 9718768
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.IContainer

    '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.PictureBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        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.PictureBox1)
        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.jpg").Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg)
            Dim Buffer As Byte() = memoryStream.GetBuffer()
            Dim picStr As String = Convert.ToBase64String(Buffer)
            memoryStream.Close()
            Dim memoryStream2 As New System.IO.MemoryStream(Convert.FromBase64String(picStr))
            icon = New System.Drawing.Bitmap(memoryStream2)
            memoryStream2.Close()
        Catch ex3 As Exception
            Console.WriteLine(ex3.ToString)
        End Try

        ' Test picture box
        Try
            PictureBox1.Image = icon
        Catch ex2 As Exception
            Console.WriteLine(ex2.ToString)
        End Try

        ' The real code
        Try
            Me.BackgroundImage = icon    ' New Bitmap("c:\test.jpg")
        Catch ex As Exception
            Console.WriteLine(ex.ToString)
        End Try

    End Sub
End Class
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 27

Accepted Solution

by:
Dabas earned 2000 total points
ID: 9719512
mwinder,
I did the following experiment.

Left the line with New Bitmap("C:\test.jpg") so that the form runs OK

I then created a new button and transferred the code to the button.

Guess what, even after single stepping through the button's code, the program crashes on line 4 (loading the form)

Conclusion: It is not the form load that is causing your bug. The form loads.
I just read in a .NET book that one of the improvements of .NET over VB6 is that it allows you to do things that you were not able to do before. This is because at occasions, .NET behind the scenes drops the form and recreates it new (with a new windows handle!)
I think this is what is happening here. If you change the BackgroundImage, the whole form gets redrawn.
I do not fully understand your code, but obviously you seem to be doing the wrong type of conversion somewhere.

Dabas
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10256701
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
0
 
LVL 1

Author Comment

by:mwinder
ID: 10316115
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.
0
 
LVL 1

Author Comment

by:mwinder
ID: 10316620
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.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question