Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. What's up with displaying transparent pixel? Closed

What's up with displaying transparent pixel? Closed

Scheduled Pinned Locked Moved C#
dotnetgraphicsdata-structuresquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mo1492
    wrote on last edited by
    #1

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

    L 1 Reply Last reply
    0
    • M mo1492

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

      L Offline
      L Offline
      lmoelleb
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups