Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had a similar question earlier and have figured out how to add a bitmap to a form. Now I need to figure out how to add the bitmap to a panel after I have rotated it.

Beginner at this graphics stuff so please help me out.

This loads a bitmap onto the form

VB
Public Class Form1
    Private angle As Integer
    Dim imagetwo As Bitmap
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        imagetwo = New Bitmap("C:\Documents and Settings\wiswalld\My Documents\DEP Police Patch.bmp")
        imagetwo.MakeTransparent(Color.Black)
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        e.Graphics.TranslateTransform(60, 60)
        e.Graphics.RotateTransform(angle)
        e.Graphics.DrawImage(imagetwo, -50, -50, 100, 100)
        e.Graphics.ResetTransform()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.angle += 5
        Me.Refresh()
    End Sub
End Class
Posted

1 solution

You don't "override event", this is not even possible. You can only handle event by adding an event handle to its invocation list; this is what you do. For all controls, especially for Form (and only because you already subclass it), it's better and easier to override virtual protected method OnPaint.

You can subclass Panel as well, but in most cases it does not add any functionality you need, so you better subclass Control and override OnPaint. This is because sooner or later you will need derived class anyway, for example, to access protected SetStyles, for example, to introduce optimized double buffering to avoid flicker. Also, you need to call Invalidate, not Refresh. You can call Invalidate with parameters (Rectange or Region) to re-render just a part of the scene.

The only big problem in your code I can see is a hard-code path name for a bitmap file. There are no situations where a hard-code path name could be useful, but I hope this is just for a try. You also did not report any problems you are facing with (why? this is the first thing to mention? what kind of help do you expect?), but if this is a wrong rotation or position, you just need to play with the transforms you need and their order to get a grip on how they work, which is pretty easy to do.

—SA
 
Share this answer
 
Comments
wiswalld 21-Dec-11 12:28pm    
The hard coding of the bitmap location is just for a test.

The problem I have is I can rotate the bitmap just fine. But when I add it to the panel it is the original (not rotated). I did not add the code for adding the bitmap to the panel as I am not sure how.

Dim pb As New PictureBox
pb.SizeMode = PictureBoxSizeMode.AutoSize
pb.Image = imagetwo
pb.Location = New Point(MainPanel.Width / 2 - pb.Width / 2, MainPanel.Height / 2 - pb.Height / 2)
MainPanel.Controls.Add(pb)
pb.BringToFront()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900