views:

579

answers:

1

Hi I am trying to make simple app that allows one to compare image to transparent PNG templates, by dragging the template over picture. For this I need a way to create a PictureBox that will contain the PNG image and be transparent where the the png is transparent. Everything works fine but the transparency part: When I load a PNG image to the PictureBox (background color is set to transparent) it shows the background color of the containing panel rather than the image that it hovers.

I searched but only found a way to make the PictureBox completely transparent.

+1  A: 

It is difficult to make a control that is partially transparent.

What you should do is handle the lower PictureBox's Paint event (the one that does not need to be transparent), and draw the overlay image using e.Graphics.DrawImage(image, x, y). This will draw transparent and semi-transparent images correctly.

EDIT: In response to your comment, there's nothing wrong with calling the Invalidate method in the MouseMove event. However, you will notice some flickering. To solve the flickering, make a control that inherits PictureBox, and call SetStyle(ControlStyles.DoubleBuffered, true) in the constructor.

SLaks
the problem is the dragging - Woudn't be preformance disaster if i need to redraw the image on every pixel movemnt of the mouse?
TheSimon
No, it wouldn't; many many programs do this.
SLaks
thanks. I was so afraid to redraw :D
TheSimon
To solve the flickering, in most cases, I find that Update(); does a better job.
lucifer
@lucifer: Calling `Update()` will not reduce flickering.
SLaks