Windows Develop Bookmark and Share   
 index > Windows Forms Designer > transparant background picturebox
 

transparant background picturebox

Hello,

I was wondering if there is any way to have a certain picture in a picturebox that isn't a square form, like for example acat but instead of having the background white, I want it to be transparant is that possible?

What I mean is this: http://img185.imageshack.us/my.php?image=project7ma8.jpg

Thanks in advance,

Ruben Pieters

RubenPieters  Monday, February 04, 2008 2:52 PM
RubenPieters wrote:

Is that command also possible for a picturebox? (tried, but didn't work.)


Now that I'm here I can answer your question as well. What I did is I loaded the picture in a bitmap first and then replaced the white color with a transparant one. After that I placed the bitmap in the picturebox in the onpaint event of the picturebox
So the whole source looks like this:

private Bitmap waitingIcon;

/// <summary>OnLoad of the form. Show progress animation</summary>
private void WaitingSplash_Load(object sender, EventArgs e)
{
pictureBoxWaiting.Paint += new PaintEventHandler(pictureBoxWaiting_Paint);
waitingIcon = Resources.WaitingIcon;
pictureBoxWaiting.BackColor = BackColor;
}

/// <summary>Paints the PictureBox</summary>
private void pictureBoxWaiting_Paint(object sender, PaintEventArgs e)
{
using (Bitmap waitImageTmp = new Bitmap(waitingIcon, waitingIcon.Width, waitingIcon.Width))
{
waitImageTmp.MakeTransparent(Color.White);
e.Graphics.Clear(pictureBoxWaiting.BackColor);
e.Graphics.DrawImage(waitImageTmp, new Point(0, 0));
}
}

Hope it's usefull Smile
HawVie  Tuesday, February 05, 2008 10:02 AM
Try this:

http://msdn2.microsoft.com/en-us/library/system.drawing.bitmap.maketransparent.aspx

waitImageTmp.MakeTransparent(Color.White);

Btw. making a imagecolour transparant can affect the performance, because it actually picks up the color of the parent control and paint it on the top control. Also note that it will make all white pixels transparant and not only those on background behind the cat. Otherwise you will need more advanced bitmap techniques.

HawVie  Monday, February 04, 2008 9:23 PM

Is that command also possible for a picturebox? (tried, but didn't work.)

RubenPieters  Tuesday, February 05, 2008 5:46 AM

I've searched a bit and found that .gifs can be created with a transparent background, so I'll use that.

Thanks for answering anyway!

RubenPieters  Tuesday, February 05, 2008 8:36 AM
RubenPieters wrote:

Is that command also possible for a picturebox? (tried, but didn't work.)


Now that I'm here I can answer your question as well. What I did is I loaded the picture in a bitmap first and then replaced the white color with a transparant one. After that I placed the bitmap in the picturebox in the onpaint event of the picturebox
So the whole source looks like this:

private Bitmap waitingIcon;

/// <summary>OnLoad of the form. Show progress animation</summary>
private void WaitingSplash_Load(object sender, EventArgs e)
{
pictureBoxWaiting.Paint += new PaintEventHandler(pictureBoxWaiting_Paint);
waitingIcon = Resources.WaitingIcon;
pictureBoxWaiting.BackColor = BackColor;
}

/// <summary>Paints the PictureBox</summary>
private void pictureBoxWaiting_Paint(object sender, PaintEventArgs e)
{
using (Bitmap waitImageTmp = new Bitmap(waitingIcon, waitingIcon.Width, waitingIcon.Width))
{
waitImageTmp.MakeTransparent(Color.White);
e.Graphics.Clear(pictureBoxWaiting.BackColor);
e.Graphics.DrawImage(waitImageTmp, new Point(0, 0));
}
}

Hope it's usefull Smile
HawVie  Tuesday, February 05, 2008 10:02 AM
Looks good too, will check it out.

RubenPieters  Tuesday, February 05, 2008 4:15 PM

You can use google to search for other answers

Custom Search

More Threads

• Custom Control inherited from GroupBox
• DefaultValue attribute VC++ definition not working
• Tree View Help Required
• getting the events from anoteher class
• UITypeEditor ListBox help needed how to implement this.
• Is it possible to write a component -Usercontrol in vb.net and use it in vb 6 enviroment?
• Unable to get the window handle for the 'AxRHTemp' control. Windowless ActiveX controls are not supported.
• Adding charts
• How to draw a control on custom form designer with variable size
• Noob question about Composite Controls