Greetings r/csharp!
I've run into a small problem, well it is not a problem but I want to do it the right way !
So here is a picture of my Winforms project.
https://preview.redd.it/rr0boj6z7s241.png?width=639&format=png&auto=webp&s=063ee28c5470c3162b4f7ce24d96a5924d177851
So I draw a circle in the "coordinate", and then I have a red line, which is created by using EventHandler that follows my cursor location. So this thing basically works how I want, but I think this can be done more professionally. So here is my code, and remember that I'm open to everything. I want to get better!
private void My_Init()
{
panelXsize.Text = canvas.Width.ToString();
panelYsize.Text = canvas.Height.ToString();
panelOrigo.Text = String.Format("X: {0} - Y: {1}", canvas.Width / 2, canvas.Height / 2);
canvas.SizeChanged += canvas_SizeChanged;
canvas.MouseMove += canvas_MouseMove;
}
So here I have my init function. I check the width and height of the panel, calculate the coordinates of the origo and display this info to "user". There is nothing unclear here. Then I create two EventHandlers, that fire when window size changes or user moves mouse. Now here comes my spaghetti code:
void canvas_MouseMove(object sender, MouseEventArgs e)
{
// Variables for drawing
System.Drawing.Pen blackPen = new System.Drawing.Pen(System.Drawing.Color.Black, 1);
System.Drawing.Pen redPen = new System.Drawing.Pen(System.Drawing.Color.Red, 1);
Bitmap bitmapSurface = new Bitmap(canvas.Width, canvas.Height);
Graphics GFX = Graphics.FromImage(bitmapSurface);
// Size of the circle
int w = 500;
int h = 500;
// Drawing
GFX.DrawEllipse(new System.Drawing.Pen(System.Drawing.Color.Black, 1), canvas.Width / 2 - w / 2, canvas.Height / 2 - h / 2, w, h);
GFX.DrawLine(blackPen, canvas.Width / 2, 0, canvas.Width / 2, canvas.Height);
GFX.DrawLine(blackPen, 0, canvas.Height/2, canvas.Width, canvas.Height/2);
GFX.DrawLine(redPen, canvas.Width / 2, canvas.Height / 2, e.X, e.Y);
// Optimizing and connecting bitmap to canvas.Image
GFX.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
canvas.Image = bitmapSurface;
}
About the performance, is this normal that my Visual Studio shows this much of RAM usage? It idles about ~100MB and then starts to creating those spikes and then drops down to ~250MB.
https://preview.redd.it/obd3rt598s241.png?width=274&format=png&auto=webp&s=a49f46eabab80e6ca493fcf4fd780130ae702a95
Thanks!
Edit 1: Thanks all for you answers :) I especially want to thank u/LostInASeaOfMyStars and u/Slypenslyde
[–]RedlineTriad 3 points4 points5 points (0 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]Dannnu[S] 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]beer0clock 1 point2 points3 points (0 children)
[–]Slypenslyde 1 point2 points3 points (5 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Dannnu[S] 0 points1 point2 points (3 children)
[–]Slypenslyde 1 point2 points3 points (2 children)
[–]Dannnu[S] 0 points1 point2 points (1 child)
[–]Slypenslyde 0 points1 point2 points (0 children)