Michael
Michael

Reputation: 13636

How to call function with PaintEventArgs argumnt?

Given the following code example from MSDN:

private void GetPixel_Example(PaintEventArgs e)
    {

        // Create a Bitmap object from an image file.
        Bitmap myBitmap = new Bitmap(@"C:\Users\tanyalebershtein\Desktop\Sample Pictures\Tulips.jpg");

        // Get the color of a pixel within myBitmap.
        Color pixelColor = myBitmap.GetPixel(50, 50);

        // Fill a rectangle with pixelColor.
        SolidBrush pixelBrush = new SolidBrush(pixelColor);
        e.Graphics.FillRectangle(pixelBrush, 0, 0, 100, 100);
    }

How can one call the Paint function?

Upvotes: 3

Views: 27858

Answers (3)

Charlie
Charlie

Reputation: 9118

You should't call the Paint method yourself. The Paint method will be called by the .NET framework whenever your component needs to be painted. This typically happens when the window is moved, minimized, etc.

If you want to tell the .NET framework to repaint your component, call Refresh() on either the component or one of its parents.

Upvotes: 0

codeteq
codeteq

Reputation: 1532

You can call this method from a PaintEvent of a form e.g.

public class Form1 : Form
{
    public Form1()
    {
        this.Paint += new PaintEventHandler(Form1_Paint);
    }

    //....

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        GetPixel_Example(e);
    }

    private void GetPixel_Example(PaintEventArgs e)
    {
        // Create a Bitmap object from an image file.
        Bitmap myBitmap = new Bitmap(@"C:\Users\tanyalebershtein\Desktop\Sample Pictures\Tulips.jpg");

        // Get the color of a pixel within myBitmap.
        Color pixelColor = myBitmap.GetPixel(50, 50);

        // Fill a rectangle with pixelColor.
        SolidBrush pixelBrush = new SolidBrush(pixelColor);
        e.Graphics.FillRectangle(pixelBrush, 0, 0, 100, 100);
    }
}
  1. Loads the image
  2. Get the color of the pixel at x=50,y=50
  3. Draw a filled rectangle with that color at 0,0 with a size of 100,100 on the Graphics-Object of the form

Upvotes: 1

Pranay Rana
Pranay Rana

Reputation: 176956

from the paint event something like this

private PictureBox pictureBox1 = new PictureBox();

pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);

private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
   GetPixel_Example(e) ;
}

Upvotes: 9

Related Questions