How can I draw a Rectangle on a PictureBox? - c#

I am trying to just draw a Rectangle on a PictureBox that is on a Form.
Writing text like shown here works fine.
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using (Font myFont = new Font("Arial", 14))
{
e.Graphics.DrawString("Hello .NET Guide!", myFont, Brushes.Green, new Point(2, 2));
}
}
but when I try to draw a rectangle like so nothing shows up.
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Rectangle rect = new Rectangle();
rect.Location = new Point(25, 25);
rect.Width = 50;
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, rect);
}
}
What am I missing?

Well, you might have forgot to set rect.Height to something other than 0.
Did you check if the rectangle you're drawing has the correct dimensions?

Perhaps try
Rectangle rect = new Rectangle(new Point(25, 25), new Size(50, 50));
if you like it shorter.

Related

C# Drawing on panel in TLP

private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics graphicObj;
graphicObj = this.CreateGraphics();
Pen myPen = new Pen(System.Drawing.Color.Black, 2);
graphicObj.DrawEllipse(myPen, 200, 200, 200, 200);
}
This work on form. But for auto resizing items such as buttons, label I have to use TablePanelLayout, and drawing on it only works with adding to construtor in form
panel1.Paint+=new PaintEventHandler(panel1_draw);
and then by drawing with this method
private void panel1_Paint(object sender, PaintEventArgs e)
{
var g = e.Graphics;
Pen myp = new Pen(System.Drawing.Color.Red, 4);
Font fy = new Font("Helvetica", 10, FontStyle.Bold);
Brush br = new SolidBrush(System.Drawing.Color.Red);
g.DrawString(textBox1.Text, fy, br, 0,0);
}
Question is Why/what topic should I master to know it.

How to draw object in PictureBox c#?

I trying draw a rectangles in PictureBox by mouse click:
private void MyPictureBoxMouseClick(object sender, MouseEventArgs e)
{
using (Graphics g = MyPictureBox.CreateGraphics())
{
var pen = new Pen(Color.Black, 2);
g.DrawRectangle(pen, e.X, e.Y, 50, 50);
pen.Dispose();
}
}
And rectangles are drawning. But when i move mouse beyond the PictureBox all rectangles are disappear. How to avoid it?
UPDATE
I added a Paint event:
private List<Rectangle> Rectangles { set; get; }
private void MyPictureBoxPaint(object sender, PaintEventArgs e)
{
using (Graphics g = MyPictureBox.CreateGraphics())
{
var pen = new Pen(Color.Black, 2);
foreach (var rect in Rectangles)
{
g.DrawRectangle(pen, rect);
}
pen.Dispose();
}
}
private void MyPictureBoxMouseClick(object sender, MouseEventArgs e)
{
Rectangles.Add(new Rectangle(e.X, e.Y, 50, 50));
MyPictureBox.Refresh();
}
But now rectangles not drawning.
Update
Oh it was my mistake.
g.DrawRectangle(pen, rect); -> e.Graphics.DrawRectangle(pen, rect);
Yes, you're drawing over the picture box. When the next paint messgae arrives, picturebox re-paints itself again at that time it'll overwrite your rectangles.
You either need to draw it in Paint event in order to make your rectangles survive or you can Draw over the PictureBox.Image so it will stay there.
For your edit: You need to use e.Graphics property. For instance following code works for me.
private void MyPictureBoxPaint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
using (var pen = new Pen(Color.Black, 2))
{
foreach (var rect in Rectangles)
{
g.DrawRectangle(pen, rect);
}
}
}

Drawing a point in an image in C#

