drawing circle in the middle of the desktop - c#

heyho folks,
could you please help me to draw a circle in the middle of the desktop.
i am very frustrated about the desktop coordinates.
this is for an overlay
here is my code:
Graphics g = default(Graphics);
g = pictureBox1.CreateGraphics();
//g.DrawEllipse(Pens.Red, 10, 10, 200, 200);
int screenmiddlx = this.Width / 2;
int screenmiddly = this.Height / 2;
int kscreenmoddly = Height;
int kscreenmoddlx = Width;
g.DrawLine(Pens.Red, screenmiddlx - 10, screenmiddly, screenmiddlx + 10, screenmiddly);
g.DrawLine(Pens.Red, screenmiddlx, screenmiddly - 10, screenmiddlx, screenmiddly + 10);
g.DrawEllipse(Pens.Red,,,,);

this is my code:
Graphics kp = e.Graphics;
kp.DrawEllipse(Pen, x, y, width, height);
// exemple for you:
kp.DrawEllipse(Pens.Red, screenmiddlx, screenmiddly, 20, 20);

Related

Increasing the height of Panel according to wrapped string Label in Panel

i am trying to develop a win form for chatting purpose. I am developing Chat bubbles using pure inbuilt functions of .Net Framework, No fancy UI, No third party libraries.
Now let's have a look on how every thing is being done.
My following function is responsible for generating a Panel dynamically for each chat message received, the Pain event is used to draw rounded rectangle and color is transparent. A picture box is used to show static avatar.
private void SetRemoteMessage(string msg)
{
PictureBox pb = new PictureBox();
pb.Bounds = new Rectangle(0, 0, 72, 72);
pb.Image = Base64ToImage(avatar_his);
Panel p = new Panel();
Label lb = new Label();
lb.BackColor = Color.Transparent;
lb.ForeColor = Color.Blue;
lb.Text = msg;
lb.Font = new Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Point);
p.Bounds = new Rectangle(rX, rY, (Width / 2) - 25, pb.Height);
p.BackColor = Color.Transparent;
lb.Size = new Size(p.Width - pb.Width, p.Height);
lb.Paint += _control_Paint;
lb.Location = new Point(pb.Width + 5, 0);
p.Controls.Add(pb);
p.Controls.Add(lb);
SetPanel(p);
rY += p.Height + 20;
mY += p.Height + 20;
}
following is the Paint event binded to the parent "Container" Panel so that a simple rounded rectangle is shown for each bubble
private void _control_Paint(object sender, PaintEventArgs e)
{
Control c = (Control)sender;
if(!c.Name.Equals("lb"))
{
Graphics v = e.Graphics;
DrawRoundRect(v, Pens.Blue, e.ClipRectangle.Left, e.ClipRectangle.Top, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1, 10);
}
else
{
using (Font font1 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
{
e.Graphics.DrawString(c.Text, font1, Brushes.Blue, c.Bounds);
}
}
base.OnPaint(e);
}
and the following function to actually generate rounded rectangles
private void DrawRoundRect(Graphics g, Pen p, float X, float Y, float width, float height, float radius)
{
GraphicsPath gp = new GraphicsPath();
gp.AddLine(X + radius, Y, X + width - (radius * 2), Y);
gp.AddArc(X + width - (radius * 2), Y, radius * 2, radius * 2, 270, 90);
gp.AddLine(X + width, Y + radius, X + width, Y + height - (radius * 2));
gp.AddArc(X + width - (radius * 2), Y + height - (radius * 2), radius * 2, radius * 2, 0, 90);
gp.AddLine(X + width - (radius * 2), Y + height, X + radius, Y + height);
gp.AddArc(X, Y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
gp.AddLine(X, Y + height - (radius * 2), X, Y + radius);
gp.AddArc(X, Y, radius * 2, radius * 2, 180, 90);
gp.CloseFigure();
g.DrawPath(p, gp);
}
The chat message is not wrapped automatically so to wrap it, the Pain method is used as it can be seen in Paint event . .the output is good as expected instead of one thing
The Problem can be seen easily the Panel height is not increasing according when a large message is given to display in bubble.
What i tried already is
Measuring string length with Graphics class, but i was not able to implement it with success
Enabling Scrollbars, Yes this approach worked but i am not interested to use this behavior
Counting string length and increasing height, This works but not efficient, specially when form is resized all calculations of measurement then become invalid.

Getting two ellipses instead of one

So I have a code that needs to draw a circle, on random position, and also it needs to stay in predefined borders. In my code, I'm supposed to draw only one circle. In some cases, it draws only one but in other cases, it draws two of them and I have no idea why.
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Random r = new Random();
int leftX = 20;
int topY = 60;
int width = this.Width - (3 * leftX);
int height = this.Height - (int)(2.5 * topY);
float x = r.Next(20, width - 100);
float y = r.Next(60, height - 100);
Rectangle rect = new Rectangle((int)x, (int)y, 100, 100);
g.DrawRectangle(new Pen(Color.Black), rect); //rectangle around ellipse
g.DrawRectangle(new Pen(Color.Black, 3), leftX, topY, width, height); //border rectangle
g.FillEllipse(new SolidBrush(Color.Black), rect);
}
The paint event gets called whenever the form is re-drawn, so from your point of view, that's unpredictable. You would be better moving the code to generate the position into the load event:
float x;
float y;
private void Form1_Load(object sender, System.EventArgs e)
{
Random r = new Random();
x = r.Next(20, width - 100);
y = r.Next(60, height - 100);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int leftX = 20;
int topY = 60;
int width = this.Width - (3 * leftX);
int height = this.Height - (int)(2.5 * topY);
Rectangle rect = new Rectangle((int)x, (int)y, 100, 100);
g.DrawRectangle(new Pen(Color.Black), rect); //rectangle around ellipse
g.DrawRectangle(new Pen(Color.Black, 3), leftX, topY, width, height); //border rectangle
g.FillEllipse(new SolidBrush(Color.Black), rect);
}

How to fill curved area with color in C# Winform?

I want to draw curved area with color(black in my case) in a rectangle.
I tried multiple things FillPie, FillEllipse in OnPaint event but not able to do, I want to draw like thisFill Curved Area
I have tried the below code. but this is not what I want
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics gr = e.Graphics;
int x = 50;
int y = 50;
int width = 100;
int height = 100;
Rectangle rect = new Rectangle(x, y, width / 2, height / 2);
gr.FillRectangle(Brushes.Black, rect);
gr.FillPie(Brushes.White, x, y, width, height, 180, 90);
using (Pen pen = new Pen(Color.Yellow, 1))
gr.DrawArc(pen, x, y, width, height, 180, 90);
}
this code is drawing like this. I dont want to create extra rectangle. MyCode
I'm not 100% sure if this is that what you wanted:
I achieved this by creating a Region with a quarter of the specified rectangle. I then exclude the pie from it using a GraphicsPath. The resulting curve is then filled using Graphics.FillRegion with a black brush:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics gr = e.Graphics;
int x = 50;
int y = 50;
int width = 100;
int height = 100;
Rectangle rect = new Rectangle(x, y, width/ 2, height / 2);
Region r = new Region(rect);
GraphicsPath path = new GraphicsPath();
path.AddPie(x, y, width, height, 180, 90);
r.Exclude(path);
gr.FillRegion(Brushes.Black,r);
}

