Click here to Skip to main content
16,004,574 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi! I'm trying to move text in picturebox like telepromter. On first load, it is working but after a while, I receive Out of Memory error.

Below code is in Timer control.
Thank you!

What I have tried:

s = s - 1
Drawbitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)
            Graph = Graphics.FromImage(Drawbitmap)
            PictureBox1.Image = Drawbitmap
            Graph.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            Graph.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
            Graph.DrawString(RichTextBox1.Text, dlg.Font, Brush, New RectangleF(0, s, PictureBox1.Width - 20, PictureBox1.Height))
            PictureBox1.Invalidate()
Posted

The likeliest reason you are getting out of memory errors is because you aren't disposing of finite resources. Under the covers, Bitmap and Graphics are unmanaged resources, and every time you create one of these you have to dispose of it. You could add Drawbitmap.Dispose(); and Graph.Dispose(); below PictureBox1.Invalidate(); but it would be easier to wrap these inside using statements.
 
Share this answer
 
Comments
Member 13049700 4-Sep-24 4:46am    
I revised the code but I am getting this error

"A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll

Additional information: Parameter is not valid."

This is the code.
s = s - 1
Using bmp As Bitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)
Using g As Graphics = Graphics.FromImage(bmp)
PictureBox1.Image = bmp
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
g.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
g.DrawString(RichTextBox1.Text, dlg.Font, Brush, New RectangleF(0, s, PictureBox1.Width - 20, PictureBox1.Height))
PictureBox1.Invalidate()
End Using
End Using
Pete O'Hanlon 4-Sep-24 7:08am    
When you step through your code, which line breaks?
Member 13049700 4-Sep-24 22:23pm    
This one "Using bmp As Bitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)" after the 200+ loop.
Luc Pattyn 4-Sep-24 8:12am    
you should not (have a using statement) dispose of the bmp that is currently in use by PictureBox1, you should dispose of the previous bmp, the one PictureBox1 will no longer use (if any) when you assign a new image.

:)
Member 13049700 4-Sep-24 22:24pm    
this is noted Luc :)
Why does it run out of memory? Because you are responsible for Disposing of every object you create, particularly if it consumes system resources. You create a new bitmap each time that code is executed - which can consume some significant memory, but the GC can cope with that. The problem is the Graphics context you create each time, which requires a system Handle - and those are scarce resources. If you create too many handles the whole system runs out, and that causes a "out of memory" error despite there being no GC involvement.

When you create a Graphics context, always do it in a using block so that it is automatically Disposed when you are finished with it.
I'd also suggest that you either Dispose "old" bitmaps when you replace them, or better reuse a single bitmap and draw on that - your app will use considerably less memory, and will probably run faster as well.
 
Share this answer
 
Comments
Member 13049700 4-Sep-24 6:13am    
I tried to put it in Picturebox Paint event. I also got error "Parameter is not valid"

s = s - 1
Dim Drawbitmap1 = New Bitmap(PictureBox1.Width, PictureBox1.Height)
Dim Graph1 As Graphics = Graphics.FromImage(Drawbitmap1)

Graph1.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
Graph1.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
e.Graphics.Clear(System.Drawing.Color.Black)
Graph1.DrawString(RichTextBox1.Text, New Font("Arial", trackFONT.Value, FontStyle.Bold), Brush, New RectangleF(0, s, PictureBox1.Width - 20, PictureBox1.Height))

e.Graphics.Clear(System.Drawing.Color.Black)
e.Graphics.DrawImage(Drawbitmap1, 0, s)
OriginalGriff 4-Sep-24 6:58am    
Where? What did the debugger show you, or was this a compiler error?
Member 13049700 4-Sep-24 22:18pm    
On this line..

Dim Drawbitmap1 = New Bitmap(PictureBox1.Width, PictureBox1.Height)

This is the error I get

"A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll

Additional information: Parameter is not valid."
OriginalGriff 5-Sep-24 1:46am    
So use the debugger at look at the PictureBox1 properties: what values are in Width and Height?
Luc Pattyn 4-Sep-24 8:17am    
a Graphics may well be the most expensive object around, and you don't dispose of Graph1??

and you create a new Font each time around? without disposing of it?

and what is "Brush"???

If this code runs at all, it will be sluggish and won't last long when you move something over the PictureBox causing a barrage of Paint events.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900