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. Invalidate picturebox with rectangle on

Invalidate picturebox with rectangle on

Scheduled Pinned Locked Moved C#
graphics
2 Posts 2 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.
  • C Offline
    C Offline
    Casper Hansen
    wrote on last edited by
    #1

    Hello. I have a simple picturebox where there is a image loaded in Im drawing a rectangle on the picturebox and when I press a button I want to invalidate it (remove the rectangle), but it doesnt seem to work The rectangle is drawn fine and the image loads up to, but when I press my button nothing happens Here is the code im using: private void drawOnPic() { // Attach grapich to picturebox Graphics g = Graphics.FromImage(pictureBox1.Image); // Create a new pen that we shall use for drawing the line Pen PenStyle = new Pen(Color.Red, 1); // Draw a 50x50 pixels rectangle (x, y, width, hight) g.DrawRectangle(PenStyle, 20, 20, 50, 50); } private void button1_Click(object sender, EventArgs e) { pictureBox1.Invalidate(); } private void Form1_Load(object sender, EventArgs e) { drawOnPic(); }

    L 1 Reply Last reply
    0
    • C Casper Hansen

      Hello. I have a simple picturebox where there is a image loaded in Im drawing a rectangle on the picturebox and when I press a button I want to invalidate it (remove the rectangle), but it doesnt seem to work The rectangle is drawn fine and the image loads up to, but when I press my button nothing happens Here is the code im using: private void drawOnPic() { // Attach grapich to picturebox Graphics g = Graphics.FromImage(pictureBox1.Image); // Create a new pen that we shall use for drawing the line Pen PenStyle = new Pen(Color.Red, 1); // Draw a 50x50 pixels rectangle (x, y, width, hight) g.DrawRectangle(PenStyle, 20, 20, 50, 50); } private void button1_Click(object sender, EventArgs e) { pictureBox1.Invalidate(); } private void Form1_Load(object sender, EventArgs e) { drawOnPic(); }

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, Graphics.FromImage(pictureBox1.Image); gives you access to the internals of the image the PB is showing. g.DrawRectangle(...); modifies this image; such modification is irreversible, invalidating the PB only tells it to repaint itself, hence showing the modified image again. If you want to be able to undo graphic stuff, either paint on top of the PB (using its OnPaint handler) or keep a copy of the original image, so you can restore it (PB.Image=oldImage;). :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


      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