Search code examples
c#winformsbitmap

Transparent image over a control


I'm doing an application with a splash screen.

I've an image an I'd like to put below a progress bar like :

Example

Example

I've succeeded to make the bitmap transparent. But, now, the image is behind the progress bar

Now

Now

Is there a way to get the image in front of the progress bar ? Thank you. F.

Code :

public partial class Form1 : Form
{
    Bitmap m_l;
    public Form1()
    {
        InitializeComponent();

        m_l = Properties.Resources.LU;

        m_l.MakeTransparent(Color.Transparent);
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawImage(m_l, new Rectangle(new Point(0, -40), new Size(200, 264))); progressBar1.Refresh();
    }
}

Solution

  • (sorry, for missuing the answer function but the answer is to long for a comment)

    @TaW seems like you didnt quite understand the approach, so I will try to explain it in more detail

    OP asked if he can make a transparant Image over another control (a progressbar)

    I assumed this transparent Image is inside a PictureBox, you seem to assume some other control

    to position the control, if my assumption is correct the picturebox, infront of the progress bar all he has to do is right click and click "Bring to Front" on the PictureBox

    and there you have it a "transparent" PictureBox infront of a progressbar - but as you mentioned in your answer we cannot stop there since the "transparent" isnt what I expected, but obviously you knew - its this "parent background color picking" that WinForms does and we end up with a not fully transparent image infront of the ProgressBar but instead one with a gray Background

    Now the posted url comes in place: http://www.richardhyland.com/diary/2009/05/26/how-to-truely-make-a-picturebox-background-transparent/

    This is the code provided, and explained in that url:

    public static System.Drawing.Drawing2D.GraphicsPath Transparent(Image im)
    {
        int x;
        int y;
        Bitmap bmp = new Bitmap(im);
        System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
        Color mask = bmp.GetPixel(0, 0);
    
        for (x = 0; x <= bmp.Width - 1; x++)
        {
            for (y = 0; y <= bmp.Height - 1; y++)
            {
                if (!bmp.GetPixel(x, y).Equals(mask))
                {
                    gp.AddRectangle(new Rectangle(x, y, 1, 1));
                }
            }
        }
        bmp.Dispose();
        return gp;
    }
    

    With this we can achieve a fully transparent Picture box infront of a Progress bar.

    So without this Code, we have this:

    But with that Code:

    Notice, this approach has some downsides:

    1. doesn't work perfectly - as you can see gray pixels around the edges of the image
    2. performs poorly on big Images - since getting each pixel with GetPixel is "challange"

    (Please, ignore the fact that the image shows "JPG" and I am talking about transparent Images - this was just the first image Google search presented me and yes, the file is a transparent png)