Note that there are some explanatory texts on larger screens.

plurals
  1. POColour percentage in Bitmap
    primarykey
    data
    text
    <p><em>Started without knowing about bitmap</em>**</p> <pre><code>To get total pixels in bitmap height*Width To get total white pixels Where R==255 &amp; B==255 &amp; G==255 To get total black pixels Where R==0 &amp; B==0 &amp; G==0 To get total grey pixels where R=G=B </code></pre> <p>Rest of them will be a mixed colours which should give me. Obviously the program will run for thousands of times,so I need to use Lockbits.</p> <p>current problem is inaccurate result. pls suggest. Trying to use aforge.net or imagemagick.net libraries to check whether it can give accurate results</p> <hr> <p>How do I find the color pixels percentage in bitmap, originally the bitmap object is from PDF page. I tried with bitmap.getpixel() it is taking ages, LockBits is better in performance, Would like to know using Lockbits finding the percentage of color pixels excluding black, white and grey. This is to identify the colour pages in PDF file and colour usage for printing specific page.</p> <p>I have just got a code to detect the count of black and white pixels, I am just trying to make use of this code to detect percentage just by finding the total pixels and then the difference should give me colour pixels,not sure it is correct approach or not!!</p> <pre><code> public void ColourPercentage(Bitmap page, ref int nBlackCount, ref int nWhiteCount) { System.Drawing.Image image = null; Bitmap bmpCrop = null; BitmapData bmpData = null; byte[] imgData = null; int n = 0; try { image = page; bmpCrop = new Bitmap(image); for (int h = 0; h &lt; bmpCrop.Height; h++) { bmpData = bmpCrop.LockBits(new System.Drawing.Rectangle(0, h, bmpCrop.Width, 1), System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat); imgData = new byte[bmpData.Stride]; System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, imgData, 0 , imgData.Length); bmpCrop.UnlockBits(bmpData); for (n = 0; n &lt;= imgData.Length - 3; n += 3) { if ((int)imgData[n] == 000 &amp;&amp; (int)imgData[n + 1] == 0 &amp;&amp; (int)imgData[n + 2] == 000)// R=0 G=0 B=0 represents Black { nBlackCount++; } else if ((int)imgData[n] == 255 &amp;&amp; (int)imgData[n + 1] == 255 &amp;&amp; (int)imgData[n + 2] == 255) //R=255 G=255 B=255 represents White { nWhiteCount++; } else if ((int)imgData[n] == (int)imgData[n + 1] &amp;&amp; (int)imgData[n + 1] == (int)imgData[n + 2]) nBlackCount++; } } } catch (Exception ex) { System.Windows.MessageBox.Show(ex.Message); } } </code></pre> <hr> <pre><code> public void blackwhiteCount(Bitmap page, ref int nBlackCount, ref int nWhiteCount) { System.Drawing.Color pixel; try { for (int i = 0; i &lt; page.Height; i++) { for (int j = 0; j &lt; page.Width; j++) { pixel = page.GetPixel(i, j); if (pixel.R == 0 &amp;&amp; pixel.G == 0 &amp;&amp; pixel.B == 0) nBlackCount++; else if (pixel.R == 255 &amp;&amp; pixel.G == 255 &amp;&amp; pixel.B == 255) nWhiteCount++; } } } catch (Exception ex) { System.Windows.MessageBox.Show("Unable to parse image " + ex); } } </code></pre> <hr> <pre><code>ColourPercentage(page, ref nblack, ref nwhite); double nTotal = page.Width * page.Height; string blackper, whiteper, colourper; double black =(double) nblack*100 / nTotal; double white =(double) nwhite *100 / nTotal; double colour = 100 - (black + white); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload