//Initialize Graphics object Graphics graphicsObj = CreateGraphics(); //Get the device context (DC) associated with graphicsObj handle IntPtr hdc = graphicsObj.GetHdc(); //Do some drawing operations on hdc //Release the DC when it is no longer needed graphicsObj.ReleaseHdc(hdc);
//Initialize Bitmap object with width and height Bitmap bitmapObj = new Bitmap(500, 500); //Get the device context (DC) associated with bitmapObj handle IntPtr hdc = bitmapObj.GetHdc(); //Do some drawing operations on hdc //Release the DC when it is no longer needed bitmapObj.ReleaseHdc(hdc);In this example, we initialize a Bitmap object with a specified width and height. We then get the device context (DC) associated with its handle using the GetHdc method. We perform some drawing operations on the DC and release it using the ReleaseHdc method when it is no longer needed.