473,545 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Disposing of Bitmap

Hi,

Objective:
Draw a grid on a bitmap and set this as a panel's image. (Rather than
draw the grid directly on the panel and redraw repeatedly in the paint
event.)

Problem:
It works fine. I'm just not sure about disposing of the objects. I've
tried a variety of approaches but I find that when I redraw repeatedly
(just for testing) I get memory creep.
[I've recently moved to VB.Net from VB6 - head hurty.]

Question:
How should I be disposing of the bitmap to prevent the memory creep.

Thanks in advance.
Gman

Code as follows:
'This app is one form, with one panel and one button - that's it. Only
code is that which is below.

'Declare with form/class scope so I can easily dispose of it.
Private m_bmp As Bitmap

Private Sub Button1_Click(B yVal sender As System.Object, _
ByVal e As System.EventArg s) Handles Button1.Click

' Dispose of any existing bitmap
If Not m_bmp Is Nothing Then m_bmp.Dispose()

'create a bitmap that is the same size as the panel
'(it may have been resized)
m_bmp = New Bitmap(Panel1.W idth, Panel1.Height)
Dim gfx As System.Drawing. Graphics = Graphics.FromIm age(m_bmp)
Dim myPen As New System.Drawing. Pen(Color.Blue)

' Calculate the span of the lines
Dim iLinesInc As Integer = Panel1.Width \ 10
' Draw the lines on the bitmap
Dim i As Integer
For i = iLinesInc To Panel1.Width - iLinesInc Step iLinesInc
gfx.DrawLine(my Pen, i, 0, i, Panel1.Height)
Next i

' Dispose of the old image
If Not Panel1.Backgrou ndImage Is Nothing Then _
Panel1.Backgrou ndImage.Dispose ()
' Load the new bitmap into the panel
Panel1.Backgrou ndImage = m_bmp

' Tidy up
gfx.Dispose()
myPen.Dispose()
'm_bmp.Dispose( ) 'we can't dispose of it here

End Sub
Feb 16 '06 #1
0 1513

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1217
by: Dennis | last post by:
I have a global variable that is of type Image. I set this to different images in different routins using BitBlt to copy from the screen. If I dispose of the variable image after I'm thru with each image, not only the image is disposed but also the variable and I can't reference it again. Question is do I need to dispose of the old image...
10
1013
by: Dennis | last post by:
I don't think I clearly stated my problem in my prior question so let me try again. I have the following: Private myobject as Bitmap myobject = mybitmap (created from a part of the screen) How do I dispose of the instance of mybitmap object yet retain myobject so I can use it again like:
4
2481
by: Qwert | last post by:
Hello, I have a listview with an image list. After dispoing the images and the image list, the bitmap files remain locked: REM Create. objImage = Image.FromFile(strBmp) If Me.lsvTest.LargeImageList Is Nothing Then Me.lsvTest.LargeImageList = New ImageList() End If
7
8232
by: Fir5tSight | last post by:
Hi All, I used the following code in C#: using System.Drawing; //blah blah blah Bitmap bmp = new Bitmap();
2
1196
by: abhi.menon | last post by:
I have a "Plot" button the code associated with which (when Clicked) is as follows private: System::Void showPic_Click(System::Object * sender, System::EventArgs * e) { /* code to use Matlab to create a 'bmp' and save to C:\tmp\matlabFromNET_TRY2\somefile.bmp */
14
11869
by: eliss.carmine | last post by:
I'm using TCP/IP to send a Bitmap object over Sockets. This is my first time using C# at all so I don't know if this is the "right" way to do it. I've already found out several times the way I was doing something was really inefficient and could reduce 10 lines of code with 2, etc. For reading, I am using a TcpClient and I call ...
8
4296
by: Joergen Bech | last post by:
Suppose I have Dim bm As New Bitmap(16, 16,Imaging.PixelFormat.Format8bppIndexed) I cannot use Dim g As Graphics = Graphics.FromImage(bmdest) Dim hdc As IntPtr = g.GetHdc() as the FromImage call will fail for indexed bitmaps.
29
2345
by: Jerry Spence1 | last post by:
I'm rather confused as to whether something should be disposed of, or not. What is the general rule? How can you be sure of doing the right thing? I've heard about disposing unmanaged resources but what is an unmanaged resource? Does everything need disposing? I've been using Webrequest, Webresponse, Streamreaders etc. How can I be sure...
0
7468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7808
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7423
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7757
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5329
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.