| | |
Painting On A Form
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 10
Reputation:
Solved Threads: 0
Hi all,
I am just wondering if someone can help me.
Basically, I have the following code that I am using to allow me to draw on a picturebox.
There are a few things that I need to be able to do and I can't work out exactly how to get them done.
1: I need the drawing to stay in the picturebox once the form is minimized and then restored.
2: I need to be able to save the drawing to an image file.
The DrawToBitmap parts of the code are just left in from when I tried to load the bitmap again with the Pain event for the picturebox.
Any help with this would be appreciated.
Thanks
I am just wondering if someone can help me.
Basically, I have the following code that I am using to allow me to draw on a picturebox.
There are a few things that I need to be able to do and I can't work out exactly how to get them done.
1: I need the drawing to stay in the picturebox once the form is minimized and then restored.
2: I need to be able to save the drawing to an image file.
The DrawToBitmap parts of the code are just left in from when I tried to load the bitmap again with the Pain event for the picturebox.
Any help with this would be appreciated.
Thanks
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.IO; namespace Painter { public partial class Form1 : Form { public Form1() { InitializeComponent(); } bool Drag = false; int NewX; int NewY; int oldX; int oldY; Bitmap bmp; private void Form1_Load(object sender, EventArgs e) { bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height); } private void button1_Click(object sender, EventArgs e) { pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, pictureBox1.Width,pictureBox1.Height)); bmp.Save("c:\\out.bmp", System.Drawing.Imaging.ImageFormat.Bmp); } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { Drag = true; NewX = e.X; NewY = e.Y; } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { Graphics g = pictureBox1.CreateGraphics(); Pen myPen = new Pen(Color.Black, 2); if (Drag == true) { g.DrawLine(myPen, oldX, oldY, e.X, e.Y); oldX = e.X; oldY = e.Y; } oldX = e.X; oldY = e.Y; } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { Drag = false; pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height)); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { } } }
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
Then you need to not draw your picture on the pressing of a button but on the painting of the box, then, you toggle within that method wether you do nothing much, or paint the picture in memory.
This will solve all you problems in one hit
This will solve all you problems in one hit
Last edited by LizR; Jan 27th, 2009 at 4:43 pm.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
•
•
Join Date: Aug 2008
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
Then you need to not draw your picture on the pressing of a button but on the painting of the box, then, you toggle within that method wether you do nothing much, or paint the picture in memory.
This will solve all you problems in one hit
Thanks for the quick repsonse.
I apologise for being thick here, but I don't follow you.
I was just trying to save the image to file when I hit the button (which does save the picturebox to file, but all it shows is grey).
How would you go about putting the drawing that I did on the picturebox into the bitmap I declared and then throwing that back onto the picturebox everytime the paint method is called?
Thanks again.
•
•
Join Date: Aug 2008
Posts: 1,735
Reputation:
Solved Threads: 186
the problem is you arent drawing to your bitmap you made, you're just drawing on the screen which isnt remembered, a little like the whole if you wave a sparkler in the air you see the picture linger for a little but then its gone.
As I said its the same reason when you minimize and return that the image is gone, because it never really was there..
So, with the way you've done it, you cant. its not real.
You have to change a little about what you did.
Firstly, you need to ensure you have made an image in the first place such as:
Now your picturebox has an image for you to work with.
Then for example, you can draw on it with such as :
now open output.bmp and you'll find a white box with a diagonal black line.
As I said its the same reason when you minimize and return that the image is gone, because it never really was there..
So, with the way you've done it, you cant. its not real.
You have to change a little about what you did.
Firstly, you need to ensure you have made an image in the first place such as:
C# Syntax (Toggle Plain Text)
Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height); pictureBox1.Image = b;
Now your picturebox has an image for you to work with.
Then for example, you can draw on it with such as :
C# Syntax (Toggle Plain Text)
Brush bb = Brushes.White; Graphics g = Graphics.FromImage(pictureBox1.Image); g.FillRectangle(bb, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height)); Pen p = new Pen(Color.Black, 2); g.DrawLine(p, 0, 0, pictureBox1.Width, pictureBox1.Height); g.Dispose(); pictureBox1.Invalidate(); pictureBox1.Image.Save("C:\\outpt.bmp");
now open output.bmp and you'll find a white box with a diagonal black line.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
![]() |
Similar Threads
- How would I make a text adventure? (Python)
- Help with drawing on a panel in C# (C#)
- User Drawn Form (C++)
- draw inside form (C#)
- advanced computer insanity (Community Introductions)
- Tell us about yourself! (Community Introductions)
- Design time painting (C#)
- New Thread is not allowing proper Paint Events (C#)
Other Threads in the C# Forum
- Previous Thread: Message Box
- Next Thread: Hello, Please help how to save Listview data to MSSQL database?
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# cast check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ httpwebrequest image index input install java label list listbox listener mailmerge mandelbrot math mouseclick mysql operator path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox robot save saving serialization server sleep socket sockets sql sql-server statistics stream string stringformatting sun table tcp text textbox thread time timer update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






