Painting On A Form

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 10
Reputation: nlblnx is an unknown quantity at this point 
Solved Threads: 0
nlblnx nlblnx is offline Offline
Newbie Poster

Painting On A Form

 
0
  #1
Jan 27th, 2009
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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Painting On A Form

 
0
  #2
Jan 27th, 2009
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 10
Reputation: nlblnx is an unknown quantity at this point 
Solved Threads: 0
nlblnx nlblnx is offline Offline
Newbie Poster

Re: Painting On A Form

 
0
  #3
Jan 27th, 2009
Originally Posted by LizR View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Painting On A Form

 
0
  #4
Jan 28th, 2009
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:

  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 :

  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.
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC