What's up with displaying transparent pixel? Closed
-
Still don't know why this happens but it doesn't matter. I found that setting my control to DoubleBuffered = true, the display time goes to 0. Sorry for the post. Can some tell me why displaying the transparent pixel takes so much time? See bottom of sample for time comparison. public class CellColor { public Rectangle Rect; public Color Color; public CellColor(Rectangle rect, Color clr) { Rect = rect; Color = clr; } } // At form load time, this array is loaded with pixel data from a 32 x 32 Icon. // About half of the pixels in the sample icon are transparent (A=0,R=255,G=255,B=255). private CellColor[,]? _CellBounds; protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); DrawCells(e.Graphics); } private void DrawCells(Graphics gr) { int iWd = this._CellBounds!.GetUpperBound(0) + 1; int iHt = this._CellBounds.GetUpperBound(1) + 1; Stopwatch sw = new(); sw.Start(); for (int iRow = 0; iRow < iHt; iRow++) { for (int iCol = 0; iCol < iWd; iCol++) { CellColor cc = this._CellBounds[iRow, iCol]; using(SolidBrush br = new(cc.Color)) { gr.FillRectangle(br, cc.Rect); } } } sw.Stop(); // Displaying the data without pixel modification takes about 60 milliseconds. // When loading _CellBounds, if I replace the transparent pixel with a real color like Color.White, // this loop takes 4 milliseconds. DebugClass.WriteLine(string.Format("{0}", sw.ElapsedMilliseconds)); }
-
Still don't know why this happens but it doesn't matter. I found that setting my control to DoubleBuffered = true, the display time goes to 0. Sorry for the post. Can some tell me why displaying the transparent pixel takes so much time? See bottom of sample for time comparison. public class CellColor { public Rectangle Rect; public Color Color; public CellColor(Rectangle rect, Color clr) { Rect = rect; Color = clr; } } // At form load time, this array is loaded with pixel data from a 32 x 32 Icon. // About half of the pixels in the sample icon are transparent (A=0,R=255,G=255,B=255). private CellColor[,]? _CellBounds; protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); DrawCells(e.Graphics); } private void DrawCells(Graphics gr) { int iWd = this._CellBounds!.GetUpperBound(0) + 1; int iHt = this._CellBounds.GetUpperBound(1) + 1; Stopwatch sw = new(); sw.Start(); for (int iRow = 0; iRow < iHt; iRow++) { for (int iCol = 0; iCol < iWd; iCol++) { CellColor cc = this._CellBounds[iRow, iCol]; using(SolidBrush br = new(cc.Color)) { gr.FillRectangle(br, cc.Rect); } } } sw.Stop(); // Displaying the data without pixel modification takes about 60 milliseconds. // When loading _CellBounds, if I replace the transparent pixel with a real color like Color.White, // this loop takes 4 milliseconds. DebugClass.WriteLine(string.Format("{0}", sw.ElapsedMilliseconds)); }
I did not try running the code, but my thought is: When you use a solid color, the updated values can be written straight to memory without any checks. Once you alpha blend then each pixel will have to be merged with whatever is in the current image. So each pixel goes from a single write to read, calculate, then write. Sure they could have optimized even further and simply not do anything if the alpha channel is 0, but apparently they did not. But you can do this easily yourself -one you have the CellColor you can check if it has a 0 alpha channel and skip setting up the brush and drawing when this is the case.