DrawImage question

L

Lev Elbert

Using DrawImage I draw (in OnPaint) a bitmap on a form

protected override void OnPaint(PaintEventArgs e)
{
if (_album.Count > 0)
{
// Paint the current image
Photograph photo = _album.CurrentPhoto;
Graphics g = e.Graphics;
......
g.DrawImage(photo.Image, this.DisplayRectangle);
}

All works, but when I drug the corner of the form (resize the form i
horizontal direction) the picture is redrawn incorrectly. Namely: it
should be stretched, but instead of stretching it draws some pieces of
the original image several times. It stretches too, but also repeats
the fragments. The effect is "accumulative". I other words: the more
you move, the more junk you see.

xppro, sp2, net 1.1, vs 2003
 
B

Bela Istok

I have some questions for you:
1.- Why you don't use a PictureBox?
2.- If the PictureBox don't serves your need, why don't do a class that
encapsulate the Picture(s)?

Regards,

Bela Istok
 
T

Tom Spink

Lev said:
Using DrawImage I draw (in OnPaint) a bitmap on a form

protected override void OnPaint(PaintEventArgs e)
{
if (_album.Count > 0)
{
// Paint the current image
Photograph photo = _album.CurrentPhoto;
Graphics g = e.Graphics;
......
g.DrawImage(photo.Image, this.DisplayRectangle);
}

All works, but when I drug the corner of the form (resize the form i
horizontal direction) the picture is redrawn incorrectly. Namely: it
should be stretched, but instead of stretching it draws some pieces of
the original image several times. It stretches too, but also repeats
the fragments. The effect is "accumulative". I other words: the more
you move, the more junk you see.

xppro, sp2, net 1.1, vs 2003

Hi,

You need to clean your canvas before you paint your image. Use
FillRectangle to blank out the entire area first, then use DrawImage.
Also, try using this.ClientRectangle as opposed to DisplayRectangle.
 
L

Lev Elbert

1.- Why you don't use a PictureBox?

This is a study of .net. Looks like I found a bug :) I checked on 2
machines. Besides, PictureBox has the same problem.
 
T

Tom Spink

Lev said:
This is a study of .net. Looks like I found a bug :) I checked on 2
machines. Besides, PictureBox has the same problem.
Hi,

Looks like I found a bug

Don't automatically assume this! You may just be getting the same behaviour
on both machines because your code is executing the same... which in fact
it is.

And it's not a bug! The reason you're seeing this behaviour is because your
painting routine doesn't clear the area that you're trying to paint to, and
when you resize the form your exposing an area of the form that has not
been painted.

This is undefined behaviour, and Windows will draw whatever lies on the edge
of the undefined area. You need to put something there.

I made a post talking about a solution, but in hindsight, the other problem
is that your painting code is not getting called when you resize your form.
You need to make sure this happens by using this.SetStyle(...) in your
form's constructor to tell the form to redraw on resize, using the
ControlStyles.ResizeRedraw setting.
 
L

Lev Elbert

You need to clean your canvas before you paint your image. Use
FillRectangle to blank out the entire area first, then use DrawImage.
Also, try using this.ClientRectangle as opposed to DisplayRectangle.

Before I posted the question I tryed blanking the form (both
FillRectangle and Graphics.Clear(SystemColors.Control)) - the same
result.

In my case ClientRectangle is the same as DisplayRectangle.

But thank you anyway. I think this is a bug eather in .net or in the
video driver.
 
L

Lev Elbert

I found a solution! Not sure that this one is THE solution, but anyway.

I have to handle Resize message calll Invalidate() in the handler. Al
works
 

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

Top