Lost trying to draw a line of x length y degrees

I think its my maths that's letting me down!
I'm trying to draw a line of x length at Y degrees on a picturebox control with a North South East west, lines.
the below works but draws the line line at 250 degrees not 290!?
Bitmap bmp = new Bitmap(400, 400);
Graphics g = Graphics.FromImage(bmp);
// smooth graphics
g.SmoothingMode = SmoothingMode.AntiAlias;
g.DrawLine(new Pen(Color.Black, 2), 205, 20, 205, 385);
g.DrawLine(new Pen(Color.Red, 2), 20, 205, 390, 205);
// let's draw a coordinate equivalent to (20,30) (20 up, 30 across)
g.DrawString("N", new Font("Calibri", 12 , FontStyle.Bold), new SolidBrush(Color.Black), 197, 0);
g.DrawString("S", new Font("Calibri", 12, FontStyle.Bold), new SolidBrush(Color.Black), 197, 385);
g.DrawString("W", new Font("Calibri", 12, FontStyle.Bold), new SolidBrush(Color.Black), 0, 195);
g.DrawString("E", new Font("Calibri", 12, FontStyle.Bold), new SolidBrush(Color.Black), 390, 195);
//Draw Wind line
int x = 205, y = 205;
int wSpeed = 30*3; //length of line
int angle = 290; //angle to draw line at.
int startX = x;
int startY = y;
int endX = Convert.ToInt32(Math.Round(x + wSpeed * Math.Sin(Calcs.Radians(angle))));
int endY = Convert.ToInt32(Math.Round(y + wSpeed * Math.Cos(Calcs.Radians(angle))));
g.DrawLine(new Pen(Color.Blue, 2), startX, startY, endX, endY);
PictureBox display = pictureBox1;
this.Controls.Add(display);
display.Image = bmp;
public static double Radians(double dDegrees)
{
double result = Math.PI * dDegrees / 180.0;
return result;
}
If you look at the results: its -20 degrees from 270, instead of +20 expected, meaning you v got correct Y but wrong X. I suggest check(change) the sign of X coord. Graphics is very often trial-error unless you are expert.
int endX = Convert.ToInt32(Math.Round(x - wSpeed * Math.Sin(Calcs.Radians(angle))));

