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. Other Discussions
  3. IT & Infrastructure
  4. Maybe a C# bug

Maybe a C# bug

Scheduled Pinned Locked Moved IT & Infrastructure
helpcsharpquestionlearning
6 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.
  • D Offline
    D Offline
    Duong Tien Nam
    wrote on last edited by
    #1

    I try this code by overriding control's OnPaint method:

    Pen p = new Pen(Color.Red, UnitConverter.PixelToInch(1));
    p.DashStyle = DashStyle.Dot;
    g.DrawLine(p, 1, 1, 50, 1);

    The result seems like it draws a dot with 1 inch width, like this:

    __________ __________ __________ __________

    but if you change the pen width to 2 pixels (means 0.00xx inch of course), we have the correct result:

    ...................................................................

    (in 2 pixel width). am I misunderstand the .NET method to draw line or is it a bug? Please help me to solve this problem, thank for you help.

    J 1 Reply Last reply
    0
    • D Duong Tien Nam

      I try this code by overriding control's OnPaint method:

      Pen p = new Pen(Color.Red, UnitConverter.PixelToInch(1));
      p.DashStyle = DashStyle.Dot;
      g.DrawLine(p, 1, 1, 50, 1);

      The result seems like it draws a dot with 1 inch width, like this:

      __________ __________ __________ __________

      but if you change the pen width to 2 pixels (means 0.00xx inch of course), we have the correct result:

      ...................................................................

      (in 2 pixel width). am I misunderstand the .NET method to draw line or is it a bug? Please help me to solve this problem, thank for you help.

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      The question is what is the implementation of UnitConverter.PixelToInch(1) because I cant find that anywhere in the framework. However, I have tried many values for the 2nd parameter of Pen and I always seemto get the right result.

      M D 2 Replies Last reply
      0
      • J J4amieC

        The question is what is the implementation of UnitConverter.PixelToInch(1) because I cant find that anywhere in the framework. However, I have tried many values for the 2nd parameter of Pen and I always seemto get the right result.

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        It's the digital Dutch unit converter[^] of course :)

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        1 Reply Last reply
        0
        • J J4amieC

          The question is what is the implementation of UnitConverter.PixelToInch(1) because I cant find that anywhere in the framework. However, I have tried many values for the 2nd parameter of Pen and I always seemto get the right result.

          D Offline
          D Offline
          Duong Tien Nam
          wrote on last edited by
          #4

          I'm sorry not to show you enough information. The first thing I forget is the PageUnit change for graphics object

          Graphics g = e.Graphics;
          g.PageUnit = GraphicsUnit.Inch;
          Pen p = new Pen(Color.Red, UnitConverter.PixelToInch(1));
          p.DashStyle = DashStyle.Dot;
          g.DrawLine(p, 1, 1, 50, 1);

          The second: UnitConvert is just a class I write to change the pen width:

          public static class UnitConverter
          {
          static float _dpi = 96;
          public static float Dpi
          {
          get { return UnitConverter._dpi; }
          set { UnitConverter._dpi = value; }
          }

              public static float PixelToUnit(double pixel, GraphicsUnit unit)
              {
                  double result = 0;
                  switch (unit)
                  {
                      case GraphicsUnit.Inch:
                          result = pixel / Dpi;
                          break;
          
                      case GraphicsUnit.Pixel:
                          result = pixel;
                          break;
          
                      case GraphicsUnit.Millimeter:
                          result = pixel / (Dpi / 2.54) \* 10;
                          break;
          
                      case GraphicsUnit.Point:
                          result = pixel / (Dpi / 72);
                          break;
                  }
          
                  return (float)result;
              }
          }
          

          }

          J 1 Reply Last reply
          0
          • D Duong Tien Nam

            I'm sorry not to show you enough information. The first thing I forget is the PageUnit change for graphics object

            Graphics g = e.Graphics;
            g.PageUnit = GraphicsUnit.Inch;
            Pen p = new Pen(Color.Red, UnitConverter.PixelToInch(1));
            p.DashStyle = DashStyle.Dot;
            g.DrawLine(p, 1, 1, 50, 1);

            The second: UnitConvert is just a class I write to change the pen width:

            public static class UnitConverter
            {
            static float _dpi = 96;
            public static float Dpi
            {
            get { return UnitConverter._dpi; }
            set { UnitConverter._dpi = value; }
            }

                public static float PixelToUnit(double pixel, GraphicsUnit unit)
                {
                    double result = 0;
                    switch (unit)
                    {
                        case GraphicsUnit.Inch:
                            result = pixel / Dpi;
                            break;
            
                        case GraphicsUnit.Pixel:
                            result = pixel;
                            break;
            
                        case GraphicsUnit.Millimeter:
                            result = pixel / (Dpi / 2.54) \* 10;
                            break;
            
                        case GraphicsUnit.Point:
                            result = pixel / (Dpi / 72);
                            break;
                    }
            
                    return (float)result;
                }
            }
            

            }

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            Duong Tien Nam wrote:

            UnitConverter.PixelToInch

            Duong Tien Nam wrote:

            public static float PixelToUnit(double pixel, GraphicsUnit unit)

            Your code still doesnt compile, but I see what you're doing now. I added this method to UnitConverter:

            public static float PixelToInch(double pixel)
            {
            return PixelToUnit(pixel, GraphicsUnit.Inch);
            }

            I get the same result you do. However I notice that this line: g.PageUnit = GraphicsUnit.Inch; if left as Pixel produces the correct results. Remember that you're calculating Picels from an Inch value, so no need to set the PageUnit to Inches. If you want to set it to inches you may as well not have the UnitConverter stuff in there:

            Graphics g = e.Graphics;
            g.PageUnit = GraphicsUnit.Inch;
            Pen p = new Pen(Color.Red, 1F);
            p.DashStyle = DashStyle.Dot;
            g.DrawLine(p, 1, 1, 500, 1);

            D 1 Reply Last reply
            0
            • J J4amieC

              Duong Tien Nam wrote:

              UnitConverter.PixelToInch

              Duong Tien Nam wrote:

              public static float PixelToUnit(double pixel, GraphicsUnit unit)

              Your code still doesnt compile, but I see what you're doing now. I added this method to UnitConverter:

              public static float PixelToInch(double pixel)
              {
              return PixelToUnit(pixel, GraphicsUnit.Inch);
              }

              I get the same result you do. However I notice that this line: g.PageUnit = GraphicsUnit.Inch; if left as Pixel produces the correct results. Remember that you're calculating Picels from an Inch value, so no need to set the PageUnit to Inches. If you want to set it to inches you may as well not have the UnitConverter stuff in there:

              Graphics g = e.Graphics;
              g.PageUnit = GraphicsUnit.Inch;
              Pen p = new Pen(Color.Red, 1F);
              p.DashStyle = DashStyle.Dot;
              g.DrawLine(p, 1, 1, 500, 1);

              D Offline
              D Offline
              Duong Tien Nam
              wrote on last edited by
              #6

              The reason I have to change the graphics' PageUnit to inch because I want all to draw in inch measurement. If you test you code above, you will see that it draws a dotted line with 1 inch width. So you need to convert 1 inch width pen to 1 pixel with pen with PixelToInch method above. I hope you can understand my explanation because English is not native language. Thank you.

              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