I have a small app that looks pretty much like old Paint from Windows. I implemented all the Graphic using picture box Paint event. The only problem is that when I click this button a Color Dialog box should appear and let me change the color of my pen. But whenever I click the button the box never appears and my program gets stuck in the Paint event, most precisely at the line where I do the following:
pictureBox1.Image = bmp;
What I am doing wrong? Please, I would appreciate any help!
I hope the following code snippet is enough.
This is my picturebox Paint event:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using(Graphics g = Graphics.FromImage(bmp))
{
if (lineButton && mouseIsUp)
{
g.DrawLine(myPen, mAnchorPoint, mFinalPoint);
mAnchorPoint = Point.Empty;
mFinalPoint = Point.Empty;
}
pictureBox1.Image = bmp;
}
}
And this is the button event that fires up when I wish to change my pen color:
private void ColorButton_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
if (cd.ShowDialog() == DialogResult.OK)
{
myPen.Color = cd.Color;
}
}
Don't use pictureBox1.Image = bmp; inside pictureBox1_Paint. Instead:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
using(Graphics g = Graphics.FromImage(bmp))
{
if (lineButton && mouseIsUp)
{
g.DrawLine(myPen, mAnchorPoint, mFinalPoint);
mAnchorPoint = Point.Empty;
mFinalPoint = Point.Empty;
}
//pictureBox1.Image = bmp;
}
e.Graphics.DrawImage(bmp, 0, 0);
}
Or, a better approach, set pictureBox1.Image = bmp; once, do all your drawings on bmp(not in pictureBox1_Paint) and final call pictureBox1.Invalidate();. You don't need to write code in pictureBox1_Paint().
Related
Here is my source code. I can't seem to get the bitmap to show the lines drawn on the panel when I move the mouse with the button pressed. Frustrated and looking for someone to help me finish the code so I can complete the app for my 9-yo daughter. Thank you in advance...
namespace TV_PAINT
{
public partial class ALANA_PAINT : Form
{
Graphics g;
Pen p = new Pen(Color.Black, 7);
Point sp = new Point(0, 0);
Point ep = new Point(0, 0);
int m = 0;
Bitmap BP;
public ALANA_PAINT()
{
InitializeComponent();
tb1.Text = p.Width.ToString();
BP = new Bitmap(pnl1.ClientSize.Width, pnl1.ClientSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
}
private void closeButton_Click(object sender, EventArgs e)
{
pnl1.Dispose();
p.Dispose();
this.Close();
}
private void clearButton_Click(object sender, EventArgs e)
{
//pnl1.Invalidate();
p.Color = System.Drawing.Color.Black;
p.Width = 7;
tb1.Text = p.Width.ToString();
//pnl1.Invalidate();
}
private void pnl1_MouseDown(object sender, MouseEventArgs e)
{
sp = e.Location;
if (e.Button == MouseButtons.Left)
m = 1;
if (e.Button == MouseButtons.Right)
m = 1;
}
private void pnl1_MouseMove(object sender, MouseEventArgs e)
{
if (m == 1)
{
ep = e.Location;
//g = pnl1.CreateGraphics();
Graphics g = Graphics.FromImage(BP);
g.DrawLine(p, sp, ep);
}
sp = ep;
}
private void pnl1_MouseUp(object sender, MouseEventArgs e)
{
m = 0;
}
BP is just a variable in the form. As I can see, it is not displayed anywhere in your form. Why do you need a bitmap for it.
You can do something like this, just get the graphics of your form, and draw using that graphic. https://msdn.microsoft.com/en-us/library/ztxk24yx(v=vs.110).aspx
Noted: you need to do it on PaintEvent of the form, otherwise your drawing will be removed after the next repaint, so you need some variables to store all of your lines, then draw all of them in the paint event.
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(0, 0, 200, 300));
myBrush.Dispose();
formGraphics.Dispose();
Updated:
If you want to save your change to a bitmap. You can use Form.DrawToBitmap to save your drawing in the form to a bitmap, then call bitmap.Save() to a file in directory.
Am developing a paint application in C# , VS2010
The start-up interface is a blank pictureBox ,
I did a mouseDown and a mouseMove event Handler for brush tool paint
and it works fine
When i try to save the new picture (After painting on the blank pictureBox)
I enter the file , and it's only a blank picture.
The problem is that the code is not saving the effects.
why?
mouseDown event handler
private void mouseDown(object sender, MouseEventArgs e)
{
if (CurrentFunction == "DrawFree")
{
if (e.Button == MouseButtons.Left)
ReleaseFlag = true;
StartPoint = e.Location;
}
}
mouseMove event handler
private void mouseMove(object sender, MouseEventArgs e)
{
if (CurrentFunction == "DrawFree")
{
if (ReleaseFlag)
{
EndPoint = e.Location;
g = pictureBox1.CreateGraphics();
g.DrawLine(p, StartPoint, EndPoint);
}
StartPoint = EndPoint;
}
}
Save code
private void savePhotoToolStripMenuItem_Click(object sender, EventArgs e)
{
using (Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width,
pictureBox1.ClientSize.Height))
{
using (Graphics g = Graphics.FromImage(bmp))
{
Image yourImage = pictureBox1.Image;
Bitmap yourBitmap = new Bitmap(yourImage);
g.DrawImage(yourBitmap,
new Rectangle(0, 0, bmp.Width, bmp.Height),
new Rectangle(0, 0, yourImage.Width, yourImage.Height),
GraphicsUnit.Pixel);
}
bmp.Save(#"d:\yourfile.png", ImageFormat.Png);
}
}
Since you Bitmapped "yourBitmap" with yourImage, try replacing bmp.Save with yourBitmap.Save
yourBitmap.Image.Save(#"d:\yourfile.png", ImageFormat.Png);
I have a pictureBox1 with image inside and when i click on it its drawing points.
Now i added a reset button i called it when i click on it its should clear all the drawings i did on the pictureBox and leavethe image inside without the drawings on it.
I did:
private void button4_Click(object sender, EventArgs e)
{
Graphics graphics;
graphics = pictureBox1.CreateGraphics();
graphics.DrawImage(pictureBox1.Image, 0, 0);
}
So i draw a lot of points on pictureBox1 then click the button and all points are gone but then once i click on the picturebox1 again i see also the new points but also the old points i did before the clearing.
How can i clear the old drawings so it wont show up on the next clicks ?
This is the paint event: Moved the paint event to a new class:
public static void Paint(List<PointF> pb1points, GraphicsPath pb1gp, Point movingPoint, PictureBox pictureBox1, Graphics e)
{
e.Clear(Color.White);
e.DrawImage(pictureBox1.Image, movingPoint);
Pen p;
p = new Pen(Brushes.Green);
foreach (PointF pt in pb1points)
{
e.FillEllipse(Brushes.Red, pt.X, pt.Y, 3f, 3f);
}
using (Pen pp = new Pen(Color.Green, 2f))
{
pp.StartCap = pp.EndCap = LineCap.Round;
pp.LineJoin = LineJoin.Round;
e.DrawPath(pp, pb1gp);
}
}
You can try using Graphics.Clear().
Reference: http://msdn.microsoft.com/en-us/library/system.drawing.graphics.clear(v=vs.110).aspx
setting the Image property to null should work.
picBox.Image = null;
Ii it's not worked ,you might be used the InitialImage property to display your image.
pictBox.InitialImage = null;
Please refer the link:
Clear image on picturebox
This is working:
private void button4_Click(object sender, EventArgs e)
{
Graphics graphics;
graphics = pictureBox1.CreateGraphics();
graphics.DrawImage(pictureBox1.Image, 0, 0);
pb1points = new List<PointF>();
}
i have one picture showing of the human body and i want to use shapes to locate the injuries of the patient. all the shapes will shows off when the user click button. right now im testing with only one shape.
here is my code.
private void button7_Click_4(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.Clear(this.BackColor);
Image img = Image.FromFile("C:\\Users\\HDAdmin\\Pictures\\humanbody\\effect2.png");
g.DrawImage(img, 0, 0, img.Height, img.Width);
g.Dispose();
}
right now, the shape appear at the back of the image. so how i want to make the shape appear in front of the picture?
Couple of issues.
1) Painting should happen in a paint event. Do not use CreateGraphics since that will only be a temporary drawing.
2) Your DrawImage width and height arguments are reversed.
3) It doesn't look like you are painting the PictureBox control that you have on the form:
private Image img;
public Form1() {
InitializeComponent();
button1.Click += button1_Click;
pictureBox1.Paint += pictureBox1_Paint;
}
void button1_Click(object sender, EventArgs e) {
img = = Image.FromFile("C:\\Users\\HDAdmin\\Pictures\\humanbody\\effect2.png");
pictureBox1.Invalidate();
}
void pictureBox1_Paint(object sender, PaintEventArgs e) {
e.Graphics.Clear(pictureBox1.BackColor);
if (img != null) {
e.Graphics.DrawImage(img, 0, 0, img.Width, img.Height);
//Draw test shape:
e.Graphics.DrawRectangle(Pens.Red, new Rectangle(10, 10, 20, 60));
}
}
I think you should first get graphics of human image and then draw shape image on it. Some thing like that :
Image img = Image.FromFile("C:\\Users\\HDAdmin\\Pictures\\humanbody\\effect2.png");
Graphics g = Graphics.FromImage ( img );
g.DrawImage(ShapeImage, 0, 0, 30, 30); // you can set your required x,y,width,height
I want to draw a editable TextBox on top of picture box and the user is allowed to enter text into this box.After entering text the text box should disappear and the text entered should be painted to the picture in the picture box.Please help me on this,I'am doing this in c#.
Bitmap myBitmap = new Bitmap("C:\\myImage.jpg");
Graphics g = Graphics.FromImage(myBitmap);
g.DrawString("My\nText", new Font("Tahoma", 20), Brushes.White, new PointF(0, 0));
Im stuck with this
I think you are confusing "drawing" with the "editable" part.
It sounds like you just want to use a TextBox. A "basic" demonstration:
private Bitmap bmp = new Bitmap(256, 256);
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Image = bmp;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
TextBox txt = new TextBox();
txt.Location = e.Location;
txt.Width = 120;
txt.Leave += new EventHandler(txt_Leave);
pictureBox1.Controls.Add(txt);
}
void txt_Leave(object sender, EventArgs e)
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawString(((TextBox)sender).Text, ((TextBox)sender).Font, Brushes.Black, ((TextBox)sender).Location);
}
((TextBox)sender).Leave -= new EventHandler(txt_Leave);
pictureBox1.Controls.Remove((TextBox)sender);
((TextBox)sender).Dispose();
pictureBox1.Invalidate();
}
Sound like a TextBox control of MSPaint program, is it right?
Try this approach: http://bytes.com/topic/c-sharp/answers/230866-how-insert-text-bitmap-image-using-c
Hope this help.
But in your code the only way to actually draw the text from textBox is to change focus on something else (e.g. by Tab key).
https://stackoverflow.com/a/7350238/2359840