GDIPLUS: how rotate an image by a center?
GDIPLUS: how rotate an image by a center?
GDIPLUS: how rotate an image by a center?
GDIPLUS: how rotate an image by a center?
GDIPLUS: how rotate an image by a center?
GDIPLUS: how rotate an image by a center? GDIPLUS: how rotate an image by a center? GDIPLUS: how rotate an image by a center? GDIPLUS: how rotate an image by a center? GDIPLUS: how rotate an image by a center? GDIPLUS: how rotate an image by a center? GDIPLUS: how rotate an image by a center? GDIPLUS: how rotate an image by a center?
GDIPLUS: how rotate an image by a center? GDIPLUS: how rotate an image by a center?
GDIPLUS: how rotate an image by a center?
Go Back  Xtreme Visual Basic Talk > > > GDIPLUS: how rotate an image by a center?


Reply
 
Thread Tools Display Modes
  #1  
Old 01-01-2017, 02:19 PM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,296
Default GDIPLUS: how rotate an image by a center?


i'm trying rotate an image, but i can't find the solution for my problem. i had seen several samples without sucess
Code:
Dim i As Single
        For i = 0 To 25
            'GdipTranslateWorldTransform hGraphics, 100 + destWidth / 2, 100 + destHeight / 2, MatrixOrderAppend
            GdipRotateWorldTransform hGraphics, i, MatrixOrderAppend
            GdipTranslateWorldTransform hGraphics, 100 + lngwidth / 2, 100 + lngheight / 2, MatrixOrderAppend
            GdipDrawImageRectI hGraphics, HBITMAP, -100, -100, lngwidth, lngheight   ', 0, 0, lngwidth, lngheight, UnitPixel, ImageAttributes
            GdipResetWorldTransform hGraphics
            Sleep 500
        Next
the image is drawed and rotated, but not on same position
can anyone advice me?
lngwidth and lngheight are the image size. the 100X100 it's the image drawed position.
Reply With Quote
  #2  
Old 01-03-2017, 10:13 PM
passel's Avatar
passelGDIPLUS: how rotate an image by a center? passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

Well, there are a number of ways to approach this, and how you do kind of depends on how you think of the objects you are drawing and what they're coordinates are.
Do you use world coordinates for your points, or object relative coordinates.

The default Matrix order for GDIPLUS is MatrixOrderPrePend, and this is the object relative coordinate approach.
You can look at this approach as moving the coordinate system (the 0,0 point) to some place in your world coordinate system (where your object is located), then rotating and scaling that coordinate system, then drawing your object using it's local coordinate system.

The other approach is essentially the reverse, MatrixOrderAppend. You draw your object in world coordinates, so if you want to rotate it, you temporarily move it to the 0,0 point of the world coordinate system, rotate it, then move it back to where it was and draw it, using the world coordinates.
Either system is valid, and which you prefer depends on how you conceptualize the coordinate space.

I prefer the default, probably because of the 3d modeling I've seen, where the model is defined to some local coordinate system, and you translate the local coordinate system out to some world location, and transform it, then draw the object using its relative points. You don't change the objects points themselves unless you are deforming or moving parts of the object itself.

As I said, since I prefer the default, then the MatrixOrder would be Prepend.
To rotate an image at some given point on the screen I would :
1. Translate the 0,0 point to that point.
2. Rotate the coordinate system.
3. Draw the image using local coordinates, i.e. 0,0 would be the center of the image, so the upper left corner would be always be (-width/2, -height/2).

I don't use GDIPLUS with VB6 so don't have any examples of that.
But, the VB.Net version of the code would simply be along these lines.
Code:
    'Put the 0,0 point in the center of the ADI ball for all the following drawing.
    e.Graphics.TranslateTransform(ADI_cx, ADI_cy)  'translate 0,0 to the center of the ADI ball
    e.Graphics.RotateTransform(sroll)                  'rotate coordinate system by roll value
    e.Graphics.DrawImage(picPitchLadder.Image, dr, sr, GraphicsUnit.Pixel) 'copy PitchLadder image
    e.Graphics.ResetTransform()
Some of the values used in the code are as follows. They never have to change to show a rotating image centered at ADI_cx, ADI_cy.
Code:
  Dim ADI_cx As Integer = 258
  Dim ADI_cy As Integer = 154

  Dim sr As Rectangle = New Rectangle(0, 0, 252, 252)  '
  Dim dr As Rectangle = New Rectangle(-126, -126, 252, 252) 'destination rectangle of ADI ball, assuming 0,0 in middle of ball
