0

I can draw a line on image by using below code. Width of pen is fixed as 5. There is a dropdown for selecting resolution of screen (640*480,352*288,320*240 etc). When selecting it, screen will change into that resolution. As a result the width of pen seems different. In each resolution pen width looks different. How can I fix the width of pen for all resolution?

  public void DrawLineInt(Bitmap bmp)
{
    Pen blackPen = new Pen(Color.Black, 5);

    int x1 = 100;
    int y1 = 100;
    int x2 = 500;
    int y2 = 100;
    // Draw line to screen.
    using (var graphics = Graphics.FromImage(bmp))
    {
        graphics.DrawLine(blackPen, x1, y1, x2, y2);
    }
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
user2431727
  • 877
  • 2
  • 15
  • 46
  • 1
    I imagine the width of the pen remains the same, but appears bigger because the screen is smaller or bigger. Create a method that will scale your pen width in the same ratio as your screen resolution. – Andrew Jan 30 '19 at 07:16
  • yes it appears bigger. you mean penwidth= (screen width/height)*1/100 ? – user2431727 Jan 30 '19 at 07:22
  • What is the *resolution of screen*? Do you mean the size of the Bitmap? – Jimi Jan 30 '19 at 07:25
  • 1
    Can you provide a [mcve]? Ideally some code that uses the same size pen for two images so that we can see what the difference you're describing is. – ProgrammingLlama Jan 30 '19 at 07:28
  • @jimi actually it is a video. videoSource1.VideoResolution = videoSource1.VideoCapabilities[comboBox1Resolution.SelectedIndex]; – user2431727 Jan 30 '19 at 07:38
  • 1
    You could scale the Pen on the scale factor defined by a reference measure. E.g., if the reference measure is `640x480`, then the Pen at `320x240` could be `int p1 = (int)(p * ((float)320 / 640)) + 1;`. Which will give you `3`. So, if you scale to `1280x1024`, the Pen would be `11`. It's better to have odd Pen sizes (because of the way the Pen is used, half inside and half outside the bounding rectangle). – Jimi Jan 30 '19 at 07:39
  • But you could also. probably better, use floating point values for the Pen. – Jimi Jan 30 '19 at 08:05

0 Answers0