Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Simple paint app - problem saving

Simple paint app - problem saving

Scheduled Pinned Locked Moved C#
graphicshelpquestionannouncement
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SoftcodeSoftware
    wrote on last edited by
    #1

    I have a picture box, on which I detect mousemove events, and have the user draw on the picture box. Currently, I can save the image, but it's just plain white, without the actual drawing, any ideas where Im going wrong? [CODE:]

    private Graphics m_objGraphics;

    private void pb_white_board_MouseMove(object sender, MouseEventArgs e)
    {
    Rectangle rectEllipse = new Rectangle();

            if (e.Button != MouseButtons.Left) return;
    
            rectEllipse.X = e.X - 1;
            rectEllipse.Y = e.Y - 1;
            rectEllipse.Width = 3;
            rectEllipse.Height = 3;
    
            //pb\_white\_board.Update();
    
            m\_objGraphics.DrawEllipse(System.Drawing.Pens.Black, rectEllipse);
            Brush blackBrush = Brushes.Black;
            m\_objGraphics.FillEllipse(blackBrush, rectEllipse);
        }
    

    private void btn_save_whiteboard_Click(object sender, EventArgs e)
    {
    Bitmap bitmap = new Bitmap(pb_white_board.Width, pb_white_board.Height);
    pb_white_board.DrawToBitmap(bitmap, new Rectangle(0, 0, pb_white_board.Width, pb_white_board.Height));
    m_objGraphics.Save("image1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
    }

    Thanks, Ben.

    A M 2 Replies Last reply
    0
    • S SoftcodeSoftware

      I have a picture box, on which I detect mousemove events, and have the user draw on the picture box. Currently, I can save the image, but it's just plain white, without the actual drawing, any ideas where Im going wrong? [CODE:]

      private Graphics m_objGraphics;

      private void pb_white_board_MouseMove(object sender, MouseEventArgs e)
      {
      Rectangle rectEllipse = new Rectangle();

              if (e.Button != MouseButtons.Left) return;
      
              rectEllipse.X = e.X - 1;
              rectEllipse.Y = e.Y - 1;
              rectEllipse.Width = 3;
              rectEllipse.Height = 3;
      
              //pb\_white\_board.Update();
      
              m\_objGraphics.DrawEllipse(System.Drawing.Pens.Black, rectEllipse);
              Brush blackBrush = Brushes.Black;
              m\_objGraphics.FillEllipse(blackBrush, rectEllipse);
          }
      

      private void btn_save_whiteboard_Click(object sender, EventArgs e)
      {
      Bitmap bitmap = new Bitmap(pb_white_board.Width, pb_white_board.Height);
      pb_white_board.DrawToBitmap(bitmap, new Rectangle(0, 0, pb_white_board.Width, pb_white_board.Height));
      m_objGraphics.Save("image1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
      }

      Thanks, Ben.

      A Offline
      A Offline
      aamironline
      wrote on last edited by
      #2

      Two things... 1 - You are not dwawing on a PictureBox and 2 - You are trying to fetch drawn image from the PictureBox where painting has not taken place... Cheers ;)

      M Aamir Maniar aamirOnline.com

      1 Reply Last reply
      0
      • S SoftcodeSoftware

        I have a picture box, on which I detect mousemove events, and have the user draw on the picture box. Currently, I can save the image, but it's just plain white, without the actual drawing, any ideas where Im going wrong? [CODE:]

        private Graphics m_objGraphics;

        private void pb_white_board_MouseMove(object sender, MouseEventArgs e)
        {
        Rectangle rectEllipse = new Rectangle();

                if (e.Button != MouseButtons.Left) return;
        
                rectEllipse.X = e.X - 1;
                rectEllipse.Y = e.Y - 1;
                rectEllipse.Width = 3;
                rectEllipse.Height = 3;
        
                //pb\_white\_board.Update();
        
                m\_objGraphics.DrawEllipse(System.Drawing.Pens.Black, rectEllipse);
                Brush blackBrush = Brushes.Black;
                m\_objGraphics.FillEllipse(blackBrush, rectEllipse);
            }
        

        private void btn_save_whiteboard_Click(object sender, EventArgs e)
        {
        Bitmap bitmap = new Bitmap(pb_white_board.Width, pb_white_board.Height);
        pb_white_board.DrawToBitmap(bitmap, new Rectangle(0, 0, pb_white_board.Width, pb_white_board.Height));
        m_objGraphics.Save("image1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        Thanks, Ben.

        M Offline
        M Offline
        mav northwind
        wrote on last edited by
        #3

        Where is m_objGraphics created? I guess that you're creating the Graphics object somewhere during initialization of your code and then use it later, which is a big no-no! To do it correctly you have to call methods on a Graphics object you get from the Image displayed in the PictureBox:

        private void pb_white_board_MouseMove(object sender, MouseEventArgs e)
        {
        if (e.Button != MouseButtons.Left) return;

        Rectangle rectEllipse = new Rectangle(e.X - 1, e.Y - 1, 3, 3);

        using (Graphics g = Graphics.FromImage(pb_white_board.Image))
        {
        g.DrawEllipse(System.Drawing.Pens.Black, rectEllipse);
        g.FillEllipse(System.Drawing.Brushes.Black, rectEllipse);
        }
        // Invalidate the picturebox so that you see the modified image
        pb_white_board.Invalidate();
        }

        Regards, mav -- Black holes are the places where God divided by 0...

        S 1 Reply Last reply
        0
        • M mav northwind

          Where is m_objGraphics created? I guess that you're creating the Graphics object somewhere during initialization of your code and then use it later, which is a big no-no! To do it correctly you have to call methods on a Graphics object you get from the Image displayed in the PictureBox:

          private void pb_white_board_MouseMove(object sender, MouseEventArgs e)
          {
          if (e.Button != MouseButtons.Left) return;

          Rectangle rectEllipse = new Rectangle(e.X - 1, e.Y - 1, 3, 3);

          using (Graphics g = Graphics.FromImage(pb_white_board.Image))
          {
          g.DrawEllipse(System.Drawing.Pens.Black, rectEllipse);
          g.FillEllipse(System.Drawing.Brushes.Black, rectEllipse);
          }
          // Invalidate the picturebox so that you see the modified image
          pb_white_board.Invalidate();
          }

          Regards, mav -- Black holes are the places where God divided by 0...

          S Offline
          S Offline
          SoftcodeSoftware
          wrote on last edited by
          #4

          Thanks, works a treat now :D

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups