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. Creating graphics object

Creating graphics object

Scheduled Pinned Locked Moved C#
graphicsquestion
7 Posts 4 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.
  • G Offline
    G Offline
    gwithey
    wrote on last edited by
    #1

    Is it possible to create a graphics object in the constructor of a normal calss in order to measure a string? Not using a form or control?

    Graphics g = ?
    m_nodeSize = g.MeasureString(m_name, nodeFont);

    Thanx George

    K L 2 Replies Last reply
    0
    • G gwithey

      Is it possible to create a graphics object in the constructor of a normal calss in order to measure a string? Not using a form or control?

      Graphics g = ?
      m_nodeSize = g.MeasureString(m_name, nodeFont);

      Thanx George

      K Offline
      K Offline
      kevinnicol
      wrote on last edited by
      #2

      Graphics g = Graphics.FromImage(new Bitmap(80, 80)); Give that a shot.

      G 1 Reply Last reply
      0
      • G gwithey

        Is it possible to create a graphics object in the constructor of a normal calss in order to measure a string? Not using a form or control?

        Graphics g = ?
        m_nodeSize = g.MeasureString(m_name, nodeFont);

        Thanx George

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        http://msdn.microsoft.com/en-us/library/5y289054%28VS.80%29.aspx[^]

        G 1 Reply Last reply
        0
        • K kevinnicol

          Graphics g = Graphics.FromImage(new Bitmap(80, 80)); Give that a shot.

          G Offline
          G Offline
          gwithey
          wrote on last edited by
          #4

          Thank you Kevinnicol That worked great, i was looking for something like that.

          public static SizeF MeasureString(string s, Font font)
          {
          SizeF result;
          using (var image = new Bitmap(0, 0))
          {
          using (var g = Graphics.FromImage(image))
          {
          result = g.MeasureString(s, font);
          }
          }

              return result;
          }
          

          Did see this example but preffer the below:

          // Generate nodeSize by measuring the name of the node
          Graphics g = Graphics.FromImage(new Bitmap(80, 80));
          m_nodeSize = g.MeasureString(m_name, nodeFont);

          Thanx George

          P 1 Reply Last reply
          0
          • L Lost User

            http://msdn.microsoft.com/en-us/library/5y289054%28VS.80%29.aspx[^]

            G Offline
            G Offline
            gwithey
            wrote on last edited by
            #5

            Thank you stancrm I saw this however couldnt find a solution as:

            Graphics g;
            // Sets g to a graphics object representing the drawing surface of the
            // control or form g is a member of.
            g = this.CreateGraphics();

            Only works in a control or on a form. Not in a normal C# class Thanx George

            L 1 Reply Last reply
            0
            • G gwithey

              Thank you Kevinnicol That worked great, i was looking for something like that.

              public static SizeF MeasureString(string s, Font font)
              {
              SizeF result;
              using (var image = new Bitmap(0, 0))
              {
              using (var g = Graphics.FromImage(image))
              {
              result = g.MeasureString(s, font);
              }
              }

                  return result;
              }
              

              Did see this example but preffer the below:

              // Generate nodeSize by measuring the name of the node
              Graphics g = Graphics.FromImage(new Bitmap(80, 80));
              m_nodeSize = g.MeasureString(m_name, nodeFont);

              Thanx George

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              gwithey wrote:

              Graphics g = Graphics.FromImage(new Bitmap(80, 80)); m_nodeSize = g.MeasureString(m_name, nodeFont);

              Sorry, did you mean to say that you prefer this version? If so, please don't - use the version above it instead as that disposes of managed resources instead.

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys | Onyx

              1 Reply Last reply
              0
              • G gwithey

                Thank you stancrm I saw this however couldnt find a solution as:

                Graphics g;
                // Sets g to a graphics object representing the drawing surface of the
                // control or form g is a member of.
                g = this.CreateGraphics();

                Only works in a control or on a form. Not in a normal C# class Thanx George

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                See the sample :

                Bitmap myBitmap = new Bitmap(@"C:\Documents and
                Settings\Joe\Pics\myPic.bmp");
                Graphics g = Graphics.FromImage(myBitmap);

                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