473,883 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Graphics.World when drawing

I am wondering whether it is easy to setup a coordinate system for
drawing (using GDI+) that uses meters (or any custom scaling for that
matter). Currently, I need to convert from pixels to meters for
scaling. This can sometimes get rather complex when you need to take
scaling into account or other things like window resizing, scrolling,
etc.

There is a property called PageUnit which can be set to World. I could
not find any examples that illustrate how to use this "World" mode. Can
anyone show me a snippet of code how to setup the client drawing area
to work with meters?

In old VB, it was possible to create a custom scale. If I said that the
client area is 100 x 100, my app simply interpreted that as being
whatever I chose to define as the units. So if I drew a line 10 units
long horizontally (and assuming that the scale factor was set to one),
it would draw a line 1/10th the width of my client area. I am looking
for something along this line of simplicity.

Thanks for any help
Johann Blake

Nov 17 '05 #1
6 8175
You can set the page units to millimeters. This gives each unit in world
space the value of 1 mm. Conversions to and from pixels are unneccesary.
Such conversion schemes can be a nightmare especially if you have to take
printing into account.

All zooming, panning and mouse input should be handled using a Matrix object
and you should simply paint all your object in their correct place in world
space coordinates.

See the GDI+ FAQ and Windows Forms Tips and Tricks for some ideas.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

<jo*********@ya hoo.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I am wondering whether it is easy to setup a coordinate system for
drawing (using GDI+) that uses meters (or any custom scaling for that
matter). Currently, I need to convert from pixels to meters for
scaling. This can sometimes get rather complex when you need to take
scaling into account or other things like window resizing, scrolling,
etc.

There is a property called PageUnit which can be set to World. I could
not find any examples that illustrate how to use this "World" mode. Can
anyone show me a snippet of code how to setup the client drawing area
to work with meters?

In old VB, it was possible to create a custom scale. If I said that the
client area is 100 x 100, my app simply interpreted that as being
whatever I chose to define as the units. So if I drew a line 10 units
long horizontally (and assuming that the scale factor was set to one),
it would draw a line 1/10th the width of my client area. I am looking
for something along this line of simplicity.

Thanks for any help
Johann Blake

Nov 17 '05 #2
World is only for specifying font sizes using world coordinates -- the same
coordinates as the current PageUnit and PageScale. World cannot be used as
the coordinate type for Graphics.PageUn it.

Regards,
Frank Hileman

check out VG.net: http://www.vgdotnet.com
Animated vector graphics system
Integrated Visual Studio .NET graphics editor

<jo*********@ya hoo.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I am wondering whether it is easy to setup a coordinate system for
drawing (using GDI+) that uses meters (or any custom scaling for that
matter). Currently, I need to convert from pixels to meters for
scaling. This can sometimes get rather complex when you need to take
scaling into account or other things like window resizing, scrolling,
etc.

There is a property called PageUnit which can be set to World. I could
not find any examples that illustrate how to use this "World" mode. Can
anyone show me a snippet of code how to setup the client drawing area
to work with meters?

In old VB, it was possible to create a custom scale. If I said that the
client area is 100 x 100, my app simply interpreted that as being
whatever I chose to define as the units. So if I drew a line 10 units
long horizontally (and assuming that the scale factor was set to one),
it would draw a line 1/10th the width of my client area. I am looking
for something along this line of simplicity.

Thanks for any help
Johann Blake

Nov 17 '05 #3
That is very strange that you should say that World cannot be used as
the coordinate type for Graphics.PageUn it. The PageUnit uses the
GraphicUnit enumeration which includes World as one of its members and
there is nothing that I was able to find in MSDN documentation that
indicates that it cannot be used.

Nov 17 '05 #4
I actually do set the page units to millimeters. You mention:

"This gives each unit in world space the value of 1 mm"

What I discovered however is that this results in a line being
physically drawn on the screen in millimeters. This is not "world"
space, at least not the way most programmers envision world space.
World space is whatever a line length represents in the real world and
not how physically long it appears on a screen, which can be modified
with zooming factors.

What I am trying to avoid of course is any relationship between
physical lengths on the screen and the real world lengths that a line
length represents.

So if I want to draw a line that is a meter long but want it to appear
on the screen as 10 mm, I need to set ScaleTransform to some value that
scales from meters down to millimeters. The problem is, that a straight
meter-to-mm conversion isn't good enough. You need to take into account
the zooming factor. Why is that? Imagine displaying a single rectangle
on a screen. The rectangle is 10 meters wide by 20 meters high. If I
want the rectangle to fit exactly the height of the screen (such as in
Word with "fit to screen"), you need to apply a secondary amount of
scaling to the ScaleTransform and this can only be done by taking the
height of pixels of the client area into account. The client size
property returned in an OnPaint event gives its size in pixels and not
in millimeters (or whatever units you have set).

Nov 17 '05 #5
A trap that novices often fall into is to take zooming and panning into
account when doing drawing. This is entirely the wrong approach and causes
lots of problems because wherever you zoom or pan you must also unzoom and
pan back to deal with mouse interactions and so-on.

The correct technique is to set up a transform that takes care of your
zooming and / or panning and draw everything as if it were at 1:1 scale
allowing the display pipeline to put the pixels in the right place for you.

First apply a transform that copes with whatever level of zoom and pan you
need. Second, save the Graphics state either using Graphics.Save
(BeginContainer/EndContainer is broken) or using a home-grown matrix stack.
Third apply any other transform such as scale or rotation. Fourth, draw all
objects at 1:1 scale. Fifth, restore the graphics state back to the original
setting. Nesting states can give complex yet consistent drawing object
models.

