Windows Develop Bookmark and Share   
 index > Windows Forms General > Draw partially transparent image with Graphics class
 

Draw partially transparent image with Graphics class

Hi,

I am creating a user control class and would like to draw an image on it using the Graphics class. But, I need the white bits to not draw. I know the command MyGraphics.DrawImage(Image,X,Y,Width,Height), but this cannot draw transparent parts. Is it possible to do this?

Thanks,,

Matt
Codeeater  Friday, August 28, 2009 5:11 PM
Use Bitmap.MakeTransparent. For example:

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e) {
using (Bitmap bmp = Properties.Resources.Example) {
bmp.MakeTransparent(Color.White);
e.Graphics.DrawImage(bmp, 0, 0);
}
}
}

Hans Passant.
  • Marked As Answer byCodeeater Sunday, August 30, 2009 10:13 AM
  •  
nobugz  Friday, August 28, 2009 8:21 PM
Hello,
I have an idea. Cheat. Fake it out.

Draw your stuff on a borderless form dynamically aligned over top of your current form.


Rudedog =8^D
Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2  Friday, August 28, 2009 5:15 PM
Set CompositingMode to SourceOver and set the alpha of thepixels you want transparent to 0.
JohnWein  Friday, August 28, 2009 5:29 PM
Use Bitmap.MakeTransparent. For example:

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e) {
using (Bitmap bmp = Properties.Resources.Example) {
bmp.MakeTransparent(Color.White);
e.Graphics.DrawImage(bmp, 0, 0);
}
}
}

Hans Passant.
  • Marked As Answer byCodeeater Sunday, August 30, 2009 10:13 AM
  •  
nobugz  Friday, August 28, 2009 8:21 PM

You can use google to search for other answers

Custom Search

More Threads

• Master-Detail Comobox Databinding woes!
• datagridview combo box problem
• Tooltip do not reshow after they expire
• Bring form to the front
• Windows User Control in Web Page using OBJECT tag.
• Text Box & Dates
• comparing dates...
• DataGridView and Custom Columns
• system tray app help - want the app to start minimized
• DatagridView columns hide