开发者

Transparent PNG in PictureBox

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 sea开发者_StackOverflow中文版rched but only found a way to make the PictureBox completely transparent.


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.


This works fine if you add the pictureBoxOnTop to the list of controls of the pictureBoxToBeHovered to be hovered.

        pictureBoxToBeHovered.Controls.Add(pictureBoxOnTop);
        pictureBoxOnTop.BackColor = Color.Transparent;
        pictureBoxOnTop .Location = new Point(0,0) ;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