How to draw image with alpha channel software set in .NET?

This is a .NET application for Windows forms using C ++ / CLI. I have a JPEG that I want to draw in my client area to represent an object. In some cases - for example, when a user drags objects around - I would like to draw an object with some transparency.

What are the different ways available to do this?

+3
source share
2 answers

50%. System.Drawing.Bitmap (GetPixel, SetPixel):

Color pixelColor = bitmap.GetPixel(x,y);
Color transparentPixelColor = MakePixelTransparent(pixelColor);
bitmap.SetPixel(x,y,transparentPixelColor);

MakePixelTransparent() - (- ARGB, A- Argb).

, ( )...

: , :

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 );

. , ...

+2

Source: https://habr.com/ru/post/1715223/


All Articles