Reputation: 59516
This is a .NET application on Windows forms using C++/CLI. I have a JPEG that I want to paint in my client area to represent an object. On some occasions -- like when the user is dragging objects around -- I'd like to draw the object with some transparency.
What are the various available ways to do this?
Upvotes: 3
Views: 1811
Reputation: 26410
I would try creating a second image with 50% transparency. As a System.Drawing.Bitmap you can get and set its Pixels (GetPixel, SetPixel):
Color pixelColor = bitmap.GetPixel(x,y);
Color transparentPixelColor = MakePixelTransparent(pixelColor);
bitmap.SetPixel(x,y,transparentPixelColor);
MakePixelTransparent()
would set the alpha value in the supplied color (something like getting the ARGB-value, setting the A-byte and create a new color out of the new Argb-value).
Thats what I would try (I didn't though)...
EDIT: I tried it now, out of curiosity:
Bitmap bitmap = new Bitmap( "YourImageFile.jpg" );
bitmap.MakeTransparent();
for ( int y = 0; y < bitmap.Height; y++ ) {
for ( int x = 0; x < bitmap.Width; x++ ) {
Color pixelColor = bitmap.GetPixel( x, y );
Color transparentPixelColor = Color.FromArgb( pixelColor.ToArgb() & 0x7fffffff );
bitmap.SetPixel( x, y, transparentPixelColor );
}
}
e.Graphics.DrawImage( bitmap, 10, 10 );
Works. That way you can also make only parts of the image transparent...
Upvotes: 2