SelectObject





VB.Net


Imports System.Runtime.InteropServices

Public Class Form1

    Private Declare Function SelectObject Lib "gdi32" Alias "SelectObject" (ByVal hdc As IntPtr, ByVal hObject As IntPtr) As IntPtr

    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        ' Create a new bitmap
        Dim bitmap As New Bitmap(100, 100)

        ' Get the device context of the form
        Dim gr As Graphics = CreateGraphics()
        Dim hdc As IntPtr = gr.GetHdc()

        ' Select the bitmap into the device context
        Dim hObject As IntPtr = bitmap.GetHbitmap()
        SelectObject(hdc, hObject)

        ' Draw something on the device context
        Using g As Graphics = Graphics.FromHdc(hdc)
            g.FillRectangle(Brushes.Red, New Rectangle(10, 10, 50, 50))
        End Using

        ' Release the object from the device context
        SelectObject(hdc, IntPtr.Zero)

        ' Release the device context
        gr.ReleaseHdc()

    End Sub
End Class