Draw an image with custom transparency in GDI +

I draw a lot of images (all sizes = 24x24 pixelformat = 32BppPArgb) on the control using the Drawing.Graphics object and the DrawImage () function. In my application, you can zoom out, which means that the Graphics object has a transform matrix attached to it that controls both scaling and panning.

It makes no sense to draw these icons when the scale drops below 50%, but I would like to make the transition from drawing icons so as not to draw icons more smoothly. I., starting at 70%, the icons should be drawn with an additional transparency factor so that they become fully transparent by 50%.

How can I draw a bitmap with additional transparency without taking much more time than DrawImage ()?

Thanks David

+4
source share
1 answer

You simply create the corresponding ColorMatrix, initialize the ImageAttributes object and pass that ImageAttributes object to one of the overloaded version of Graphics.DrawImage. This sample will give you 50% transparency:

float[][] matrixAlpha = { new float[] {1, 0, 0, 0, 0}, new float[] {0, 1, 0, 0, 0}, new float[] {0, 0, 1, 0, 0}, new float[] {0, 0, 0, 0.5f, 0}, new float[] {0, 0, 0, 0, 1} }; ColorMatrix colorMatrix = new ColorMatrix( matrixAlpha ); ImageAttributes iaAlphaBlend = new ImageAttributes(); iaAlphaBlend.SetColorMatrix( colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap ); 
+7
source

All Articles