I have an image with the dimensions of 100x50 and I want to draw a dot in the center - i.e. at 50x25. How would I do this?
you could use the setPixle() function.
Graphics g = pictureBox1.CreateGraphics();
g.DrawEllipse(Pens.Black, new Rectangle(50, 25, 1, 1));
look here for saving the picture
it does not draw on form load so you should add your code in form paintevent :
private void Form1_Paint(object sender, PaintEventArgs e)
{
Application.DoEvents();
Graphics g = pictureBox1.CreateGraphics();
g.DrawEllipse(Pens.DarkBlue, new Rectangle(120, 90, 1, 1));
}
Image img = pictureBox1.Image;
Graphics g = Graphics.FromImage(img);
g.DrawEllipse(Pens.DarkBlue, new Rectangle(50, 25, 1, 1));
g.DrawImage(img, new Point(0, 0));
img.Save("d:\\img.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
try this for other question :
Rectangle bounds = new Rectangle(10, 20, 50, 60);
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
Graphics g = Graphics.FromImage(bitmap);
g.CopyFromScreen(Point.Empty,Point.Empty, bounds.Size);
bitmap.Save("d:\\img.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
this code will capture screen with bound of bounds rectangle.

How to draw an ellipse C# via GDI?

I am new in C# and i am trying to draw a color filled ellipse, I foud some code but i couldn't figure out how to do it.
I tried with this code:
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Aquamarine, 2);
g.DrawEllipse(pen, 10, 10, 100, 20);
But the method does not exist.
Colud you help me?
Thanks in advance.
You should do your graphics drawing in the forms Paint Event, otherwise as soon as the screen updates you will loose your drawing. This is a quick and dirty example on how to do so.
public Form1()
{
InitializeComponent();
this.Paint += new PaintEventHandler(Form1_Paint);
}
void Form1_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Aquamarine,2);
SolidBrush brush = new SolidBrush(Color.Aquamarine);
e.Graphics.DrawEllipse(pen, 10, 10, 100, 20);
e.Graphics.FillEllipse(brush, 10, 50, 100, 20);
}

How to Save the drawing (contents) from a panel into any format on a physical storage with C#? (WinForms)

Can someone tell me what i don't make as it should, why i cannot save the drawing to a physical storage?
private void panel1_Paint(object sender, PaintEventArgs e)
{
Pen p = new Pen(Color.Red, 3);
Bitmap bmp = new Bitmap(700, 900);
Graphics gr = this.CreateGraphics();
gr.DrawLine(p, new Point(30, 30), new Point(80, 120));
gr.DrawEllipse(p, 30, 30, 80, 120);
//when i do this way it saves only a black rectangle, without other drawn content
bmp.Save(#"C:\testBMP.jpeg", ImageFormat.Jpeg);
// If i use the following 2 commented lines it saves only a empty rectangle.
//Rectangle rec = new Rectangle(0, 0, 700, 900);
// panel1.DrawToBitmap(bmp, rec);
}
Thank you for advice!
You have two problems here.
Drawing your panel's contents. This should be done inside its Paint event handler, like this:
private void panel1_Paint(object sender, PaintEventArgs e)
{
using (Pen p = new Pen(Color.Red, 3))
{
// get the panel's Graphics instance
Graphics gr = e.Graphics;
// draw to panel
gr.DrawLine(p, new Point(30, 30), new Point(80, 120));
gr.DrawEllipse(p, 30, 30, 80, 120);
}
}
Saving your panel's contents as an image. This part should be done somewhere else (for example, when you click on a "Save" button):
private void saveButton_Click(object sender, EventArgs e)
{
int width = panel1.Size.Width;
int height = panel1.Size.Height;
using (Bitmap bmp = new Bitmap(width, height))
{
panel1.DrawToBitmap(bmp, new Rectangle(0, 0, width, height));
bmp.Save(#"C:\testBMP.jpeg", ImageFormat.Jpeg);
}
}
The instance gr has nothing to do with your bitmap (bmp). So you're creating graphics that are associated with the form or control, and have a separate bitmap. When you save the bitmap, you haven't drawn anything in it.
You need to get a Graphics object from the Image, not from your form. I have not tested this, but it should work.
private void panel1_Paint(object sender, PaintEventArgs e)
{
using (Pen p = new Pen(Color.Red, 3))
using (Bitmap bmp = new Bitmap(700, 900))
using (Graphics gr = Graphics.FromImage(bmp))
{
gr.DrawLine(p, new Point(30, 30), new Point(80, 120));
gr.DrawEllipse(p, 30, 30, 80, 120);
bmp.Save(#"C:\testBMP.jpeg", ImageFormat.Jpeg);
}
}

Categories

Resources