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

Sdílet
Vložit
  • čas přidán 27. 08. 2024

Komentáře • 111

  • @robertflegal1973
    @robertflegal1973 Před 6 lety

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

  • @martinomasreiter3124
    @martinomasreiter3124 Před 8 lety +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 Před 8 lety +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 Před 5 lety +1

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

  • @Cruz0e
    @Cruz0e Před 2 lety +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)

  • @lemonandgaming6013
    @lemonandgaming6013 Před 5 lety

    oh boi the abstract art this program makes is impressive

  • @NetMistro
    @NetMistro Před 2 lety

    Thanks for this. You explain very well.

  • @brianbrewster6532
    @brianbrewster6532 Před 6 lety

    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.

  • @shohanvlogs1987
    @shohanvlogs1987 Před 2 lety

    Good video

  • @omerodabasi
    @omerodabasi Před 7 lety +6

    Very helpful tutorial, subbed!

  • @tomwilson4572
    @tomwilson4572 Před 8 lety +1

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

    • @langtonmwanza6689
      @langtonmwanza6689 Před 8 lety

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

  • @Ivan67968
    @Ivan67968 Před 4 lety

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

  • @NimishP
    @NimishP Před 2 lety

    @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.

  • @bboyrd01
    @bboyrd01 Před 9 lety

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

  • @james970027
    @james970027 Před 2 lety +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

  • @tuananhvu6448
    @tuananhvu6448 Před 3 lety

    It's help me a lot, thanks 4 tutorial

  • @trunghoale8584
    @trunghoale8584 Před 8 měsíci

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

  • @harishmehta2992
    @harishmehta2992 Před 3 lety

    Thanks, very good video

  • @Greebstreebling
    @Greebstreebling Před 2 lety +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 Před 2 lety

      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.

  • @MessingWithCode
    @MessingWithCode Před 2 lety

    i love it

  • @konstantinkirilov9671

    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

  • @Obishman
    @Obishman Před 5 lety

    Thanks, excellent video

  • @zacheryhenry6580
    @zacheryhenry6580 Před 8 lety

    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

  • @user-xg7hi5mh3g
    @user-xg7hi5mh3g Před 3 lety

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

  • @gunteramberg1949
    @gunteramberg1949 Před 2 lety

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

  • @hz6612
    @hz6612 Před 4 lety

    Amaaaaaazing!!

  • @gopalakrishnanudhayasuriya1983

    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 Před 6 lety

      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

  • @chrisconn5649
    @chrisconn5649 Před 8 lety

    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

  • @KeenanWoodall
    @KeenanWoodall Před 10 lety

    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

  • @x_salaheddinechannel_x

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

  • @zacheryhenry6580
    @zacheryhenry6580 Před 8 lety

    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

  • @nasredeenyousif5911
    @nasredeenyousif5911 Před 4 lety

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

  • @Bostonite1985
    @Bostonite1985 Před 10 lety

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

  • @titusdrake9037
    @titusdrake9037 Před 4 lety

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

  • @damonmangrum5805
    @damonmangrum5805 Před 10 lety

    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

  • @butterdogestarwars6709

    Is there a way to save the image?

  • @lewisoliver5148
    @lewisoliver5148 Před 7 lety

    Thanks. Helped me a lot ^^

  • @70ME3E
    @70ME3E Před 9 lety

    love it, thank you! :)

  • @hasnatqureshi1188
    @hasnatqureshi1188 Před 8 lety +1

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

    • @chrismerritt7291
      @chrismerritt7291  Před 8 lety +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.

  • @igorflamenguista1
    @igorflamenguista1 Před 8 lety

    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 Před 8 lety

      +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.

  • @doro516
    @doro516 Před 5 lety

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

    • @ferna2294
      @ferna2294 Před 4 lety

      "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"

  • @tomg2002
    @tomg2002 Před 4 lety

    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 Před 2 lety

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

  • @BdyPhrk
    @BdyPhrk Před 8 lety +1

    Does this use the CPU or GPU to draw ?

  • @chriswinchester4805
    @chriswinchester4805 Před 9 lety

    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 Před 9 lety

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

    • @trunghoale8584
      @trunghoale8584 Před 8 měsíci

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

  • @mr.tuttifrutti9280
    @mr.tuttifrutti9280 Před 7 lety

    It was cool, dude!

  • @chriswinchester4805
    @chriswinchester4805 Před 9 lety

    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 Před 9 lety +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.

  • @ahmadsaleh4628
    @ahmadsaleh4628 Před 8 lety

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

  • @ddragomirov
    @ddragomirov Před 8 lety

    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  Před 8 lety +1

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

    • @ddragomirov
      @ddragomirov Před 8 lety

      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 Před 8 lety +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 Před 8 lety +1

      Hope you get me >]

  • @bj0urne
    @bj0urne Před 6 lety +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 Před 8 lety

    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  Před 8 lety

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

    • @zacheryhenry6580
      @zacheryhenry6580 Před 8 lety

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

    • @zacheryhenry6580
      @zacheryhenry6580 Před 8 lety

      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 Před 7 lety

      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();
      }

  • @Ropad10
    @Ropad10 Před 9 lety

    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 Před 9 lety +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.

  • @boliussa
    @boliussa Před 8 lety

    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 Před 8 lety

      +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 Před 8 lety

      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 Před 8 lety

      +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.

  • @VStyleMentVS
    @VStyleMentVS Před 8 lety

    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 Před 8 lety +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 Před 8 lety

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

  • @PaulKruskamp
    @PaulKruskamp Před 7 lety

    cool, subbed

  • @dylanhynds5877
    @dylanhynds5877 Před 7 lety

    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)

  • @tomwilson4572
    @tomwilson4572 Před 8 lety

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

    • @FelixPow1
      @FelixPow1 Před 8 lety

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

  • @zacheryhenry6580
    @zacheryhenry6580 Před 8 lety

    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

  • @JohnnyPhoenix
    @JohnnyPhoenix Před 10 lety

    How can I donate (trollish nickname apart)?

    • @chrismerritt7291
      @chrismerritt7291  Před 10 lety

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

  • @damonmangrum5805
    @damonmangrum5805 Před 10 lety

    nevermind i got it there was no semi colon thanks

  • @krimbelkacem5751
    @krimbelkacem5751 Před 10 lety

    how to print on the paper your work using c#

  • @damonmangrum5805
    @damonmangrum5805 Před 10 lety +1

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

  • @mathewo9209
    @mathewo9209 Před 7 lety

    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 Před 7 lety

    dispose your graphics object!

  • @ctbram0627
    @ctbram0627 Před 9 lety +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.

  • @JensHallgren
    @JensHallgren Před 7 lety +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 Před 2 lety

      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.

  • @damonmangrum5805
    @damonmangrum5805 Před 10 lety

    { } expected

  • @been1324
    @been1324 Před 3 lety +3

    shite