How to draw object in PictureBox c#? - 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);
}
}
}

Related

How can I draw from another class?

I have MainForm class. Here I can make somthing like this.
private void MainForm_Paint(object sender, PaintEventArgs e)
{
Graphics graphics = this.CreateGraphics();
Rectangle rectangle = new Rectangle(50, 100, 150, 150);
graphics.DrawEllipse(Pens.Black, rectangle);
graphics.DrawRectangle(Pens.Red, rectangle);
}
And I can see result in my Form.
But I have another class Image. And I want to draw from here. How can I do it?
Send the PaintEventArgs (the below came from one i have been using)
class Draw
{
public void Paint(PaintEventArgs e)
{
e.Graphics.DrawRectangles(Pens.Blue, GetRectangle());
}
}
where GetRectangle would be another method to define the rectangle
you should also be able to just send your object (in your case the instance of MainForm)
class Draw
{
public void Paint(MainForm main)
{
Graphics graphics = main.CreateGraphics();
}
}
or the graphics object
class Draw
{
public void Paint(Graphics graphics)
{
Rectangle rectangle = new Rectangle(50, 100, 150, 150);
graphics.DrawEllipse(Pens.Black, rectangle);
graphics.DrawRectangle(Pens.Red, rectangle);
}
}
you still need the event handler for the PictureBox, so you would do something like
private void MainForm_Paint(object sender, PaintEventArgs e)
{
Graphics graphics = this.CreateGraphics();
Draw image = new Draw();
image.Paint(graphics);
}

How can i replace the image in pictureBox1 with a crop image of a drawn on the pictureBox1 rectangle area?

First i'm drawing a rectangle on the pictureBox1 with the mouse
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
rect = new Rectangle(e.X, e.Y, 0, 0);
painting = true;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
rect = new Rectangle(
rect.Left,
rect.Top,
Math.Min(e.X - rect.Left, pictureBox1.ClientRectangle.Width - rect.Left),
Math.Min(e.Y - rect.Top, pictureBox1.ClientRectangle.Height - rect.Top));
}
this.pictureBox1.Invalidate();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (painting == true)
{
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, rect);
}
}
}
The variable rect is global Rectangle and painting is global bool.
Then I did inside the pictureBox1 mouseup event
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
pictureBox1.Image = SaveRectanglePart(pictureBox1.Image, rect);
}
And the method SaveRectanglePart
Bitmap bmptoreturn;
public Bitmap SaveRectanglePart(Image image, RectangleF sourceRect)
{
using (var bmp = new Bitmap((int)sourceRect.Width, (int)sourceRect.Height))
{
using (var graphics = Graphics.FromImage(bmp))
{
graphics.DrawImage(image, 0.0f, 0.0f, sourceRect, GraphicsUnit.Pixel);
}
bmptoreturn = bmp;
}
return bmptoreturn;
}
What i want to do is when i finish drawing the rectangle in the mouseup event to clear the pictureBox1 and replace the image in there with the rectangle image only.
But i'm getting exception parameter not valid in the mouseup event
pictureBox1.Image = SaveBitmapPart(pictureBox1.Image, rect);
And should i dispose somewhere the variable bmptoreturn ?
In the function SaveRectanglePart the variable bmp is Dispose of before the function returns as a result of the using statement. You need to remove the using statement and the code should work.
Bitmap bmptoreturn;
public Bitmap SaveRectanglePart(Image image, RectangleF sourceRect)
{
var bmp = new Bitmap((int)sourceRect.Width, (int)sourceRect.Height)
using (var graphics = Graphics.FromImage(bmp))
{
graphics.DrawImage(image, 0.0f, 0.0f, sourceRect, GraphicsUnit.Pixel);
}
bmptoreturn = bmp;
return bmptoreturn;
}
But we have the issue of what bmptoreturn and pictureBox1.Image were referencing before they were set. The old Image/Bitmap the reference will be lost in memory until garbage collection comes along to free their memory. To be a good programmer we need to Dispose of these Image/Bitmap when we are done with them.
Image tmp = bmptoreturn;
bmptoreturn = bmp;
if(tmp != null)
tmp.Dispose();
...
Image tmp = pictureBox1.Image;
pictureBox1.Image = SaveBitmapPart(pictureBox1.Image, rect);
if(tmp != null)
tmp.Dispose();
Also, I am not sure why you are using bmptoreturn but it is not needed in the code from what I can tell. You can simply return bmp if bmptoreturn is not being used elsewhere.

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.

Manually crop and save image from Picturebox in C# winforms

i am having 1 picturebox in winform using C#, i want to crop some part of image and save that selected part it into image.
Following is my code for croping source image
private void picFace_mousedown(object sender, MouseEventArgs e)
{
rect = new System.Drawing.Rectangle(e.X, e.Y, 0, 0);
this.picFace.Invalidate();
}
private void picFace_mousemove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
rect = new System.Drawing.Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
}
this.picFace.Invalidate();
}
private void picFace_paint(object sender, PaintEventArgs e)
{
using (System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, rect);
}
}
Simple, Bitmap has a clone method takes the rectangle you have defined.
http://msdn.microsoft.com/en-us/library/ms141944.aspx
Then when you have your new bitmap, it comes with a save method
http://msdn.microsoft.com/en-us/library/ktx83wah.aspx

How to draw a line on panel? It is not showing up

I have a Panel called panel1 and I am trying to draw a line on my panel1 using this code:
var g = panel1.CreateGraphics();
var p = new Pen(Color.Black, 3);
var point1 = new Point(234,118);
var point2 = new Point(293,228);
g.DrawLine(p, point1, point2);
But nothing is showing up. Any ideas? This is in a windows form.
Handle the Panel's Paint event and put it in there. What's happening is that it's being drawn once in the constructor but then being drawn over in the Paint event everytime it's called.
private void panel1_Paint(object sender, PaintEventArgs e)
{
base.OnPaint(e);
using(Graphics g = e.Graphics)
{
var p = new Pen(Color.Black, 3);
var point1 = new Point(234,118);
var point2 = new Point(293,228);
g.DrawLine(p, point1, point2);
}
}
Put it in some event after the form has been created and shown on the screen and it should work fine.
It's best to put it in the Paint event, as keyboardP stated, but it will not show up if called before the form is shown on the screen.
To test this you can add a button and add the code to the click event:
private void button1_Click(object sender, EventArgs e)
{
using (Graphics g = panel1.CreateGraphics())
{
g.DrawLine(new Pen(Color.Back, 3), new Point(234,118), new Point(293,228));
}
}
To see your drawing - you can simply make a button with a Click Event and draw when the button is clicked. For example:
private void btnDraw_Click(object sender, EventArgs e)
{
Graphics dc = drawingArea.CreateGraphics();
Pen BlackPen = new Pen(Color.Black, 2);
dc.DrawLine(BlackPen, 0, 0, 200, 200);
BlackPen.Dispose();
dc.Dispose();
}
Oh, and by the way "drawingArea" is the (Name) of either a PictureBox or Panel you have added to your form (to draw in it).
If you follow the other answers and still your drawings are not showing up try removing all controls from the control that is being drawn to. The other controls may be covering whatever you are trying to draw.
private void button2_Click(object sender, EventArgs e)
{
panel1.Paint += new PaintEventHandler(
(object sender1, PaintEventArgs e1) => {
var p = new Pen(Color.Black, 3);
var point1 = new Point(234, 118);
var point2 = new Point(293, 228);
e1.Graphics.DrawLine(p, point1, point2);
}
);
panel1.Invalidate();
}

Categories

Resources