57196

How to call paint event from a button click event?

Question:

I have a pictureBox with the event "paint" and I also got code in there to draw graphics.

such as:

public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { Graphics graphics = e.Graphics; graphics.DrawEllipse(Pens.Blue, 10, 10, 100, 100); } private void button1_Click(object sender, EventArgs e) { pictureBox1.Invalidate(); } }

}

My question is. How to fire these events of from a button_click event? I searched alot on the web and found alot of awnsers like ".invalidate()" or ".Refresh()" and ".Update". But my assignment tells me that I need to do it with the .Refresh() method and the painting needs to be in a pictureBox.

What I notice from .Refresh() method. Is that it erases the pictureBox(draws the pictureBox how it was created on initialize). So firing off .Refresh method in a button did not work for me.

Any other suggestions how to fire off a paint event from a button?

Answer1:

This should do the trick:

public partial class Form1 : Form { public Form1() { InitializeComponent(); pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height); } private void button1_Click(object sender, EventArgs e) { using (var g = Graphics.FromImage(pictureBox1.Image)) { g.DrawEllipse(Pens.Blue, 10, 10, 100, 100); pictureBox1.Refresh(); } } }

Recommend

  • Drawing event draws two object
  • heart shaped picturebox
  • Show trail of moving pixel in C# WinForm project
  • c# Bitmap.Save A generic error occurred in GDI+ windows application
  • What is the best practice for changing status of a record in database
  • Python regular expression to remove all square brackets and their contents
  • Way to display image scale in picturebox [closed]
  • Light-weight winform graphic element substitute of Control
  • Drawline offset at both ends
  • Enhanced crosshair cursor possible?
  • c# Resize parent control when a child pictureBox changes the Image
  • C# Invalidate troubles
  • Setting a variable in a for loop (with temporary variable) Lua
  • Recreate Apple Watch fitness tracker ‘progress’ bar - gradient on CAShapeLayer stroke
  • draw a B+ tree in latex
  • error in docker build publish plugin
  • Does the failbit effect the call ignore on cin?
  • How do I recognize a line break with a switch case that evaluates a char in Java?
  • MarkLogic Node.js Sort on “last-modified”
  • 3.0.0.M1: SSL - Invalid keystore format
  • Winforms: Add a close “x” button in a UserControl
  • OCILogon during Grace Period - ORA-28002
  • Bad interaction between Zope2 XML-RPC and AT Image mutator?
  • Issue with routerLink directive
  • how to solve invalid conversion specifier warning in iphone app
  • using html data-attributes as css-variable (i.e. text-shadow)
  • Trying to string.Join an IList
  • Elasticsearch script query involving root and nested values
  • Symfony 2. CSRF token is invalid
  • c# open webrowser in many tab
  • CakePHP ACL tutorial initDB function warnings
  • Using a canvas object in a thread to do simple animations - Java
  • How to Cache Real-time Data?
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Using variable in a value field in jMeter
  • Numpy divide by zero. Why?
  • AT Commands to Send SMS not working in Windows 8.1
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How do I configure my settings file to work with unit tests?
  • Is it possible to post an object from jquery to bottle.py?