GDI+ FromImage, FromHdc, and FromHwnd Methods in VB.NET
In this article I will explain about The FromImage, FromHdc, and FromHwnd Methods in GDI+.
An application can use Graphics class members to get a Graphics object. The Graphics class provides three methods to create a Graphics object: FromHwnd, FromHdc, and FromImage.
FromImage takes and Image object as input parameter and returns a Graphics object. The following code snippet creates a Graphics object from an Image object. One a Graphics object has been created, you can call its members.
Dim img As Image = Image.FromFile("Rose.jpg")
Dim g As Graphics = Graphics.FromImage(img)
' Do something
Note: Make sure you call the Dispose method of the Graphics object when you're finished with it.
FromHdc creates a Graphics object from a window handle to a device context. The following code snippet shows an example in which FromHdc takes one parameter, of type IntPtr.
Dim hdc As IntPtr = e.Graphics.GetHdc()
Dim g As Graphics = Graphics.FromHdc(hdc)
' Do something
Note: You need to call the ReleaseHdc method to release resource allocated by a window handle to a device context, and also make sure you call the Dispose method of the Graphics object when you're finished with it.
FromHwnd returns a Graphics object for a form. The following method takes a window handle.
Dim g As Graphics = Graphics.FromHwnd(Me.Handle)
To draw on a form, an application can pass this handle. One an application has a Graphics object, it can call any Graphics class method to draw graphics object.
Hope the article would have helped you in understanding The FromImage, FromHdc, and FromHwnd Methods in GDI+. Read other articles on GDI+ on the website.