Asma Good
Asma Good

Reputation: 421

loading image on a panel

First I load an image in a picturebox. Then I measure the areas in it and create a new picture. Now I want to load the image in a panel and draw a line by mouse. I added to my form:

private Image imag;

I also added to my project:

private void drawP_Paint(object sender, PaintEventArgs e)
{
    Graphics g = drawP.CreateGraphics(); 
    g.DrawImage(imag, new Point(0,0));
}

I set the image in a function:

imag = (Image)bm;     
// or
imag = picturebox1.Image; // the made picture   
drawP.Invalidate();

But nothing appears when running the project.

Upvotes: 0

Views: 14421

Answers (2)

LarsTech
LarsTech

Reputation: 81675

Try changing it to this:

private void drawP_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawImage(imag, new Point(0,0));
}

Also, from your comments, it sounds like you may not have the event wired up. Example:

public Form1()
{
  InitializeComponent();
  drawP.Paint += drawP_Paint;
}

To draw a line on that image:

private void button1_Click(object sender, EventArgs e) {
  using (Graphics g = Graphics.FromImage(imag)) {
    g.DrawLine(Pens.Red, new Point(0, 0), new Point(32, 32));
  }
  drawP.Invalidate();
}

Upvotes: 1

Marco
Marco

Reputation: 57593

You should place your code in panel Paint event.

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        Image imag = Image.FromFile(filename);
        e.Graphics.DrawImage(imag, new Point(0,0)); 
    }

This makes you sure that everytime panel is redrawn (after beeing invalidated for any reason) your image is visible.

Upvotes: 2

Related Questions