The inverse of the matrix used to draw any specific object can be used to
"backtrack" the mouse from the current client coordinates to real-world
measurements in the graphics system.

See the GDI+ FAQ for more detail on all these subjects.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

<jo*********@ya hoo.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I actually do set the page units to millimeters. You mention:

"This gives each unit in world space the value of 1 mm"

What I discovered however is that this results in a line being
physically drawn on the screen in millimeters. This is not "world"
space, at least not the way most programmers envision world space.
World space is whatever a line length represents in the real world and
not how physically long it appears on a screen, which can be modified
with zooming factors.

What I am trying to avoid of course is any relationship between
physical lengths on the screen and the real world lengths that a line
length represents.

So if I want to draw a line that is a meter long but want it to appear
on the screen as 10 mm, I need to set ScaleTransform to some value that
scales from meters down to millimeters. The problem is, that a straight
meter-to-mm conversion isn't good enough. You need to take into account
the zooming factor. Why is that? Imagine displaying a single rectangle
on a screen. The rectangle is 10 meters wide by 20 meters high. If I
want the rectangle to fit exactly the height of the screen (such as in
Word with "fit to screen"), you need to apply a secondary amount of
scaling to the ScaleTransform and this can only be done by taking the
height of pixels of the client area into account. The client size
property returned in an OnPaint event gives its size in pixels and not
in millimeters (or whatever units you have set).

Nov 17 '05 #6
Hi Bob,

Actually I don't take zooming into account when drawing, that is, when
I call DrawLine, I don't factor in zooming. Zooming is done using
ScaleTransform and is only set once. After that, I do draw 1:1. But
when applying the initial transform, it is absolutely required to take
the client size into account which is always defined in pixels. I can't
see how it is possible to avoid this. Where things become rather
difficult has to do with scrolling and resizing. Any events raised as a
result of resizing or scrolling give you either a size or offset in
pixels that need to be converted over to real world coordinates. So if
you have a drawing that is larger than the client area and you have
AutoScrollMinSi ze set to allow you to scroll down the form to the part
not showing, you will still need to modify the transformation to pan
correctly.

What I have found very much lacking on the Internet is a simply
application that shows drawing in a units, like meters, that shows
zooming, panning, resizing and scrolling altogether. Almost every
example out there shows drawing on a form that does not include
scrollbars or take resizing into account.

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
9586
by: Paul Hoad | last post by:
I'm trying to use MeasureString() to determine the length in pixels of a string However to do I need a System.Drawing.Graphics object to do this I need to create a System.Drawing.Graphics object for which there is only two constructors System.Drawing.Graphics.FromHdc
0
4331
by: Eric | last post by:
I'm also interested in finding out more about using vector graphics when designing the UI in Windows forms applications. I know a lot about GDI+ and raster graphics, but vector graphics is a new subject for me. I'm also preparing for Avalon and I've stumbled upon this in the meantime which seems to be a pretty good idea of what Avalon will be like when it comes out: VG.NET: (Animated Vector Graphics for Visual Studio)...
5
20298
by: Vin | last post by:
Hi, I am using the following code to draw whatever the user draws using x,y. // draws lines directly on a winform. CreateGraphics().DrawLine(APen, x, y, OldX, OldY); Now how do I save the drawing on to a bmp file on my harddisk? C# code in this regard would be very helpful. I tried all forums but invain.
1
3246
by: Hadar | last post by:
Hi, I'm getting "object is currently in use elsewhere" when I use System.Drawing.Graphics.MesureString. This is what I do: My controls use a utility class the helps it to mesure strings. To get the best performance for the utility class, its members, as well as the System.Drawing.Graphics object, are static:
3
3098
by: Hitesh | last post by:
Hi, I am getting the response from another Website by using the HttpHandler in my current site. I am getting the page but all the images on that page are not appearing only placeholder are displayed. Can anybody know this issue and help me to resolve this. In past i received the response saying that i should download the image first and then parse the actual response and modify the src attribute of the
4
2580
by: John Swan | last post by:
Hello. I'm trying to create a simple program that amongst other things creates a thumbnail of an image (Bitmap) to a set size determined by the user in pixels? The problem is: All of the examples I have seen so far are in c#. Can anyone please provide reference to a c++ managed version. Thanks. John
10
1953
by: Galen Somerville | last post by:
Going from VB6 (RAD) to VB2005 (SAD) The documentation always shows graphics drawing in a paint event like Private Sub Pictcolor_Paint(ByVal sender As Object, ByVal e _ As System.Windows.Forms.PaintEventArgs) Handles PictColor.Paint End Sub But my VB6 program draws to the picturebox PictColor from many different
3
2882
by: Siv | last post by:
Hi, A little while ago I wrote a small program that allowed the user to view products from a database. The database holds the details of the products which can be viewed via a form and additionally pictures of the product are stored in an images subfolder and the database holds the file name of the relevant picture. The user can then click a button to display the picture in a pop-up window and also another button to email the potential...
5
6295
by: AliR \(VC++ MVP\) | last post by:
Hi everyone, I need to draw some rft to the screen. And I found some code out there that uses a Richedit control and sends it an EM_FORMATRANGE to do exactly that. Now I need to add scaling to this code. What I'm trying to do is to have the richedit control draw to a metafile and then I can set the ScaleTrasform of the Graphics object and draw the metafile. But everytime I call Metafile.GetHenhmetafile() it throws a "Parameter is
0
9944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9793
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11151
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10858
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9582
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7974
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7134
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5804
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5996
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.