Ir para o conteúdo

GDI+ - Árvore de Natal de 2009

Um exemplo da utilização das capacidades da framework para trabalhar com GDI+ O código abaixo irá produzir uma árvore de Natal desenhada com base em 4 triângulos de diferentes tamanhos.

'depois de abrir um projecto novo, no código do form:
Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class Form1

    Private Luzes As New List(Of Point)
    Private R As New Random
    Private FNT As New Font("Arial", 20, FontStyle.Bold)
    Private WithEvents Piscar As New Timer With {.Interval = 500, .Enabled = True}

    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        End
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Size = New Size(250, 600)
        Me.Top = CInt((Screen.PrimaryScreen.Bounds.Height / 2) - (Me.Height / 2))
        Me.Left = CInt((Screen.PrimaryScreen.Bounds.Width / 2) - (Me.Width / 2))
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None

        Dim B As New Bitmap(ClientSize.Width, ClientSize.Height)
        Dim G As Graphics = Graphics.FromImage(B)

        Dim BGLGB As New LinearGradientBrush(New Point(CInt(B.Width / 2), 0), New Point(CInt(B.Width / 2), 600), Color.DarkBlue, Color.Blue)

        'Os gradientes, de diferentes tamanhos para todos os 4 triângulos
        Dim LGB1 As New LinearGradientBrush(New Point(125, 20), New Point(125, 470), Color.White, Color.DarkGreen)
        Dim LGB2 As New LinearGradientBrush(New Point(125, 20), New Point(125, 353), Color.White, Color.DarkGreen)
        Dim LGB3 As New LinearGradientBrush(New Point(125, 0), New Point(125, 236), Color.White, Color.DarkGreen)
        Dim LGB4 As New LinearGradientBrush(New Point(125, 0), New Point(125, 120), Color.White, Color.DarkGreen)

        'Atribuição dos 3 pontos para a produção dos triângulos
        Dim T1() As PointF = {New PointF(125, 20), New Point(10, 470), New PointF(230, 470)}
        Dim T2() As PointF = {New PointF(125, 20), New Point(20, 353), New PointF(220, 353)}
        Dim T3() As PointF = {New PointF(125, 20), New Point(30, 236), New PointF(210, 236)}
        Dim T4() As PointF = {New PointF(125, 20), New Point(40, 120), New PointF(200, 120)}

        'Para pintar o céu azul
        G.FillRectangle(BGLGB, 0, 0, B.Width, B.Height)

        Dim R As New Random

        'Para pintar as estrelas, aleatóriamente
        For P = 1 To 500
            G.FillEllipse(Brushes.White, R.Next(0, B.Width), R.Next(0, B.Height), 2, 2)
        Next

        'Atribuição dos pontos para o polígono que representa a estrela
        Dim Estrela() As PointF = {New PointF(0 + 115, 10), New PointF(5 + 115, 10), _
                                   New PointF(10 + 115, 0), New PointF(15 + 115, 10), _
                                   New PointF(20 + 115, 10), New PointF(15 + 115, 15), _
                                   New PointF(20 + 115, 25), New PointF(10 + 115, 18), _
                                   New PointF(0 + 115, 25), New PointF(5 + 115, 15), _
                                   New PointF(0 + 115, 10), New PointF(0 + 115, 0)}

        'Desenho dos triângulos com os pontos previamente colocados. Rebordo e preenchimento
        G.FillPolygon(LGB1, T1) : G.DrawPolygon(New Pen(Color.Black, 2), T1)
        G.FillPolygon(LGB2, T2) : G.DrawPolygon(New Pen(Color.Black, 2), T2)
        G.FillPolygon(LGB3, T3) : G.DrawPolygon(New Pen(Color.Black, 2), T3)
        G.FillPolygon(LGB4, T4) : G.DrawPolygon(New Pen(Color.Black, 2), T4)

        'Desenho do polígono que representa a estrela
        G.FillPolygon(Brushes.Yellow, Estrela)

        'Desenho do tronco da árvore
        G.FillRectangle(Brushes.Brown, 100, 470, 50, (600 - 470))

        'Desenho do rebordo do tronco da árvore
        G.DrawRectangle(New Pen(Color.Black, 2), 100, 470, 50, (600 - 470))

        'Colocação abstracta da posição das luzes
        Luzes.Add(New Point(72, 105)) : Luzes.Add(New Point(100, 70)) : 
        Luzes.Add(New Point(136, 101)) : Luzes.Add(New Point(176, 105))
        Luzes.Add(New Point(127, 52)) : Luzes.Add(New Point(90, 134))
        Luzes.Add(New Point(149, 139)) : Luzes.Add(New Point(121, 160))
        Luzes.Add(New Point(69, 206)) : Luzes.Add(New Point(133, 215))
        Luzes.Add(New Point(106, 186)) : Luzes.Add(New Point(167, 180))
        Luzes.Add(New Point(152, 217)) : Luzes.Add(New Point(68, 251))
        Luzes.Add(New Point(128, 298)) : Luzes.Add(New Point(165, 253))
        Luzes.Add(New Point(109, 256)) : Luzes.Add(New Point(52, 333))
        Luzes.Add(New Point(75, 296)) : Luzes.Add(New Point(127, 328))
        Luzes.Add(New Point(187, 328)) : Luzes.Add(New Point(170, 286))
        Luzes.Add(New Point(63, 369)) : Luzes.Add(New Point(169, 394))
        Luzes.Add(New Point(94, 418)) : Luzes.Add(New Point(120, 368))
        Luzes.Add(New Point(36, 441)) : Luzes.Add(New Point(113, 453))
        Luzes.Add(New Point(201, 429)) : Luzes.Add(New Point(142, 447))
        Luzes.Add(New Point(186, 386))

        Me.BackgroundImage = B
    End Sub

    Private Sub Piscar_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Piscar.Tick
        Dim G As Graphics = Me.CreateGraphics
        'Desenhar um novo círculo, com uma cor diferente, nas posições das luzes previamente definidas
        For Each P As Point In Luzes
            Dim TB As New SolidBrush(DarCor())
            G.FillEllipse(TB, P.X, P.Y, 10, 10)
        Next

        'Cálculo de tamanho e posicionamento da palavra FELIZ NATAL, desenhada 3 vezes com um pequeno offset
        'para simular profundidade
        Dim TempMsg As String = "FELIZ NATAL"
        Dim TempTam As SizeF = G.MeasureString(TempMsg, FNT)
        G.DrawString(TempMsg, FNT, New SolidBrush(DarCor), 125 - (TempTam.Width / 2), 500)
        G.DrawString(TempMsg, FNT, New SolidBrush(DarCor), 125 - (TempTam.Width / 2) + 2, 500 + 2)
        G.DrawString(TempMsg, FNT, New SolidBrush(DarCor), 125 - (TempTam.Width / 2) + 4, 500 + 4)
    End Sub

    'Dar uma cor aleatória, dentro das que se querem disponibilizar
    Private Function DarCor() As Color
        Dim Cor1 As Color = Color.LightGreen
        Dim Cor2 As Color = Color.Blue
        Dim Cor3 As Color = Color.Red
        Dim Cor4 As Color = Color.Yellow

        Select Case CInt(R.Next(1, 4))
            Case 1 : Return Cor1
            Case 2 : Return Cor2
            Case 3 : Return Cor3
            Case 4 : Return Cor4
        End Select

    End Function

End Class

Resultado final

Imagem contendo uma árvore de Natal, e a mensagem "Feliz Natal"