Windows form not display in correct format when preview

I want to print data contained in part of a area in a windows form in C#. This is my DrawAll() method which called when click on preview button.I want to print only the data in that part without background image. But It not give the correct format,
this is the correct format
This is what i got
this is the DrawAll method
` private void DrawAll(Graphics g)
{
// RectangleF srcRect = new RectangleF(0, 0, this.pictureBox1.Width, this.pictureBox1.Height);
Rectangle srcRect=new Rectangle(0,0,this.pictureBox1.Width,this.pictureBox1.Height);
int nWidth = printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.Width;
int nHeight = printDocument1.PrinterSettings.DefaultPageSettings.PaperSize.Height;
Rectangle destRect = new Rectangle(0, 0, nWidth, nHeight /3);
// Rectangle drawRect = (Rectangle)destRect.;
// g.DrawImage(this.pictureBox1.BackgroundImage, destRect, srcRect, GraphicsUnit.Pixel);
Pen aPen = new Pen(Brushes.Black, 1);
g.DrawRectangle(aPen,destRect);
float scalex = destRect.Width / srcRect.Width;
float scaley = destRect.Height / srcRect.Height;
// Pen aPen = new Pen(Brushes.Black, 1);
for (int i = 0; i < this.Controls.Count; i++)
{
if (Controls[i].GetType() == this.paytext.GetType())
{
TextBox theText = (TextBox)Controls[i];
g.DrawString(theText.Text, theText.Font, Brushes.Black, theText.Bounds.Left * scalex, theText.Bounds.Top * scaley, new StringFormat());
}
if (Controls[i].GetType() == this.label4.GetType())
{
Label theTextlbl = (Label)Controls[i];
g.DrawString(theTextlbl.Text, theTextlbl.Font, Brushes.Black, theTextlbl.Bounds.Left * scalex, theTextlbl.Bounds.Top * scaley, new StringFormat());
}
if (Controls[i].GetType() == this.dateTimePicker1.GetType())
{
DateTimePicker theDate = (DateTimePicker)Controls[i];
g.DrawString(theDate.Text, theDate.Font, Brushes.Black, theDate.Bounds.Left * scalex, theDate.Bounds.Top * scaley, new StringFormat());
}
}
}`

Categories