C# How to Use Paint and the C# Graphics Class to Draw Lines

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ธ.ค. 2024

ความคิดเห็น • 111

  • @info_1941
    @info_1941 7 ปีที่แล้ว

    Just what I needed. Just complicated enough to get me started but short enough to get going quickly. Thanks much!!

  • @martinomasreiter3124
    @martinomasreiter3124 9 ปีที่แล้ว +16

    Cool drawings, well done. Some suggestions:
    1) Use the e.graphics EventArgs of your paint event for drawing, instead of creating a new instance.
    2) Use control.invalidate method to force to repaint the full control. (slight difference to your refresh method)
    PS: We did a similar thing in phyton, fun stuff ;)

  • @TheCerubus
    @TheCerubus 9 ปีที่แล้ว +1

    Great video program works as promised (once I found the line of code I left out) and gave me a basic understanding of C# graphics. The only negative is that I believe that it is still recommended to dispose of the pen and other drawing tools when you are done.

  • @ToyLikeChannel
    @ToyLikeChannel 5 ปีที่แล้ว +1

    Wow, it works under SharpDevelop! Thank you, your video was really helpful, your explanation was clear and detailed.

  • @NimishP
    @NimishP 2 ปีที่แล้ว

    @27:10 To convert degrees to radian multiply degrees with PI / 180 instead. This will reduce error to a great extents.
    Math library has predefined constant PI.

  • @lemonandgaming6013
    @lemonandgaming6013 6 ปีที่แล้ว

    oh boi the abstract art this program makes is impressive

  • @brianbrewster6532
    @brianbrewster6532 6 ปีที่แล้ว

    Thanks, Chris. Really got my creative juices flowing watching this fascinating video. I think if I can translate this to VB I might be able to experiment with this powerful app to make intricate patterns for a project I'm working on. Also, at 35:00, I saw a semblance of a wormhole. This would be a dream come true if I could figure out how to draw something like this and turn it into an animation! Thank you so much, sir.

  • @Cruz0e
    @Cruz0e 2 ปีที่แล้ว +2

    your conversion is equal to * π/180 (because 180 degree in radians is π), so if you want to, you could use *Math.Pi/180 instead
    easy to memorize 180=π (once you understand what it is)
    π is a perimeter of a semicircle (180 degree) with radius of 1
    if 180° is π then 1° is equal to π/180
    your angle is ω[deg] then your ω[rad] = ω[deg]*π/180
    (I used 2π/360 first, bit mixed it up so I edited to correct it, but it's the same thing 2π is the perimeter of a circle)

  • @Ivan67968
    @Ivan67968 4 ปีที่แล้ว

    Vert nice and interesting! Learned a lot! Thank you👍😀

  • @omerodabasi
    @omerodabasi 8 ปีที่แล้ว +6

    Very helpful tutorial, subbed!

  • @NetMistro
    @NetMistro 3 ปีที่แล้ว

    Thanks for this. You explain very well.

  • @gunteramberg1949
    @gunteramberg1949 2 ปีที่แล้ว

    so many thanks, this is great
    one question to you, if you want to go a step back, what is the code ??

  • @Greebstreebling
    @Greebstreebling 2 ปีที่แล้ว +1

    thank you for sharing this, there's loads of good stuff, presented in an engaging and effective way.
    In my application I set a bacground image in the panel. When I do that the lines do not draw over the background image. Any ideas ?

    • @Greebstreebling
      @Greebstreebling 2 ปีที่แล้ว

      I found an answer and what worked for me was to add the following:
      static Bitmap baseImage = (Bitmap)Image.FromFile(@"C:\Compass_Images\Capture.png");
      and in the Canvas_Paint method, add in the following line, just before drawline():
      Canvas.BackgroundImage = baseImage;
      When this runs, the line is drawn on top of the background image.

  • @boliussa
    @boliussa 9 ปีที่แล้ว

    By 20:32 You've declared Graphics g,.. and done g.drawLines.... How does it know what panel or form to draw it on?

    • @tomwilson4572
      @tomwilson4572 9 ปีที่แล้ว

      +boliussa Use the Graphics object passed to the Paint event handler
      private void canvas_Paint(object sender, PaintEventArgs e)
      {
      for (int i = 0; i < _lines; ++i) // _lines=Int32.Parse(number_of_lines.Text)
      {
      DrawLine(e.Graphics);
      }
      }
      Modify DrawLine to use this graphics object instead of canvas.CreateGraphics()
      private void DrawLine(Graphics g)
      {
      ...
      g.DrawLines(_pen, points);
      ...
      }

    • @boliussa
      @boliussa 9 ปีที่แล้ว

      Thanks. Where you write " use this graphics object instead of canvas.CreateGraphics()" That suggests that's advantageous.. So, what is the difference between doing canvas.createGraphics() and canvas.DrawLines() vs doing e.Graphics.Drawlines/g.DrawLines?

    • @tomwilson4572
      @tomwilson4572 9 ปีที่แล้ว

      +boliussa There is no functional difference, however, +Chris Merritt is creating a second Graphics object, when there is already one available via PaintEventArgs. It is also GDI+ good practice and saves resources.

  • @titusdrake9037
    @titusdrake9037 4 ปีที่แล้ว

    Great tutorial. Only one question: where, in the code, we use my_increment variable? Or it is not used? Thankyou!

  • @zacheryhenry6580
    @zacheryhenry6580 8 ปีที่แล้ว

    We're you able to see the code I pasted here or do I need to paste it in the other location you mentioned? I feel like it has to be something simple it's driving me nuts

  • @james970027
    @james970027 3 ปีที่แล้ว +1

    great demonstration it helped a lot - just wish visual studio was actually a good software, i spent way over an hour to basically follow what you have done just down to how purely buggy visual studio is on my laptop sadly lol

  • @shohanvlogs1987
    @shohanvlogs1987 3 ปีที่แล้ว

    Good video

  • @tuananhvu6448
    @tuananhvu6448 3 ปีที่แล้ว

    It's help me a lot, thanks 4 tutorial

  • @bboyrd01
    @bboyrd01 9 ปีที่แล้ว

    Thank you So much chris merritt, This will be highly useful for my research.

  • @zacheryhenry6580
    @zacheryhenry6580 8 ปีที่แล้ว

    I think I must have something wrong with the button click function but I can't find it. If I manually input the number of lines and everything and start the program it works correctly but then when I hit go the canvas just wipes. So I think it does the line drawing portion correctly and when I click the button it wipes the canvas but doesn't appear to re-do everything when I hit go idk

  • @nasredeenyousif5911
    @nasredeenyousif5911 4 ปีที่แล้ว

    Good tutorial ...I see that you do not use the increment parameter
    what are the dimensions (initial) of your form (Form1)?

  • @butterdogestarwars6709
    @butterdogestarwars6709 4 ปีที่แล้ว

    Is there a way to save the image?

  • @tomwilson4572
    @tomwilson4572 9 ปีที่แล้ว +1

    A neat idea for demonstrating Graphics in a WinForm though the implementation could be a little tidier.

    • @langtonmwanza6689
      @langtonmwanza6689 8 ปีที่แล้ว

      i know right like having the draw function just take the graphics surface as an argument

  • @KeenanWoodall
    @KeenanWoodall 10 ปีที่แล้ว

    Could you post the source. I'm very new, typed something wrong, and visual is freaking out at me. I can't open the script so I can't fix it. Also where it says "private void canvas_Paint(object sender, PaintEventArgs e)" it said for me "private void panel1_Paint(object sender, PaintEventArgs e)" I changed its name to panel in the properties window a few minutes prior, so I wan't sure why is said panel1 instead of canvas. I think it messed everything up when I changed the methods name to canvas_Paint

  • @gopalakrishnanudhayasuriya1983
    @gopalakrishnanudhayasuriya1983 10 ปีที่แล้ว +1

    Hey,is it possible fit drawing inside the panel,when i am giving high values to line it goes outside of panel...help me

    • @xorxpert
      @xorxpert 6 ปีที่แล้ว

      Gopalakrishnan Udhayasuriyan then you clamp/bound the values before it’s drawn 🤦‍♂️
      For ex, if X > width then X = width - X vice versa. Do the same for Y

  • @harishmehta2992
    @harishmehta2992 3 ปีที่แล้ว

    Thanks, very good video

  • @انامسلموكفى
    @انامسلموكفى 3 ปีที่แล้ว

    I need Help please i how i could draw a schape in runtime

  • @x_salaheddinechannel_x
    @x_salaheddinechannel_x 4 ปีที่แล้ว

    Hello,
    I want to help me,
    Can i put the point = (12.5 , 14.6),
    Thank you.

  • @trunghoale8584
    @trunghoale8584 ปีที่แล้ว

    System.NullReferenceException: 'Object reference not set to an instance of an object.'
    g was null.
    Help me?????

  • @BdyPhrk
    @BdyPhrk 8 ปีที่แล้ว +1

    Does this use the CPU or GPU to draw ?

    • @JayPaysLPs
      @JayPaysLPs 8 ปีที่แล้ว +1

      +B100dyPhr34k CPU

  • @tomg2002
    @tomg2002 5 ปีที่แล้ว

    Great video, thanks! Does Graphics.DrawLine create something that can be counted, later? Like... could you say, for each... line that is green... count'em up? Is it an vector that persists beyond changing the pixel colors? I'm still kicking around my first "good idea" and your demonstration helped a lot.

    • @NimishP
      @NimishP 2 ปีที่แล้ว

      AutoCAD or MicroStation would be better choice if you want to manipulate these entities for future.

  • @hz6612
    @hz6612 4 ปีที่แล้ว

    Amaaaaaazing!!

  • @Obishman
    @Obishman 5 ปีที่แล้ว

    Thanks, excellent video

  • @ddragomirov
    @ddragomirov 8 ปีที่แล้ว

    Hi, I want to ask you if there is a way that I can alter the speed of the pen so that it looks like an animation ?

    • @chrismerritt7291
      @chrismerritt7291  8 ปีที่แล้ว +1

      +Dayan Dragomirov Hmmm. You could put the main thread to sleep after every line is drawn. google C# Thread.Sleep

    • @ddragomirov
      @ddragomirov 8 ปีที่แล้ว

      Thank you for the answer, but I want to slow down the drawing of the line itself so I don't think sleeping would work in that case.

    • @MrKaprson
      @MrKaprson 8 ปีที่แล้ว +2

      Drawing of the line itself is a single procces that you cannot influence. You would have to create a cycle where length of the line (in pixels) is the amount of steps and one single dot must be drawn in each step. Then you can thread.Sleep(theAmounOfATime).

    • @MrKaprson
      @MrKaprson 8 ปีที่แล้ว +1

      Hope you get me >]

  • @mr.tuttifrutti9280
    @mr.tuttifrutti9280 8 ปีที่แล้ว

    It was cool, dude!

  • @Bostonite1985
    @Bostonite1985 10 ปีที่แล้ว

    Every time I try to run the program, I get a 'build errors' message. What has gone wrong?

  • @ahmadsaleh4628
    @ahmadsaleh4628 8 ปีที่แล้ว

    Hello
    Please help I want CaseTools in C# to do something like the shadow in photoshop
    anything else please help me

  • @doro516
    @doro516 5 ปีที่แล้ว

    do you what can I use instead of canvas, in my visual studio version it doesn't include canvas

    • @ferna2294
      @ferna2294 5 ปีที่แล้ว

      "canvas" was the name of the panel he used to draw. You have to name your panel "canvas" in order to use it.
      8:58 On the right of the screen you can read "(name) canvas"

  • @chriswinchester4805
    @chriswinchester4805 10 ปีที่แล้ว

    Very good video, not hard to follow at all. I did, however, have a problem with creating a null Graphics object. I am using VS 2013. What should I put in the parameters?

    • @chriswinchester4805
      @chriswinchester4805 10 ปีที่แล้ว

      I found that I left out some important code under the drawLine function. All is good now.

    • @trunghoale8584
      @trunghoale8584 ปีที่แล้ว

      @@chriswinchester4805 can you see that problem? my problem: System.NullReferenceException: 'Object reference not set to an instance of an object.'
      g was null

  • @chrisconn5649
    @chrisconn5649 8 ปีที่แล้ว

    I appreciate the video, the graphics basics were helpful, but I was getting a little frustrated that you weren't quite sure where you were going sometimes. Regardless, thanks. Also, I think Pi/180 coverts radians to angles

  • @hasnatqureshi1188
    @hasnatqureshi1188 8 ปีที่แล้ว +1

    Can any one tell me how can I simulate data structures in c# like stacks,queues and link lists

    • @chrismerritt7291
      @chrismerritt7291  8 ปีที่แล้ว +1

      +Hasnat Adil That would be a tutorial all of its own. You can build your own classes to make linked lists, queues, and stacks. Perhaps I'll build a tutorial in the near future around those.

  • @konstantinkirilov9671
    @konstantinkirilov9671 2 ปีที่แล้ว

    Does someone else has the warnings with the code on the static int my_incrememt,num_lines, center_x,center_y and another warning on Graphics g = null; please help if someone has a solution

  • @Ropad10
    @Ropad10 9 ปีที่แล้ว

    Hi, I'd like to make a game, I'm trying to learn Java, but I find C# alot easier to learn as my first language.
    Is it possible to create sprites through paint or other drawing programs and use them in c#?

    • @alecr474
      @alecr474 9 ปีที่แล้ว +1

      Kevin Hansen yes, usually it is easier to use a different library/engine then basic .NET (the C# and visual basic libraries and format), using XNA, mono, or unity plugins for visual studio, otherwise you would use a WPF form instead of a windows form. WPF is made for 2D and 3D graphic handling. I am sure you can use win-forms, but i am just listing the easiest methods.

  • @chriswinchester4805
    @chriswinchester4805 10 ปีที่แล้ว

    I wonder if an engineer (financial or mechanical) could create a calculator, so that the calculator outputs would then become the inputs for lineDraw.

    • @alecr474
      @alecr474 9 ปีที่แล้ว +1

      Chris Winchester that is very possible. what i would do is create three double variables, one to hold the operator used by the calculator user, and the other two to be the numbers they put in. then, after the computer does operations, i would pass those variables to a draw method to get lines.

  • @70ME3E
    @70ME3E 10 ปีที่แล้ว

    love it, thank you! :)

  • @bj0urne
    @bj0urne 6 ปีที่แล้ว +1

    When i'm at the point of drawing at the end, after all the code is good, the lines does all wierd kinds of shit, they are not that square that you got, and i double checked our code.

  • @zacheryhenry6580
    @zacheryhenry6580 8 ปีที่แล้ว

    I'm stuck I've been trying to get this to work for so long. When I start my program 1 horizontal line is there like in the video but when I hit go the line just disappears.

    • @chrismerritt7291
      @chrismerritt7291  8 ปีที่แล้ว

      Can you paste your code in pastebin and share the link?

    • @zacheryhenry6580
      @zacheryhenry6580 8 ปีที่แล้ว

      I'm not familiar with pastebin. I posted my code in the comment above though

    • @zacheryhenry6580
      @zacheryhenry6580 8 ปีที่แล้ว

      Thanks for any help if you can though. I love this idea and have been trying for so long to get it to work I just can't figure out where i made an error. I feel it has to be with the button clicking

    • @SuatDemir
      @SuatDemir 8 ปีที่แล้ว

      add drawline(); to button1_Click like that.
      private void button1_Click(object sender, EventArgs e)
      {
      my_length = Int32.Parse(length.Text);
      my_angle = Int32.Parse(angle.Text);
      my_amount = Int32.Parse(increment.Text);
      start_x = canvas.Width / 2;
      start_y = canvas.Height / 2;
      canvas.Refresh();
      drawline();
      }

  • @damonmangrum5805
    @damonmangrum5805 10 ปีที่แล้ว

    im having trouble with the new
    new Point(start_x,start_y);
    new Point(start_x+100,start_y+100);
    it keeps asking me to add { } to it

  • @lewisoliver5148
    @lewisoliver5148 8 ปีที่แล้ว

    Thanks. Helped me a lot ^^

  • @VStyleMentVS
    @VStyleMentVS 9 ปีที่แล้ว

    Is this code means
    end_x=(int) (start_x + Math.Cos(my_angle * ...)* my_length)
    thise
    end_x= start_x + Math.Cos(my_angle * ...)* my_length
    ??

    • @tomwilson4572
      @tomwilson4572 9 ปีที่แล้ว +1

      +Василь Кохан my_angle needs to be in radians (without the hideous long decimal fraction) and is best declared as double rather than int for the trigonometry calculation. The result then needs converting to an int for end_x.
      // convert degrees to radians
      double cosAngle = Math.Cos(_angle * Math.PI / 180.0);
      double sinAngle = Math.Sin(_angle * Math.PI / 180.0);
      _endx = (int)(_startx + _length * cosAngle);
      _endy = (int)(_starty + _length * sinAngle);
      BTW I prefer a naming convention that distinguishes local variables from class member variables (these have an underscore prefix).

    • @langtonmwanza6689
      @langtonmwanza6689 8 ปีที่แล้ว

      also instead of Point you can used PointF.....it acepts decimal coordinates and handles them much better than just simple rounding off

  • @igorflamenguista1
    @igorflamenguista1 8 ปีที่แล้ว

    Guys, i've got a problem... when I write about the class canvas, this problem show up:
    The name 'canvas' does not exist in current context.
    I've searched out and I was supposed to put using System.Windows.Controls but visual studio didnt recognize that namespace. btw im using vs2012 windows 8.

    • @TheJazz747
      @TheJazz747 8 ปีที่แล้ว

      +igorflamenguista1 he renamed the panel in which he was drawing the stuff "canvas". to you it should be like "panelX" where X is your panel number.

  • @MessingWithCode
    @MessingWithCode 3 ปีที่แล้ว

    i love it

  • @zacheryhenry6580
    @zacheryhenry6580 8 ปีที่แล้ว

    So I got it to work but it's probably not the correct way. I pasted this inside of the button_click portion after canvas refresh:
    myPen.Width = 1;
    my_length = Int32.Parse(length.Text);
    g = canvas.CreateGraphics();
    for (int i = 0; i < Int32.Parse(number_of_lines.Text); i++)
    drawline();
    It's basically the canvas portion that wasn't getting called when i clicked go, only the canvas refresh part was. Copying this down in the button_click section after canvas refresh caused it to work idk what the downsides of this would be

  • @dylanhynds5877
    @dylanhynds5877 7 ปีที่แล้ว

    Does anyone have the end source code? I'm having trouble with the variables "Colour" and "Black" (Just so everybody knows iv'e only just started this and i haven't made it to the end)

  • @JohnnyPhoenix
    @JohnnyPhoenix 10 ปีที่แล้ว

    How can I donate (trollish nickname apart)?

    • @chrismerritt7291
      @chrismerritt7291  10 ปีที่แล้ว

      Haha. I like your handle. There's a donate button on my channel home page @ the top right. th-cam.com/channels/hKYo_FJBF1aMV7VVWXkO9A.html. Thanks!

  • @PaulKruskamp
    @PaulKruskamp 8 ปีที่แล้ว

    cool, subbed

  • @tomwilson4572
    @tomwilson4572 9 ปีที่แล้ว

    Use the following code snippet to convert from degrees to radians
    // convert degrees to radians
    double angleRadians = angleDegrees * Math.PI / 180.0;

    • @FelixPow1
      @FelixPow1 8 ปีที่แล้ว

      +Tom Wilson Thank you very much sir! Your way to convert from degrees to radians makes the drawing perfect

  • @krimbelkacem5751
    @krimbelkacem5751 10 ปีที่แล้ว

    how to print on the paper your work using c#

  • @damonmangrum5805
    @damonmangrum5805 10 ปีที่แล้ว

    nevermind i got it there was no semi colon thanks

  • @JensHallgren
    @JensHallgren 7 ปีที่แล้ว +1

    OMG, this is really scary... no offence, but... you are supposed to have some skills in coding (I suppose?), and still messing things up in this tiny example so you don't really know what is happening (I've been watching until 31.20 by now) .... Isn't there any method to actually ..eh.. get it right without lots of trial and error and guessing? Imagine you are working on a huge project with many files and many thousand lines of code? What if you code everything in this manner, it would be soaked with bugs.... some that you might discover, some you will never discover until it's too late..... Thanks anyway for a good introduction to Visual Studio and to draw graphics. Just what I needed. (I'm into embedded systems, so I haven't done much "Windows" stuff yet.). It's really good teaching to explain everything , like how to set up the controls with labels, textboxes and a button.

    • @Greebstreebling
      @Greebstreebling 2 ปีที่แล้ว

      when you say it's really scary, was your life at risk? Perhaps a sense of perspective would be good - and cut the author some slack for helping the rest of us out big time. From an educational perspective, talking as you 'do' is a great way to help folks learn.

  • @ctbram0627
    @ctbram0627 10 ปีที่แล้ว +3

    You should maybe practice or write your example and test it before recording. By 37 minutes and 35 seconds I was totally confused because of the logic mistakes. Also a good 5 minutes of you kind of scratching your head does not make for exciting viewing.

  • @mathewo9209
    @mathewo9209 7 ปีที่แล้ว

    Dear god, why are you called CreateGraphics inside the paint ever. That's a bad idea, never do that, use the graphics class provided by the method, that's the whole paint of the paint event.

  • @ismaelgoldsteck5974
    @ismaelgoldsteck5974 8 ปีที่แล้ว

    dispose your graphics object!

  • @damonmangrum5805
    @damonmangrum5805 10 ปีที่แล้ว +1

    new Point(start_x,start_y);
    new Point(end_x,end_y);

  • @damonmangrum5805
    @damonmangrum5805 10 ปีที่แล้ว

    { } expected

  • @been1324
    @been1324 3 ปีที่แล้ว +3

    shite