Pen width

grant1842

New member
Joined
Nov 4, 2018
Messages
3
Programming Experience
Beginner
New to C#
I have some c# code i am trying to modify.
I want to set the pen.with to a fixed with when id draws on the canvas.

I did not write this code just trying to find out where the pen.with is.
private void DrawMainCanvas()
{
    using (Graphics g = Graphics.FromImage(todraw_bmp))
    {
        g.Clear(Color.White);
        g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));

        if (sb_ShowAllLines.Checked)
        {
            DrawRects(todraw_bmp, rects);
        }


        foreach (DrawRect r in LinesToDisplay)
        {
            if (r.str != "")
            {


                g.DrawString(r.str, r.font, new SolidBrush(r.clr), r.rect.X, r.rect.Y);
                SizeF textsize = g.MeasureString(r.str, r.font);

                r.rect.Width = (int) textsize.Width;
                //r.rect.Width = 16;
                r.rect.Height = (int) textsize.Height;
            }
            else
            {
                if (sb_Transparent.Checked)
                {
                    g.FillRectangle(new SolidBrush(Color.FromArgb(128, r.clr.R, r.clr.G, r.clr.B)), r.rect);
                }
                else
                {
                    g.FillRectangle(new SolidBrush(r.clr), r.rect);
                }
            }
        }
    }

    Image.bmp_to_screen();
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (loaded)
    {
        if (ActiveObj != null)
        {
            if (ActiveObj.obj.str == "") //selected line
            {
                Brush BrushRectSmall = new HatchBrush(HatchStyle.Percent50, Color.FromArgb(128, 128, 128, 128));
                e.Graphics.FillRectangle(BrushRectSmall, Image.to_scr(ActiveObj.PointRect(1)));
                e.Graphics.FillRectangle(BrushRectSmall, Image.to_scr(ActiveObj.PointRect(2)));
                e.Graphics.DrawLine(Pens.Black, Center(Image.to_scr(ActiveObj.PointRect(1))),
                    Center(Image.to_scr(ActiveObj.PointRect(2))));

            }
            else //selected text
            {
                e.Graphics.DrawRectangle(Pens.Black, Image.to_scr(ActiveObj.DrawRect()));
            }
        }
    }
}

Thanks for any help and comments
 
Last edited by a moderator:
I have edited your code so that it has proper formatting and no missing braces. Please do that for us in future. If you'd like us to help you with code, please make it as easy to read as possible.

As for the question, can we assume that you mean the Width property of the Pen object is what you want to set? If so then you need to create (and then destroy) your own Pen objects instead of using standard ones from the Pens class. For instance, your Paint event handler uses Pens.Black twice. Such a standard Pen has a Width of 1. If you want specify a width yourself then create your own Pen and use it instead, e.g.
using (var p = new Pen(Color.Black, 2.0F))
{
    // Use p here.
}

Inside that 'using' block, 'p' is a Black Pen, like Pens.Black, but with a Width of 2 rather than 1.
 
Thanks for your reply.
I have copied and paste this code out of visual studio .
I hope it is formatted for you to see better now.
Please let me know if this is ok.

i have changed the code.
private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (loaded)
            {
                if (ActiveObj != null)
                {
                    if (ActiveObj.obj.str == "") //selected line
                    {
                        using (var p = new Pen(Color.Black, 8.0F))
                        {
                            Brush BrushRectSmall = new HatchBrush(HatchStyle.Percent50, Color.FromArgb(128, 128, 128, 128));
                            e.Graphics.FillRectangle(BrushRectSmall, Image.to_scr(ActiveObj.PointRect(1)));
                            e.Graphics.FillRectangle(BrushRectSmall, Image.to_scr(ActiveObj.PointRect(2)));
                            e.Graphics.DrawLine(p,Center(Image.to_scr(ActiveObj.PointRect(1))), Center(Image.to_scr(ActiveObj.PointRect(2))));
                        }
                        
                        
                        }
                    else //selected text
                    {
                        using (var p = new Pen(Color.Black, 8.0F))
                        {
                            e.Graphics.DrawRectangle(p, Image.to_scr(ActiveObj.DrawRect()));
                        }
                            
                    }
                }
            }
        }



It still has small lines when I use it.
Perhaps it is somewhere else in the c# code.
I am very new to c#.
Thanks for your help.
 
Last edited:
I do not see line numbers in the code i pasted. I must be doing something wronge
I used the





Now seems to have the line numbers.
Is this ok ?
Thanks for the help.
 
Last edited:
The CODE tag doesn't display line numbers and doesn't use syntax highlighting. It's good if you want to add your own formatting for specific reasons, e.g. highlight a particular line. The XCODE tag does add line numbers and syntax highlighting but you may have to refresh the page to see it. That is generally the better option for code snippets. The last code you posted should look like this:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (loaded)
    {
        if (ActiveObj != null)
        {
            if (ActiveObj.obj.str == "") //selected line
            {
                using (var p = new Pen(Color.Black, 8.0F))
                {
                    Brush BrushRectSmall = new HatchBrush(HatchStyle.Percent50, Color.FromArgb(128, 128, 128, 128));
                    e.Graphics.FillRectangle(BrushRectSmall, Image.to_scr(ActiveObj.PointRect(1)));
                    e.Graphics.FillRectangle(BrushRectSmall, Image.to_scr(ActiveObj.PointRect(2)));
                    e.Graphics.DrawLine(p, Center(Image.to_scr(ActiveObj.PointRect(1))), Center(Image.to_scr(ActiveObj.PointRect(2))));
                }
            }
            else //selected text
            {
                using (var p = new Pen(Color.Black, 8.0F))
                {
                    e.Graphics.DrawRectangle(p, Image.to_scr(ActiveObj.DrawRect()));
                }
            }
        }
    }
}

You should check your IDE settings for code formatting and then, if a section is not formatted correctly, deleting the last brace and retyping it should format the whole block. That said, I have ReSharper installed so I'm not always sure what functionality it is providing and what's standard in VS.

As for the issue, I can't see anything obvious. You'll just have to do some testing and debugging. Instead of starting with a big chunk of code copied from elsewhere, write something yourself. Start with a single DrawLine call and see if you can draw a line with the thickness you want. If you can then start building up your code form there, bit by bit. Software development does not consist of copying code, running it and then asking others to fix it. Debugging is a critical skill, as is being able to isolate specific functionality and test it.
 
Back
Top Bottom