Draw an image with custom transparency in GDI+

你说的曾经没有我的故事 提交于 2019-12-23 11:58:23

问题


I'm drawing lots of images (all of them dimensions=24x24 pixelformat=32BppPArgb) onto a Control using a Drawing.Graphics object and the DrawImage() function. It's possible to zoom out in my application which means that the Graphics object has a Transform Matrix attached to it which controls both zooming and panning.

There's no point in drawing these icons when the zoom drops below 50%, but I'd like to make the transition from drawing icons to not drawing icons smoother. I.e., starting at 70%, icons should be drawn with an additional transparency factor so that they will become completely transparent at 50%.

How can I draw a bitmap with an additional transparency without it taking significantly longer than DrawImage()?

Thanks, David


回答1:


You just create an appropriate ColorMatrix, initialize a ImageAttributes object with it and pass that ImageAttributes object to one of the overloaded version of Graphics.DrawImage. This sample will give you 50% transparancy:

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


来源:https://stackoverflow.com/questions/1735042/draw-an-image-with-custom-transparency-in-gdi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!