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 ;)
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.
@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.
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.
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)
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 ?
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 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); ... }
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?
+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.
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
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
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
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
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
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.
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).
"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"
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 can you see that problem? my problem: System.NullReferenceException: 'Object reference not set to an instance of an object.' g was null
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
+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.
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
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#?
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.
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.
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.
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.
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
+Василь Кохан 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).
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.
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
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)
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.
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.
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.
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.
Just what I needed. Just complicated enough to get me started but short enough to get going quickly. Thanks much!!
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 ;)
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.
Wow, it works under SharpDevelop! Thank you, your video was really helpful, your explanation was clear and detailed.
@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.
oh boi the abstract art this program makes is impressive
Exactly
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.
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)
Vert nice and interesting! Learned a lot! Thank you👍😀
Very helpful tutorial, subbed!
Thanks for this. You explain very well.
so many thanks, this is great
one question to you, if you want to go a step back, what is the code ??
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 ?
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.
By 20:32 You've declared Graphics g,.. and done g.drawLines.... How does it know what panel or form to draw it on?
+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);
...
}
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?
+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.
Great tutorial. Only one question: where, in the code, we use my_increment variable? Or it is not used? Thankyou!
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
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
Good video
It's help me a lot, thanks 4 tutorial
Thank you So much chris merritt, This will be highly useful for my research.
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
Good tutorial ...I see that you do not use the increment parameter
what are the dimensions (initial) of your form (Form1)?
Is there a way to save the image?
A neat idea for demonstrating Graphics in a WinForm though the implementation could be a little tidier.
i know right like having the draw function just take the graphics surface as an argument
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
Hey,is it possible fit drawing inside the panel,when i am giving high values to line it goes outside of panel...help me
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
Thanks, very good video
I need Help please i how i could draw a schape in runtime
Hello,
I want to help me,
Can i put the point = (12.5 , 14.6),
Thank you.
System.NullReferenceException: 'Object reference not set to an instance of an object.'
g was null.
Help me?????
Does this use the CPU or GPU to draw ?
+B100dyPhr34k CPU
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.
AutoCAD or MicroStation would be better choice if you want to manipulate these entities for future.
Amaaaaaazing!!
Thanks, excellent video
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 ?
+Dayan Dragomirov Hmmm. You could put the main thread to sleep after every line is drawn. google C# Thread.Sleep
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.
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).
Hope you get me >]
It was cool, dude!
Every time I try to run the program, I get a 'build errors' message. What has gone wrong?
Hello
Please help I want CaseTools in C# to do something like the shadow in photoshop
anything else please help me
do you what can I use instead of canvas, in my visual studio version it doesn't include canvas
"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"
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?
I found that I left out some important code under the drawLine function. All is good now.
@@chriswinchester4805 can you see that problem? my problem: System.NullReferenceException: 'Object reference not set to an instance of an object.'
g was null
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
Can any one tell me how can I simulate data structures in c# like stacks,queues and link lists
+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.
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
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#?
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.
I wonder if an engineer (financial or mechanical) could create a calculator, so that the calculator outputs would then become the inputs for lineDraw.
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.
love it, thank you! :)
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.
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.
Can you paste your code in pastebin and share the link?
I'm not familiar with pastebin. I posted my code in the comment above though
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
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();
}
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
Thanks. Helped me a lot ^^
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
??
+Василь Кохан 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).
also instead of Point you can used PointF.....it acepts decimal coordinates and handles them much better than just simple rounding off
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.
+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.
i love it
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
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)
its ok i fixed my error
How can I donate (trollish nickname apart)?
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!
cool, subbed
Use the following code snippet to convert from degrees to radians
// convert degrees to radians
double angleRadians = angleDegrees * Math.PI / 180.0;
+Tom Wilson Thank you very much sir! Your way to convert from degrees to radians makes the drawing perfect
how to print on the paper your work using c#
nevermind i got it there was no semi colon thanks
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.
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.
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.
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.
dispose your graphics object!
new Point(start_x,start_y);
new Point(end_x,end_y);
{ } expected
shite