HotDog's Blog

Hotdog (Robert Verpalen) about C# and vb.net

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

AugSeptember 2008Oct
SMTWTFS
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

Articles

Archives

Topics

CONTACT

Fun but useful linkies

General

VS 2005

Wolfenstein ET

Just a very small control that shows the image centered, but with an additional Angle property (in degrees) that shows the original image rotated. The control doesn't posess much code, but after a question on vbcity about such a thing, it seems that there's mainly some longer functions with the more difficult task of rotating an image itself (you need some math to calculate the new bounds then)

This lightweight tidbit is just to show how little code is needed in the great .net :) to create a picturebox which can rotate its image. It doesn't contain a timer, just the angle property. The timer can be added easily enough (with a RotateBox+= StepOfAngle in it) The code is in VB.net this time.. (since it was meant as an answer to a vb.net post ;-) )

Code Copy HideScrollFull
Imports System.Drawing.Drawing2D 'needs the 'normal'windows imports as well

Public
Class RotateBox
    Inherits PictureBox

    Public Sub New()
        setstyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or ControlStyles.ResizeRedraw, True)
    End Sub

    Dim a As Single
    Public Property Angle() As Single
        Get
            Return a
        End Get
        Set(ByVal Value As Single)
            a = Value
            Invalidate()
        End Set
    End Property

    Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)

        If Me.Image Is Nothing Then Return

        Dim bmp As Image = CType(Image, Bitmap)
        Dim g As Graphics = pe.Graphics

        Dim M As New Matrix
        'rotate from the middle
        M.RotateAt(Angle, New PointF(Width / 2, Height / 2))
        g.Transform = M

        'draw the bitmap centered
        g.DrawImage(bmp, CInt((Width - bmp.Width) / 2), CInt((Height - bmp.Height) / 2))

    End Sub
End
Class
. . .
posted on Thursday, September 01, 2005 2:14 AM