Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How do I change the opacity of a picturebox?

Eg, like the opacity of a form but in the form?
I know the form code, this.opacity = 67;.

I want something like this picturebox1.opacity = 55;
Posted
Updated 18-May-11 21:48pm
v2
Comments
Tarun.K.S 19-May-11 3:21am    
It should be this.Opacity = 0.67, not simply 67.
Dalek Dave 19-May-11 3:48am    
Edited for Grammar and Readability.
Philippe Mori 25-Jan-16 23:14pm    
Or in some case, you might simply split the form in 2 or 3 parts and have different opacity and transparency for different regions... You do however need some advanced code to handle moving and activation among other things.

This would be the case if you would need to see through you windows only in some parts and without using WPF which would probably be the best option in such cases.

Transparency does not work directly.
You can either use a workaround like the one shown here[^] or move over to WPF.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 19-May-11 13:01pm    
Abhinav, this is right advice, but chances are, OP does not need PictureBox at all.
I explain what to do in my solution, please see.
--SA
Abhinav S 19-May-11 13:15pm    
Thanks.
 
Share this answer
 
Comments
Dalek Dave 19-May-11 3:48am    
Nice Link.
Tarun.K.S 19-May-11 4:04am    
Thanks.
Sergey Alexandrovich Kryukov 19-May-11 13:01pm    
Tarun, this is good, but chances are, OP does not need PictureBox at all.
I explain what to do in my solution, please see.
--SA
Tarun.K.S 19-May-11 13:44pm    
Thanks SA.
Abhinav is right.

But look at this problem from the other hand: in very many cases you don't need to use PictureBox at all. Good chances are, this is your case, too. Let me put this is this way: whenever using PictureBox makes sense, you don't need transparency. Whenever you need transparency, you don't need a PictureBox at all.

Find the explanation of what to do in my past answer here:
How do I clear a panel from old drawing[^].

Now, you will achieve transparency simply by modifying alpha channel in your colors. To make it effective you need to use System.Drawing.Bitmap.LockBits and either Marshall or unsafe (using pointers), see http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits.aspx[^].

—SA
 
Share this answer
 
Comments
Abhinav S 19-May-11 13:16pm    
A different approach is always good. 5.
Sergey Alexandrovich Kryukov 19-May-11 13:23pm    
It cannot be "always" but in this case it should be good.
Thank you, Abhinav.
--SA
Tarun.K.S 19-May-11 13:43pm    
Makes sense, even I was thinking of altering the "A" or alpha channel but don't know how. I think I will do some research on Bitmap.LockBits then. 5+
Sergey Alexandrovich Kryukov 20-May-11 15:39pm    
Thank you, Tarun.
You cannot alter it per bitmap, only per-bit. In rendering it's simple, you just modify the color of brush, pen, etc.
The code sample I reference above shows how to use LockBits. Without it, efficient bitmap manipulation is impossible (if WPF, there is a writeable bitmap which does it neatly without unsafe, but with copy the bitmap to the array. Neat design, by the way).
--SA
Tarun.K.S 21-May-11 13:55pm    
Okay thanks for the info.
You may use this code block to perform image opacity on an pictureBox control in windows

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Bitmap pic = new Bitmap(WinTest.Properties.Resources.untitled);
            for (int w = 0; w < pic.Width; w++)
            {
                for (int h = 0; h < pic.Height; h++)
                {
                    Color c = pic.GetPixel(w, h);
                    Color newC = Color.FromArgb(50, c);
                    pic.SetPixel(w, h, newC);
                }
            }
            pictureBox1.Image = pic;
        }

    }
}
 
Share this answer
 
Comments
[no name] 19-May-11 5:59am    
is it possible to do this to a button?
yogiCsharp 19-May-11 10:02am    
Yes it will you just have to write Form_load event's code on that button's click event
Sergey Alexandrovich Kryukov 19-May-11 12:53pm    
Even it formally works, this is not a solution at all. GetPixel/SetPixel cannot be used due to unacceptable performance. Misleading recipe!
--SA
yogiCsharp 19-May-11 23:46pm    
Yes GetPixel/SetPixel is tricky, and it is lengthy time consuming code but here it works fine,
Please feel free to post any improved code to do this opacity work I'll be obliged.
As yogiCsharp said:

C#
private void Opacity(ref Bitmap image)
        {
            for (int w = 0; w < image.Width; w++)
            {
                for (int h = 0; h < image.Height; h++)
                {
                    Color c = image.GetPixel(w, h);
                    if (c != Color.Transparent) /*<- it only change colours different than transparency color.*/
                    {
                        Color newC = Color.FromArgb(c.A/2, c.R/2,c.G/2,c.B); /*<- this gives real opacity.*/
                        image.SetPixel(w, h, newC);
                    }
                }
            }
        }


As you can see, in the Color NewC line, I'm using half Alpha (A channel of the pixel), half Red (R channel of the image), half Green (G channel of the image), half Blue (B Channel of the image).
Also is better, so you can put the opacity in percetange on the oppacity parameter:

C#
private void Opacity(ref Bitmap image, int opacity)
        {
            for (int w = 0; w < image.Width; w++)
            {
                for (int h = 0; h < image.Height; h++)
                {
                    Color c = image.GetPixel(w, h);
                    if (c != Color.Transparent) /*<- it only change colours different than transparency color.*/
                    {
                        Color newC = Color.FromArgb(c.A*(opacity/100), c.R*(opacity/100),c.G*(opacity/100),c.B*(opacity/100)); /*<- this gives real opacity.*/
                        image.SetPixel(w, h, newC);
                    }
                }
            }
        }

Sorry for my english Spanish native speacker.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900