473,473 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

GDI+: How can I get translate, scale and rotate transforms working together?

1 New Member
I'm trying to make a control on which the user can freely drag an image with the mouse and zoom and rotate it with the mouse wheel. I'd like to do this as far as possible using graphics transforms. At the moment, I have the code below in the Paint event handler of a Windows Form. The variables PanOrigin (Point), ZoomFactor (Single, starting value 1.0) and RotateDegrees (Integer) are controlled by mouse actions; I trust their meanings are obvious. The point mp is the centre of the window.

Expand|Select|Wrap|Line Numbers
  1. e.Graphics.TranslateTransform(-mp.X, -mp.Y, MatrixOrder.Append)
  2. e.Graphics.RotateTransform(RotateDegrees, MatrixOrder.Append)
  3. e.Graphics.ScaleTransform(ZoomFactor, ZoomFactor, MatrixOrder.Append)
  4. e.Graphics.TranslateTransform(PanOrigin.X, PanOrigin.Y, MatrixOrder.Append)
  5. e.Graphics.TranslateTransform(mp.X, mp.Y, Drawing2D.MatrixOrder.Append)
  6. e.Graphics.DrawImage(TestImage, 0, 0)
  7.  
The problem with this is that the effective centre of rotation and zooming moves along with the image, instead of staying at the window centre. I've tried modifying the above code with the TransformPoints method (see lines 4-6):

Expand|Select|Wrap|Line Numbers
  1. e.Graphics.TranslateTransform(-mp.X, -mp.Y, MatrixOrder.Append)
  2. e.Graphics.RotateTransform(RotateDegrees, MatrixOrder.Append)
  3. e.Graphics.ScaleTransform(ZoomFactor, ZoomFactor, 
  4. Dim pts() as Point={PanOrigin}
  5. e.Graphics.TransformPoints(CoordinateSpace.Device, CoordinateSpace.World, pts)
  6. e.Graphics.TranslateTransform(pts(0).X, pts(0).Y, MatrixOrder.Append)
  7. e.Graphics.TranslateTransform(mp.X, mp.Y, Drawing2D.MatrixOrder.Append)
  8. e.Graphics.DrawImage(TestImage, 0, 0)
  9.  
Now zooming and rotating stay centred at the middle of the window as intended, but dragging the image takes it off in a direction of its own instead of the dragged direction.

I have tried all kinds of adjustments and variations but so far I have not succeeded in getting rid of both these problems at the same time. Surely there must be a way. I hope someone on this forum can help me: replies in C# are also gratefully accepted (if not too long or complex!).

Regards, Vic
Aug 13 '08 #1
0 1939

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: DrDevious | last post by:
Maybe I am doing something wrong but has anyone else here noticed a difference in the positioning of text between the Graphics.DrawString method and the Win32 GDI DrawText function? My text is...
12
by: Charles Law | last post by:
If I draw a rectangle, 6 inches by 2 inches, on a user control with a PageUnit of Inches, I get a rectangle of 7 by 2.2 inches. Is this what people would expect? I would have hoped that it was at...
3
by: Just Me | last post by:
Using a Richtextbox I use EM_SETZOOM to set a zoom factor. If I set it to 2 the image on the screen increase but not exactly by 2. I've coded a ruler above the Richtextbox which does scale by...
1
by: Jan Warning | last post by:
Hi all, I expected the following 2 pieces of code to give the same result, but they don't Can anybody explain me why? Dim wissel As Graphics Dim Matrix As New Matrix wissel = pe.Graphics...
1
by: iwdu15 | last post by:
hi, im trying to rotate a gdi drawn object on my form with a keypress....forinstance when i push the down arrow, for it to rotate the object drawn until the top is down, or if i push the right...
0
by: iwdu15 | last post by:
hi, im drawing about 6 GDI drawn objects on the screen. How can i make each one rotate individually in different directions, speeds, etc? ive looked everywhere and cant seem to figure it out --...
10
by: ahoway | last post by:
I am having problems entering a sentence for translating into pig latin. It is set up now to read the entire sentence as one word. I would like to know how to look at each word in the sentence so...
6
by: Marco Trapanese | last post by:
Hi, I wrote an application that draw on a form complex graphics. Tons of image with transparencies. I used GDI+ of course... but they are so slow! The paint event takes about 250 ms... I need to...
2
by: AliR \(VC++ MVP\) | last post by:
I'm trying to write a RTF render code in C#. I have the code in C++/MFC and it works fine, but I have run into a problem with the C# code. I think the C# code is from Microsoft, and I added...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.