(258,154) is where we draw the center of the rotating image on the screen.
The image is 252 pixels square. Since we want the center of the image to be at (0,0) we always draw the image (the upper left corner) at (-126,-126).

That is the way I do it. I find it easier to think that I'm moving the coordinate system around the screen and rotating it, then just drawing objects on it in relative coordinates. Seems to make it easier to have multiple connected moving parts working together.

=========================================
But, if I were trying to use the MatrixOrderAppend method, with the code you have, I think I would modify it like this. I can't test it directly, and I'm don't have the time to port the logic to VB.Net to see if it should work, but you can try it to see it makes a difference.

I would think you need something to clear the image between each rotation as well, but using the existing code you have...
Code:
        Dim i As Single
        For i = 0 To 25
            GdipTranslateWorldTransform hGraphics, -(100 + lngwidth / 2), -(100 + lngHeight / 2), MatrixOrderAppend
            GdipRotateWorldTransform hGraphics, i, MatrixOrderAppend
            GdipTranslateWorldTransform hGraphics, (100 + lngwidth / 2), (100 + lngheight / 2), MatrixOrderAppend
            GdipDrawImageRectI hGraphics, HBITMAP, 100, 100, lngwidth, lngheight   ', 0, 0, lngwidth, lngheight, UnitPixel, ImageAttributes
            GdipResetWorldTransform hGraphics
            Sleep 500
        Next
p.s. I did try the above code in a VB.Net version and it does appear to work
I put the updating of the angle (the loop) in a timer in .net because that is the better way to periodically loop, rather than use a sleep in the GUI thread.
Code:
  Dim bmp As New Bitmap(My.Resources._503car)
  Dim ang As Single

  Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    Dim lngwidth As Integer = bmp.Width
    Dim lngheight As Integer = bmp.Height

    e.Graphics.TranslateTransform(-(100 + lngwidth / 2.0F), -(100 + lngheight / 2.0F), Drawing2D.MatrixOrder.Append)
    e.Graphics.RotateTransform(ang, Drawing2D.MatrixOrder.Append)
    e.Graphics.TranslateTransform((100 + lngwidth / 2.0F), (100 + lngheight / 2.0F), Drawing2D.MatrixOrder.Append)
    e.Graphics.DrawImage(bmp, 100, 100)
  End Sub

  Private Sub Tick(sender As Object, e As System.EventArgs) Handles timer1.Tick
    ang += 1
    If ang > 360 Then ang -= 360
    Invalidate()  'to cause the paint event
  End Sub
p.p.s I would normally use the default (prepend) and the x,y point (100,100) would be the center of rotation, rather than the upper left corner of a box the image is rotating relative to, so the Paint sub would look like this instead.
Code:
  Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    Dim halfWidth As Integer = bmp.Width \ 2
    Dim halfHeight As Integer = bmp.Height \ 2

    e.Graphics.TranslateTransform(100, 100)
    e.Graphics.RotateTransform(ang)
    e.Graphics.DrawImage(bmp, -halfWidth, -halfHeight)
  End Sub
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 01-03-2017 at 10:32 PM.
Reply With Quote
  #3  
Old 01-06-2017, 09:12 AM
OnErr0r's Avatar
OnErr0rGDIPLUS: how rotate an image by a center? OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,774
Default

__________________
Quis custodiet ipsos custodues.
Reply With Quote
Reply

Tags
image, hgraphics, lngwidth, lngheight, matrixorderappend, drawed, rotate, position, imageattributes, hbitmap, gdipdrawimagerecti, unitpixel, gdipresetworldtransform, sleep, size, 100x100, advice, rotated, samples, sucess, solution, dim, destwidth, destheight, gdiprotateworldtransform


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
GDIPLUS: how rotate an image by a center?
GDIPLUS: how rotate an image by a center?
GDIPLUS: how rotate an image by a center? GDIPLUS: how rotate an image by a center?
GDIPLUS: how rotate an image by a center?
GDIPLUS: how rotate an image by a center?
GDIPLUS: how rotate an image by a center? GDIPLUS: how rotate an image by a center? GDIPLUS: how rotate an image by a center? GDIPLUS: how rotate an image by a center? GDIPLUS: how rotate an image by a center? GDIPLUS: how rotate an image by a center? GDIPLUS: how rotate an image by a center?
GDIPLUS: how rotate an image by a center?
GDIPLUS: how rotate an image by a center?
 
GDIPLUS: how rotate an image by a center?
GDIPLUS: how rotate an image by a center?
 
-->