943,771 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 8325
  • C# RSS
Jan 27th, 2009
0

Painting On A Form

Expand Post »
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

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Drawing.Drawing2D;
  10. using System.IO;
  11.  
  12. namespace Painter
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. }
  20. bool Drag = false;
  21. int NewX;
  22. int NewY;
  23. int oldX;
  24. int oldY;
  25. Bitmap bmp;
  26.  
  27. private void Form1_Load(object sender, EventArgs e)
  28. {
  29. bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
  30.  
  31. }
  32.  
  33. private void button1_Click(object sender, EventArgs e)
  34. {
  35. pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, pictureBox1.Width,pictureBox1.Height));
  36. bmp.Save("c:\\out.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
  37.  
  38. }
  39.  
  40. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  41. {
  42. Drag = true;
  43. NewX = e.X;
  44. NewY = e.Y;
  45. }
  46.  
  47. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  48. {
  49. Graphics g = pictureBox1.CreateGraphics();
  50.  
  51. Pen myPen = new Pen(Color.Black, 2);
  52.  
  53.  
  54. if (Drag == true)
  55. {
  56. g.DrawLine(myPen, oldX, oldY, e.X, e.Y);
  57. oldX = e.X;
  58. oldY = e.Y;
  59. }
  60. oldX = e.X;
  61. oldY = e.Y;
  62. }
  63.  
  64. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  65. {
  66. Drag = false;
  67. pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
  68. }
  69.  
  70. private void pictureBox1_Paint(object sender, PaintEventArgs e)
  71. {
  72.  
  73. }
  74.  
  75.  
  76.  
  77.  
  78. }
  79. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nlblnx is offline Offline
10 posts
since Aug 2008
Jan 27th, 2009
0

Re: Painting On A Form

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
Last edited by LizR; Jan 27th, 2009 at 4:43 pm.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Jan 27th, 2009
0

Re: Painting On A Form

Click to Expand / Collapse  Quote originally posted by LizR ...
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
Hi,
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nlblnx is offline Offline
10 posts
since Aug 2008
Jan 28th, 2009
0

Re: Painting On A Form

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:

C# Syntax (Toggle Plain Text)
  1. Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height);
  2. 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)
  1. Brush bb = Brushes.White;
  2. Graphics g = Graphics.FromImage(pictureBox1.Image);
  3. g.FillRectangle(bb, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
  4. Pen p = new Pen(Color.Black, 2);
  5. g.DrawLine(p, 0, 0, pictureBox1.Width, pictureBox1.Height);
  6. g.Dispose();
  7. pictureBox1.Invalidate();
  8. pictureBox1.Image.Save("C:\\outpt.bmp");

now open output.bmp and you'll find a white box with a diagonal black line.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Message Box
Next Thread in C# Forum Timeline: C++ Dll in C#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC