Results 1 to 3 of 3

Thread: RotateTransform seems to have no effect.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,945

    RotateTransform seems to have no effect.

    I'm trying to display a rectangle in a picture box and then rotate it. Using the information I found at http://www.informit.com/articles/art...29477&seqNum=9 I managed to display a rectangle in a picture box, but, I can't get it to rotate. So, how do I rotate an image in VB.NET?

    VB Code:
    1. Dim myGraphics As Graphics, myPen As Pen
    2.  
    3. myPen = New Pen(Color.Red)
    4. myGraphics = Graphics.FromHwnd(PictureBox1.Handle)
    5. myGraphics.DrawRectangle(myPen, 100, 100, 200, 200)
    6. myGraphics.TranslateTransform(100, 100)
    7. myGraphics.RotateTransform(45)

  2. #2
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    297

    Re: RotateTransform seems to have no effect.

    I'd have done it like this

    VB Code:
    1. Dim myGraphics As Graphics
    2.         Dim bm As New Bitmap(PictureBox1.Width, PictureBox1.Height)
    3.         myGraphics = Graphics.FromImage(bm)
    4.         myGraphics.TranslateTransform(100, 100)
    5.         myGraphics.RotateTransform(45)
    6.         myGraphics.DrawRectangle(Pens.Red, -50, -50, 75, 75)
    7.         PictureBox1.Image = bm

    Don't start drawing on pictureboxes. They are not there to provide a graphics surface. Just create a bitmap of the correct size and draw in that. The advantage of doing it this way is that the image will stick around.
    Otherwise, if a paint event is called on the picturebox, then the default drawing will overdraw your drawing (it will clear the background so you lose everything). If you draw in a bitmap and assign the bitmap to the pictureboxes' image property then it will be drawn during that paint event.
    Next up, you might aswell just say pens.red in the drawrectangle call. Defining your own pen will just use resources. It's not a big deal...

    Finaly rotation.
    what your original code did was to:
    draw a rectangle
    move the origin to 100,100 (the origin is the location (0,0))
    rotate both axes about the new origin by 45%

    The important thing is that moving the origin, and rotating the axes does not effect the drawing that you have already done. Imaginge you are an animator and use the transparent plastic sheets to draw your image. You draw the rectangle on one sheet, then you grab the next shhet and put it on top. Then you draw the new axes you want to use - with the different origin and the rotation. You do not effect the old rectangle, any subsequent drawing you do uses the new co-ordinate system.

    So what I did was to move the 0,0 first, then rotate. Finally I have to try to imagine the next axes. I fail to imagine the new axes and grab a bit of paper... Then I work out where I will want to draw the rectangle.
    As the origin is in the middle of the drawing area, I start the rectangle at (-x,-y). The width is easy enough.
    I used numbers that allowed the rectangle to fit into my picturebox.

    this one shows the axes before and after a translation & rotation
    VB Code:
    1. Dim bm As New Bitmap(PictureBox1.Width, PictureBox1.Height)
    2.         Dim g As Graphics = Graphics.FromImage(bm)
    3.         g.DrawLine(Pens.LightGreen, -100, 0, 100, 0)
    4.         g.DrawString("+x", Me.Font, Brushes.Black, 30, 10)
    5.         g.DrawLine(Pens.LightGreen, 0, -100, 0, 100)
    6.         g.DrawString("+y", Me.Font, Brushes.Black, 10, 30)
    7.         'move origin to the centre of the bitmap
    8.         g.TranslateTransform(bm.Width / 2, bm.Height / 2)
    9.         'rotate 45 degrees.
    10.         g.RotateTransform(45)
    11.         'draw the same lines
    12.         g.DrawLine(Pens.Orange, -100, 0, 100, 0)
    13.         g.DrawString("+x", Me.Font, Brushes.Black, 30, 10)
    14.         g.DrawLine(Pens.Orange, 0, -100, 0, 100)
    15.         g.DrawString("+y", Me.Font, Brushes.Black, 10, 30)
    16.         'show the boundary of the picbox:
    17.         Me.PictureBox1.BorderStyle = BorderStyle.FixedSingle
    18.         Me.PictureBox1.Image = bm
    Last edited by jo0ls; Oct 20th, 2005 at 11:41 AM.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,945

    Re: RotateTransform seems to have no effect.

    Well, what I didn't mention was that I used a picture box with a bitmap from a file assigned to it using the Image property. At first I wanted to see if I could rotate the picture which I loaded. The first code sample in your reply erases the picture, but it does show the rectangle, rotated. I want to see if I can draw a rectangle over the picture and rotate both the picture and the rectangle. So far I managed to rotate the rectangle and preserve the picture, but it won't rotate.

    Here's your code, modified:

    VB Code:
    1. Dim myGraphics As Graphics, bm As Object
    2.  
    3. bm = PictureBox1.Image
    4.  
    5. myGraphics = Graphics.FromImage(bm)
    6. myGraphics.TranslateTransform(100, 100)
    7. myGraphics.RotateTransform(45)
    8. myGraphics.DrawRectangle(Pens.Red, -50, -50, 75, 75)
    9.  
    10. PictureBox1.Image = bm

    As far as TranslateTransform is concerned, I will try to figure out what it does once I have managed to rotate both the picture and rectangle.

    13 years of programming experience and VB.NET makes me feel like a beginner...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width