DrawLine in a PictureBox

  • Thread starter Thread starter Jon Cosby
  • Start date Start date
J

Jon Cosby

I'm using this to draw rectangles in a PictureBox image. Not all of the
rectangles are complete, and after drawing several, some of them start
disapearing. What's the reason for this?


Pen selPen = new Pen(Color.White);
Graphics graphic = this.PicBox.CreateGraphics();

..................


Point coord1 = new Point(x0, y0);
Point coord2 = new Point(x0, y);
Point coord3 = new Point(x, y0);
Point coord4 = new Point(x, y);

graphic.DrawLine(selPen, coord1, coord2);
graphic.DrawLine(selPen, coord1, coord3);
graphic.DrawLine(selPen, coord2, coord4);
graphic.DrawLine(selPen, coord3, coord4);
 
Jon Cosby said:
I'm using this to draw rectangles in a PictureBox
image. Not all of the rectangles are complete, and
after drawing several, some of them start
disapearing. What's the reason for this?

Unless you draw your graphics in response to a Paint event, they won't be
updated with the rest of the form. You can see this effect by dragging
another window over your window. Try something like this instead:

private void MyPictureBox_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;

using Pen selPen = new Pen(Color.White)
{
// do your drawing here
}
}

PictureBox is not necessarily the best control to use, as this page
explains:
http://www.bobpowell.net/picturebox.htm

P.
 
Bitmap bitmap = new Bitmap(100, 100);
Graphics graphic = Graphics.FromImage(bitmap);

......

Bitmap bitmap = new Bitmap(pb.Size.Width,pb.Size.Height );

pb.Image=bitmap;



or :

class MyPictureBox:PictureBox

{

protected override void OnPaint(PaintEventArgs e)

{

Graphics graphic = e.Graphics;

......

}

}

Regards,

Krzemo.
 
This works for Windows bitmaps and jpeg, but it doesn't like the formatting
of gif files. Is there a way to reset this?


imgFile = this.GetImageDialog.FileName.ToString();
MyBitmap = new Bitmap(imgFile);

graphic = Graphics.FromImage(MyBitmap); // A Graphics object cannot be
created from
//
an image that has an indexed pixel format.
 
Maybe something like this:

Bitmap bitmap = new Bitmap(pb.Size.Width,pb.Size.Height );
MyBitmap = new Bitmap(imgFile);
graphic = Graphics.FromImage(bitmap);
graphic.DrawImageUnscaled(MyBitmap ,0,0,pb.Size.Width,pb.Size.Height ) ;
//draw gif image on bitmap
pb.Image=bitmap;


Regards,
Krzemo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top