开发者

Add additional half-transparent layer on picturebox

I have a standard picture box control with an image on it. I want to add half-transparent layer. It w开发者_开发百科ill not hide an image, it will be some kind of filter.

How can I do it ?


The code would be similar to this:

private void pictureBox1_Paint (object sender, PaintEventArgs e)
{
   using (SolidBrush b = new SolidBrush(Color.FromArgb(128, Color.White))
   {
       e.Graphics.FillRectangle (b, 0, 0, pictureBox1.Width, pictureBox1.Height);
   }

However, the code above simply using a single color brush whose opactity is set to 128. There are other brush classes available, such as LinearGradientBrush, TextureBrush, PAthGradientBrush and HatchBrush.

Recommendation To paint custom effects like you want to (for a button pressed state), a recommended approach would be to generate pre-rendered images that you can create either at runtime of your application or using an external program such as Adobe Photoshop and then save these images.

Then the painting code would simply take the image that represents the current state of your "button" and draw that image in the overriden pictureBox1_Paint method.

This technique results in better graphics painting performance as opposed to simply repainting the same states at different state-change times in the application.


You can register the Paint event and write some code like this:

private void pictureBox1_Paint (object sender, PaintEventArgs e)
    {
      e.Graphics.FillRectangle (Brushes.Transparent, 0, 0, pictureBox1.Width, pictureBox1.Height);
    }

You need to genereate a semi-transparent brush (change the alpa-level of the needed color)

more information about custom painting in controls can be found in MSDN

This is just a small code snippet. To use Print events and the Graphics object correctly read the MSDN stuff first.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