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

  Рет қаралды 198,103

Chris Merritt

Chris Merritt

Күн бұрын

Пікірлер: 111
@info_1941
@info_1941 6 жыл бұрын
Just what I needed. Just complicated enough to get me started but short enough to get going quickly. Thanks much!!
@martinomasreiter3124
@martinomasreiter3124 9 жыл бұрын
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 8 жыл бұрын
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 жыл бұрын
Wow, it works under SharpDevelop! Thank you, your video was really helpful, your explanation was clear and detailed.
@Cruz0e
@Cruz0e 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 5 жыл бұрын
oh boi the abstract art this program makes is impressive
@anadibrothers-yourwaytorel5259
@anadibrothers-yourwaytorel5259 3 жыл бұрын
Exactly
@NetMistro
@NetMistro 3 жыл бұрын
Thanks for this. You explain very well.
@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.
@omerodabasi
@omerodabasi 7 жыл бұрын
Very helpful tutorial, subbed!
@bboyrd01
@bboyrd01 9 жыл бұрын
Thank you So much chris merritt, This will be highly useful for my research.
@james970027
@james970027 2 жыл бұрын
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
@Ivan67968
@Ivan67968 4 жыл бұрын
Vert nice and interesting! Learned a lot! Thank you👍😀
@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.
@tuananhvu6448
@tuananhvu6448 3 жыл бұрын
It's help me a lot, thanks 4 tutorial
@hz6612
@hz6612 4 жыл бұрын
Amaaaaaazing!!
@trunghoale8584
@trunghoale8584 9 ай бұрын
System.NullReferenceException: 'Object reference not set to an instance of an object.' g was null. Help me?????
@tomwilson4572
@tomwilson4572 8 жыл бұрын
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
@shohanvlogs1987
@shohanvlogs1987 2 жыл бұрын
Good video
@Greebstreebling
@Greebstreebling 2 жыл бұрын
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.
@harishmehta2992
@harishmehta2992 3 жыл бұрын
Thanks, very good video
@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 ??
@Obishman
@Obishman 5 жыл бұрын
Thanks, excellent video
@MessingWithCode
@MessingWithCode 2 жыл бұрын
i love it
@nasredeenyousif5911
@nasredeenyousif5911 4 жыл бұрын
Good tutorial ...I see that you do not use the increment parameter what are the dimensions (initial) of your form (Form1)?
@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
@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
@lewisoliver5148
@lewisoliver5148 7 жыл бұрын
Thanks. Helped me a lot ^^
@titusdrake9037
@titusdrake9037 4 жыл бұрын
Great tutorial. Only one question: where, in the code, we use my_increment variable? Or it is not used? Thankyou!
@انامسلموكفى
@انامسلموكفى 3 жыл бұрын
I need Help please i how i could draw a schape in runtime
@70ME3E
@70ME3E 9 жыл бұрын
love it, thank you! :)
@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
@konstantinkirilov9671
@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
@x_salaheddinechannel_x
@x_salaheddinechannel_x 4 жыл бұрын
Hello, I want to help me, Can i put the point = (12.5 , 14.6), Thank you.
@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 жыл бұрын
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
@mr.tuttifrutti9280
@mr.tuttifrutti9280 7 жыл бұрын
It was cool, dude!
@tomg2002
@tomg2002 4 жыл бұрын
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.
@chriswinchester4805
@chriswinchester4805 9 жыл бұрын
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 9 жыл бұрын
I found that I left out some important code under the drawLine function. All is good now.
@trunghoale8584
@trunghoale8584 9 ай бұрын
@@chriswinchester4805 can you see that problem? my problem: System.NullReferenceException: 'Object reference not set to an instance of an object.' g was null
@Bostonite1985
@Bostonite1985 10 жыл бұрын
Every time I try to run the program, I get a 'build errors' message. What has gone wrong?
@BdyPhrk
@BdyPhrk 8 жыл бұрын
Does this use the CPU or GPU to draw ?
@JayPaysLPs
@JayPaysLPs 8 жыл бұрын
+B100dyPhr34k CPU
@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
@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 жыл бұрын
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.
@bj0urne
@bj0urne 6 жыл бұрын
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.
@chriswinchester4805
@chriswinchester4805 9 жыл бұрын
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 жыл бұрын
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.
@butterdogestarwars6709
@butterdogestarwars6709 4 жыл бұрын
Is there a way to save the image?
@ahmadsaleh4628
@ahmadsaleh4628 8 жыл бұрын
Hello Please help I want CaseTools in C# to do something like the shadow in photoshop anything else please help me
@PaulKruskamp
@PaulKruskamp 8 жыл бұрын
cool, subbed
@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"
@boliussa
@boliussa 8 жыл бұрын
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 8 жыл бұрын
+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 8 жыл бұрын
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 8 жыл бұрын
+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.
@hasnatqureshi1188
@hasnatqureshi1188 8 жыл бұрын
Can any one tell me how can I simulate data structures in c# like stacks,queues and link lists
@chrismerritt7291
@chrismerritt7291 8 жыл бұрын
+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.
@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 жыл бұрын
+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 жыл бұрын
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 жыл бұрын
Hope you get me >]
@VStyleMentVS
@VStyleMentVS 8 жыл бұрын
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 8 жыл бұрын
+Василь Кохан 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.
@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(); }
@tomwilson4572
@tomwilson4572 8 жыл бұрын
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
@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)
@dylanhynds5877
@dylanhynds5877 7 жыл бұрын
its ok i fixed my error
@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
@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. kzbin.info/door/hKYo_FJBF1aMV7VVWXkO9A. Thanks!
@damonmangrum5805
@damonmangrum5805 10 жыл бұрын
nevermind i got it there was no semi colon thanks
@JensHallgren
@JensHallgren 7 жыл бұрын
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.
@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.
@damonmangrum5805
@damonmangrum5805 10 жыл бұрын
new Point(start_x,start_y); new Point(end_x,end_y);
@krimbelkacem5751
@krimbelkacem5751 10 жыл бұрын
how to print on the paper your work using c#
@ismaelgoldsteck5974
@ismaelgoldsteck5974 8 жыл бұрын
dispose your graphics object!
@damonmangrum5805
@damonmangrum5805 10 жыл бұрын
{ } expected
@ctbram0627
@ctbram0627 10 жыл бұрын
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.
@been1324
@been1324 3 жыл бұрын
shite
Visual Studio Winform Tic Tac Toe Tutorial Example (C#)
32:34
Chris Merritt
Рет қаралды 567 М.
C# Programming - Drawing Using The Graphics Class
19:56
Bill Butterfield
Рет қаралды 83 М.
SCHOOLBOY. Мама флексит 🫣👩🏻
00:41
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 7 МЛН
Fake watermelon by Secret Vlog
00:16
Secret Vlog
Рет қаралды 16 МЛН
Which One Is The Best - From Small To Giant #katebrush #shorts
00:17
Остановили аттракцион из-за дочки!
00:42
Victoria Portfolio
Рет қаралды 3,4 МЛН
How I Got Into Product Management (and How You Can Too)
12:17
C# objects 🧍‍♂️
5:01
Bro Code
Рет қаралды 47 М.
SVG can do that?! - talk by Sarah Drasner
38:08
freeCodeCamp Talks
Рет қаралды 12 М.
C# Drawing Application Tutorial: Part 1
18:52
Dokami
Рет қаралды 18 М.
C# GAME ENGINE BY 16-YEAR-OLD! // Code Review
49:06
The Cherno
Рет қаралды 307 М.
C# Draw a Point on a Circle - Planet Orbiting
6:40
Brandon Hilde
Рет қаралды 36 М.
Why all your classes should be sealed by default in C#
11:43
Nick Chapsas
Рет қаралды 92 М.
Что должен знать C# Junior разработчик?
9:33
Sergey Nemchinskiy
Рет қаралды 101 М.
How to draw in C# using the Graphics Class - CSharp Programming
22:07
Darren Does Everything
Рет қаралды 37 М.
SCHOOLBOY. Мама флексит 🫣👩🏻
00:41
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 7 МЛН