I have a method that draws a Bitmap like so:
public void DrawGrid(){
GridBitMap = new Bitmap(x,y);
MyGraphic = Graphics.FromImage(GridBitMap);
MyGraphic.FillRectangle(BackGround, 0, 0, x, y);
//Then using loops I draw various lines like this
MyGraphic.DrawLine(Pen.Black,x,y,xx,yy);
//Then I add the Bitmap to a Picturebox
PictureBox.Image = GridBitMap;
}
My Problem is, every time the method is called. Im using more and more resources.
How do I release the Bitmap & graphic without causing much flicker?
and prevent the code from consuming more and more resources?
You should try this
if you don’t actually need to have a bitmap, why not draw directly into the picture box?
Creating a bitmap each time is time and resource consuming.
Hope this is what you are asking for. The using close ensure that the resource will be released even if there is an exception or if you exit the function prematurely.