|  |  | 
              
              | 
                  | Title | Convert between Rectangle and RectangleF variables quickly in Visual Basic .NET | 
|---|
 | Description | This example shows how to convert between Rectangle and RectangleF variables quickly in Visual Basic .NET. | 
|---|
 | Keywords | Rectangle, RectangleF, VB.NET | 
|---|
 | Categories | VB.NET | 
|---|
 |  | 
 |  | 
To convert a Rectangle into a RectangleF, use RectangleF.op_Implicit.
To convert a RectangleF into a Rectangle, use Rectangle.Round or Rectangle.Truncate.
 
The program makes a Rectangle, converts it into a RectangleF, converts that back into a Rectangle, and then draws them with different pen thicknesses so you can see them all.
               |  | 
 |  
                | Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    ' Make a rectangle.
    Dim rect1 As New Rectangle(20, 20, Me.ClientSize.Width _
        - 40, Me.ClientSize.Height - 40)
    ' Convert to RectangleF.
    Dim rectf As RectangleF = RectangleF.op_Implicit(rect1)
    ' Convert back to Rectangle.
    Dim rect2 As Rectangle = Rectangle.Round(rectf)
    'Dim rect2 As Rectangle = Rectangle.Truncate(rectf)
    ' Draw them.
    Dim the_pen As New Pen(Color.Red, 20)
    e.Graphics.DrawRectangle(the_pen, rect1)
    the_pen.Color = Color.Lime
    the_pen.Width = 10
    e.Graphics.DrawRectangles(the_pen, New RectangleF() _
        {rectf})
    the_pen.Color = Color.Blue
    the_pen.Width = 1
    e.Graphics.DrawRectangle(the_pen, rect2)
    the_pen.Dispose()
End Sub |  |  |  |   |  |  |  |  |