Trying to draw using System.Drawing.Drawing2D
-
Hi all I am new to C# and real new to drawing with it. I have been looking online and have been unsuccessful in finding tutorials or examples in what I am trying to do. I am trying to draw a very small square about 1cm in size. I want to draw it at a certain location on the form. Does anyone know how to do this or can help point me in the right direction with sample code. I have a timer implemented and when the timer interrupt occurs I want to draw the small square in a differnt location in the form. Right now its not drawing anything. I call my paint function in the timer event function by doing the following: this.Paint += new PaintEventHandler(XYZ_Paint); But nothing gets displayed to the form, not even a simple line or anything. Why is this? Please help. I am using C# and Visual Studio 2005. Thanks, Laura:sigh:
-
Hi all I am new to C# and real new to drawing with it. I have been looking online and have been unsuccessful in finding tutorials or examples in what I am trying to do. I am trying to draw a very small square about 1cm in size. I want to draw it at a certain location on the form. Does anyone know how to do this or can help point me in the right direction with sample code. I have a timer implemented and when the timer interrupt occurs I want to draw the small square in a differnt location in the form. Right now its not drawing anything. I call my paint function in the timer event function by doing the following: this.Paint += new PaintEventHandler(XYZ_Paint); But nothing gets displayed to the form, not even a simple line or anything. Why is this? Please help. I am using C# and Visual Studio 2005. Thanks, Laura:sigh:
Your timer function needs to call Invalidate(), this will force the paint event to occur.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Your timer function needs to call Invalidate(), this will force the paint event to occur.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Hi all I am new to C# and real new to drawing with it. I have been looking online and have been unsuccessful in finding tutorials or examples in what I am trying to do. I am trying to draw a very small square about 1cm in size. I want to draw it at a certain location on the form. Does anyone know how to do this or can help point me in the right direction with sample code. I have a timer implemented and when the timer interrupt occurs I want to draw the small square in a differnt location in the form. Right now its not drawing anything. I call my paint function in the timer event function by doing the following: this.Paint += new PaintEventHandler(XYZ_Paint); But nothing gets displayed to the form, not even a simple line or anything. Why is this? Please help. I am using C# and Visual Studio 2005. Thanks, Laura:sigh:
Hi, and of course you must execute the line this.Paint += new PaintEventHandler(XYZ_Paint); once, typically this is done in the form's constructor. :)
Luc Pattyn [My Articles]
-
Thanks so much! That worked. Now how can I dry a small square and display it in the form at certain locations?
Add your painting commands to your paint handler (you called it XYZ_Paint). Just get the Graphics from the PaintEventArgs, and use DrawLine, FillRectangle and all the other nice Graphics methods. For animation, use coordinate variables that get changed by your timer tick handler. :)
Luc Pattyn [My Articles]
-
Add your painting commands to your paint handler (you called it XYZ_Paint). Just get the Graphics from the PaintEventArgs, and use DrawLine, FillRectangle and all the other nice Graphics methods. For animation, use coordinate variables that get changed by your timer tick handler. :)
Luc Pattyn [My Articles]
-
Could you give me a coded example of drawing a 1cm square or a point whichever is easier on a form at location (15,25)? Thanks so much!:-D
It's just e.Graphics.FillRectange(15, 25, 20, 20); assuming that 20 pixels is about 1 cm.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Could you give me a coded example of drawing a 1cm square or a point whichever is easier on a form at location (15,25)? Thanks so much!:-D
Hi, in the form's paint handler I would do:
Graphics g=e.Graphics; // e=PaintEventArgs g.DrawRectangle(Pens.Black, 15, 25, w, h);
now what are the right values for w and h ? for a point (or a very small square), set them both to 1 or 2. for an exact size, it is complex: - first you must assume that your system settings are correct, i.e. the right dpi have been set (by default a monitor gets installed at 96 dpi, which is less than what the average modern monitor is showing). You can change them to the correct value by first calculating the value, then setting it interactively under Display Properties/Settings/ Advanced/DPI Settings: select custom settings, and then enter the number as a percentage of 96 dpi. remark: everything will look smaller now (except for the desktop, which now can hold much more stuff); you may want to increase the system's default font size, icon size, etc. - then your program should find out what the system's dpi settings are, by calling something like
Bitmap bitmap=new Bitmap(100, 100); Graphics gBM=Graphics.FromImage(bitmap); int Dpi=gBM.DpiX; gBM.Dispose();
This has to be done only once in a program (unless you want to cope with a change in the setting while the program is running...) you dont have to worry about DpiY, it will equal DpiX (you could only enter an overall dpi value in the previous step, so best is to calculate it diagonally !) Now you can calculate how many pixels are needed to travel 1 cm, and that's the right value for w and h. remark: if your system settings are incorrect, dont bother calling gBM.DpiX, since everything would be based on a lie, hence wrong. :)
Luc Pattyn [My Articles]
-
Hi, in the form's paint handler I would do:
Graphics g=e.Graphics; // e=PaintEventArgs g.DrawRectangle(Pens.Black, 15, 25, w, h);
now what are the right values for w and h ? for a point (or a very small square), set them both to 1 or 2. for an exact size, it is complex: - first you must assume that your system settings are correct, i.e. the right dpi have been set (by default a monitor gets installed at 96 dpi, which is less than what the average modern monitor is showing). You can change them to the correct value by first calculating the value, then setting it interactively under Display Properties/Settings/ Advanced/DPI Settings: select custom settings, and then enter the number as a percentage of 96 dpi. remark: everything will look smaller now (except for the desktop, which now can hold much more stuff); you may want to increase the system's default font size, icon size, etc. - then your program should find out what the system's dpi settings are, by calling something like
Bitmap bitmap=new Bitmap(100, 100); Graphics gBM=Graphics.FromImage(bitmap); int Dpi=gBM.DpiX; gBM.Dispose();
This has to be done only once in a program (unless you want to cope with a change in the setting while the program is running...) you dont have to worry about DpiY, it will equal DpiX (you could only enter an overall dpi value in the previous step, so best is to calculate it diagonally !) Now you can calculate how many pixels are needed to travel 1 cm, and that's the right value for w and h. remark: if your system settings are incorrect, dont bother calling gBM.DpiX, since everything would be based on a lie, hence wrong. :)
Luc Pattyn [My Articles]
Yeah, I did not respond to the 1cm bit, because it's never going to work, in the real world.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Yeah, I did not respond to the 1cm bit, because it's never going to work, in the real world.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Well, I started working on a Mac long time ago, all monitors were 72 dpi and all programs were real size, even Microsoft Word. It was marvelous, you could hold a printed page against the monitor and print and screen would fit up to the millimeter. When I switched to PCs and Windows (98, NT, XP), I insisted on keeping real size, although monitors improved, one of them is at 135 dpi now. And I still manage in most applications; biggest problem is with some web pages that insist on petite fonts, ignoring system settings. So I created a small background app (in C# of course) that upon ALT-S switches resolutions to some 80% of reality (and back) for easier reading those difficult pages, keeping track of desktop icon positions, and window sizes, restoring as much as possible when hitting ALT-S a second time. The one thing it does not do is restore the size of the windows that were minimized (I dont know how to get/set these values, I think they are well hidden inside Windows Explorer). Any suggestion here would be welcome. :)
Luc Pattyn [My Articles]