Picturebox slider control transparency
Asked Answered
C

1

5

I've a PictureBox in my form and load a image in it.

I need this PictureBox to change the transparency (opacity, visibilit..etc), because I need the user to see the image behind this PictureBox better, so when he wants, he just drag the control slider and the image starts to turn invisible, step by step, until he finds its ok, lets say 50% transparency.

I added the control slider but, cant find the way to do the rest. I tried the pictureBox.Opacity, pictureBox.Transparency, nothing works.

Combo answered 25/6, 2017 at 19:36 Comment(3)
What are you targetting: Winforms, WPF, ASP..? Always tag your question correctly! - In Winforms there is no simple way to do this. You will have to modify the alpha of the PBox.Image. See ColorMatrix for a fast way.. WPF can do it easily, I believe.Hydroelectric
there is natively no concept of 'behind' - all controls are siblings no matter the location. Please read How to Ask and take the tourCritchfield
there is natively no concept of 'behind Well at least in Winforms there is nesting and the picturebox is either nested to the form or a tabpage or a groupbox or a panel or some container. so, there is a concept of behind.Hydroelectric
H
13

In winforms you will have to modify the alpha of the PictureBox.Image.

To do that in a fast way use a ColorMatrix!

Here is an example:

enter image description here

The trackbar code:

Image original = null;

private void trackBar1_Scroll(object sender, EventArgs e)
{
    if (original == null) original = (Bitmap) pictureBox1.Image.Clone();
    pictureBox1.BackColor = Color.Transparent;
    pictureBox1.Image = SetAlpha((Bitmap)original, trackBar1.Value);
}

To use a ColorMatrix we need this using clause:

using System.Drawing.Imaging;

Now for the SetAlpha function; note that it is basically a clone from the MS link..:

static Bitmap SetAlpha(Bitmap bmpIn, int alpha)
{
    Bitmap bmpOut = new Bitmap(bmpIn.Width, bmpIn.Height);
    float a = alpha /  255f;
    Rectangle r = new Rectangle(0, 0, bmpIn.Width, bmpIn.Height);

    float[][] matrixItems = { 
        new float[] {1, 0, 0, 0, 0},
        new float[] {0, 1, 0, 0, 0},
        new float[] {0, 0, 1, 0, 0},
        new float[] {0, 0, 0, a, 0}, 
        new float[] {0, 0, 0, 0, 1}};

    ColorMatrix colorMatrix = new ColorMatrix(matrixItems);

    ImageAttributes imageAtt = new ImageAttributes();
    imageAtt.SetColorMatrix( colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

    using (Graphics g = Graphics.FromImage(bmpOut))
        g.DrawImage(bmpIn, r, r.X, r.Y, r.Width, r.Height, GraphicsUnit.Pixel, imageAtt);

    return bmpOut;
}

Note the the ColorMatrix expects its elements to be be scaling factors with 1 being the identity. The TrackBar.Value goes from 0-255, just like a Bitmap alpha channel..

Also note that the function creates a new Bitmap, which may lead to GDI leaking. Here the PictureBox takes care of it, it seems; at least testing it with the taskmanger ('Details' - turn on GDI-objects column!) shows no problems :-)

Final note: This will work if and only if the PictureBox is nested in the control 'behind' it! If it merely overlaps this will not work!! In my example it sits on a TabPage, which is a Container and anything you drop on it gets nested inside. It would work the same if I had dropped it onto a Panel. But PictureBoxes are no containers. So if you want another PictureBox to show up behind it, then you need code to create the nesting: pboxTop.Parent = pBoxBackground; pboxTop.Location = Point.Empty;

Hydroelectric answered 26/6, 2017 at 11:25 Comment(1)
Tnk you very much my friend. I´v tried many days. Im new to C#. This works perfect. Exacly the way i need it. Tnk you very much.Combo

© 2022 - 2024 — McMap. All rights reserved.