Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Trying to draw using System.Drawing.Drawing2D

Trying to draw using System.Drawing.Drawing2D

Scheduled Pinned Locked Moved C#
csharpvisual-studiographicshelptutorial
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    laura1316
    wrote on last edited by
    #1

    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:

    C L 2 Replies Last reply
    0
    • L laura1316

      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:

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      L 1 Reply Last reply
      0
      • C Christian Graus

        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 )

        L Offline
        L Offline
        laura1316
        wrote on last edited by
        #3

        Thanks so much! That worked. Now how can I dry a small square and display it in the form at certain locations?

        L 1 Reply Last reply
        0
        • L laura1316

          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:

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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]

          1 Reply Last reply
          0
          • L laura1316

            Thanks so much! That worked. Now how can I dry a small square and display it in the form at certain locations?

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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]

            L 1 Reply Last reply
            0
            • L Luc Pattyn

              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]

              L Offline
              L Offline
              laura1316
              wrote on last edited by
              #6

              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

              C L 2 Replies Last reply
              0
              • L laura1316

                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

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #7

                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 )

                1 Reply Last reply
                0
                • L laura1316

                  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

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  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]

                  C 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    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]

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #9

                    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 )

                    L 1 Reply Last reply
                    0
                    • C Christian Graus

                      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 )

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      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]

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups