I used
Pen pen = new Pen(Color.Red);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
to shape the border of rectangle but now I only need to show the corner of that rectangle.
You can draw it by yourself by DrawLine function in Paint event handler, something like this:
Pen pen = new Pen(Color.Red);
private void Form1_Load(object sender, System.EventArgs e)
{
pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
}
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawLine(pen, 0, 0, pictureBox1.Right, 0);
g.DrawLine(pen, 0, 0, 0, pictureBox1.Bottom);
}
It's a use case, maybe you need other coordinates, but you can fix it easily.
You could use 2 lines to get the effect you want:
private void MainForm_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Red);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
e.Graphics.DrawLine(pen, 0, 0, 50, 0 );
e.Graphics.DrawLine(pen, 0, 0, 0, 50);
}
This draws the corner of a rectangle in the top left corner of the form.
Related
I have a Label and I am trying to draw an inner circle (not filled) inside this Label, as big as possible.
I have tried two methods, one method applied to label1 and another one applied to label2. In both cases it does not work.
Note: the Label should keep its background color and content.
How can I get rid of this?
Code:
void DrawCircle1(Graphics g, Point centerPos, int radius, int cutOutLen)
{
RectangleF rectangle = new RectangleF(centerPos.X, centerPos.Y,
radius * 2,
radius * 2
);
// calculate the start angle
float startAngle = (float)(Math.Asin(
1f * (radius - cutOutLen) / radius) / Math.PI * 180);
using (System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath())
{
path.AddArc(rectangle, 180 - startAngle, 180 + 2 * startAngle);
path.CloseFigure();
//g.FillPath(Brushes.Yellow, path);
using (Pen p = new Pen(Brushes.Yellow))
{
g.DrawPath(new Pen(Brushes.Blue, 2), path);
}
}
}
private void DrawCircle2(PaintEventArgs e)
{
Label tempLabel = label2;
using (System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red))
{
using (System.Drawing.Pen myPen = new Pen(myBrush, 2))
{
e.Graphics.DrawEllipse(myPen, new System.Drawing.Rectangle(tempLabel.Location.X, tempLabel.Location.Y,
tempLabel.Width, tempLabel.Height));
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
DrawCircle1(e.Graphics, new Point(label1.Width/2, label1.Height/2), 10, 50);
DrawCircle2(e);
}
Below a screenshot:
You are drawing on Form not the Label. Instead of overriding OnPaint method of the Form, try to handle Paint event of Label controls. For example:
private void label1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.DrawEllipse(Pens.Red, 0, 0, label1.Height - 1, label1.Height - 1);
}
Same procedures you're using now, calling them from a control's Paint() event.
It's the same if you're creating a Custom control. Use the overridden OnPaint() event in this case.
In the control's Paint() event, call one/more methods to draw a shape on the control's surface.
private void label1_Paint(object sender, PaintEventArgs e)
{
DrawCircle1(e.Graphics, label1.ClientRectangle);
}
private void label2_Paint(object sender, PaintEventArgs e)
{
DrawCircle2(e.Graphics, label2.ClientRectangle);
}
Use the Control's ClientRectangle bounds to derive the size of the figure.
Here, the ClientRectangle is reduced by 1 when using Graphics.DrawEllipse() and by 2 when using Graphics.DrawPath(). The two methods calculate the pen size in relation to the drawing area in a slightly different manner.
private void DrawCircle1(Graphics g, RectangleF canvas)
{
canvas.Inflate(-2, -2);
g.SmoothingMode = SmoothingMode.AntiAlias;
using (GraphicsPath path = new GraphicsPath())
using (Pen p = new Pen(Color.Blue, 2)) {
path.StartFigure();
path.AddArc(canvas, 0, 360);
path.CloseFigure();
g.DrawPath(p, path);
}
}
private void DrawCircle2(Graphics g, RectangleF canvas)
{
canvas.Inflate(-1, -1);
g.SmoothingMode = SmoothingMode.AntiAlias;
using (Pen p = new Pen(Color.Red, 2)) {
g.DrawEllipse(p, canvas);
}
}
I have pictureBox click event. I get coordinates of click and try t draw circle:
private void pictureMain_Click(object sender, EventArgs e){
MouseEventArgs me = (MouseEventArgs)e;
Point coordinates = me.Location;
int x = coordinates.X;
int y = coordinates.Y;
// Create pen.
Pen blackPen = new Pen(Color.Red, 2);
// Create rectangle for ellipse.
Rectangle rect = new Rectangle(x, y, 50, 50);
g.DrawEllipse(blackPen, rect);
}
But it draws circle not in coordinates(x,y) of picturebox. It places circle in another place.
Try this:
private void pictureBox1_Click (object sender, EventArgs e)
{
Point ellipseCenter = ((MouseEventArgs) e).Location;
Size ellipseSize = new Size (50, 50);
Point rectPosition = new Point (ellipseCenter.X - ellipseSize.Width / 2, ellipseCenter.Y - ellipseSize.Height / 2);
Rectangle rect = new Rectangle (rectPosition, ellipseSize);
using (Graphics grp = Graphics.FromImage (pictureBox1.Image))
{
grp.DrawEllipse (Pens.Red, rect);
}
pictureBox1.Refresh ();
}
Hi I want to make my picture box to be a circle-shaped
Then print it.
The problem is I can see in the form that the picture box is circle but when I'm previewing it to print it's not circle.
Here's my code
public Form1()
{
InitializeComponent();
//This makes picturebox1 circle
System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
gp.AddEllipse(0, 0, pictureBox1.Width - 4, pictureBox1.Height - 4);
Region rg = new Region(gp);
pictureBox1.Region = rg;
}
//Preview the print
private void button1_Click(object sender, EventArgs e)
{
printPrev.Document = printDoc;
printPrev.ShowDialog();
}
private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//Draw the picturebox on PDF
e.Graphics.DrawImage(pictureBox1.Image, 230, 230);
}
Thanks
it is not working because you are not changing the Image.
You are only changing the graphics.
You could do something like this.
Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(bitmap);
g.DrawEllipse(new Pen(new SolidBrush(Color.Black),3),0,0,bitmap.Width -4,bitmap.Height - 4);
pictureBox1.Image = bitmap;
This will also solve your problem
using (var bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height)) {
pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width,bmp.Height));
e.Graphics.DrawImage(bmp, 230, 230);
}
Basically I have a form with no border, I want to keep it like that. It's in a fixed position. I'm trying to draw an outline of the form size in the form (so it looks like a border) I'm having trouble and never really used "drawing" techniques in forms.
Pen pen = new Pen(Color.Black, 20);
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
Graphics g = CreateGraphics();
g.DrawRectangle(pen, rect);
Do it in OnPaint:
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
Pen pen = new Pen(Color.Black, 20);
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
e.Graphics.DrawRectangle(pen, rect);
}
I have the following paint event (form) that is drawing the rectangle:
void LogicSimulationViewerForm_Paint(object sender, PaintEventArgs e) {
Rectangle rect = new Rectangle(100, 100, 400, 100);
Graphics c = rtbLogicCode.CreateGraphics();
c.DrawRectangle(new Pen(Color.Black, 3), rect);
}
The rectangle is shown for a brief moment and then disappears immediately. The rectangle will only show momentarily again when the user resizes the form.
How can I solve this issue?
Don't use the Control.CreateGraphics() method, use the PaintEventArgs.Graphics property:
void LogicSimulationViewerForm_Paint(object sender, PaintEventArgs e) {
Rectangle rect = new Rectangle(100, 100, 400, 100);
e.Graphics.DrawRectangle(Pens.Black, rect);
}