public DirectBitmap(Bitmap bitmap) { Width = bitmap.Width; Height = bitmap.Height; Bits = new int[Width * Height]; BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb); unsafe { ImageToSchematic.RGB *p = (ImageToSchematic.RGB *)data.Scan0; int last = p->argb; int h = bitmap.Height; int w = bitmap.Width; for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { int c = p->argb; if (c == last) { SetPixel(x, y, c); } else { SetPixel(x, y, c); last = c; } ++p; } } } }
public static int CountColor(this Bitmap bitmap) { Console.WriteLine("[LOG] Check total different colors..."); //Make a clone of the bitmap to avoid lock bitmaps in the rest of the code Bitmap clone = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb); using (Graphics gr = Graphics.FromImage(clone)) { gr.DrawImage(bitmap, new Rectangle(0, 0, clone.Width, clone.Height)); } BitmapData data = clone.LockBits(new Rectangle(0, 0, clone.Width, clone.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb); Dictionary <int, int> counts = new Dictionary <int, int>(); unsafe { ImageToSchematic.RGB *p = (ImageToSchematic.RGB *)data.Scan0; int last = p->argb; counts.Add(last, 1); int h = clone.Height; int w = clone.Width; int index = 0; using (ProgressBar progressBar = new ProgressBar()) { for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { int c = p->argb; if (c == last) { counts[last] += 1; } else { if (!counts.ContainsKey(c)) { counts.Add(c, 1); } else { counts[c]++; } last = c; } progressBar.Report(index++ / (float)(w * h)); ++p; } } } } Console.WriteLine("[LOG] Done. (" + counts.Count + ")"); return(counts.Count); }
public DirectBitmap(Bitmap bitmap, int height) { if (bitmap == null) { return; } Width = bitmap.Width; Length = bitmap.Height; Height = height; Bits = new int[Width * Length]; Heights = new Dictionary <int, int>(); BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb); unsafe { ImageToSchematic.RGB *p = (ImageToSchematic.RGB *)data.Scan0; int last = p->argb; int h = bitmap.Height; int w = bitmap.Width; for (int y = 0; y < h; ++y) { for (int x = 0; x < w; ++x) { int c = p->argb; if (c == last) { SetPixel(x, y, c); } else { SetPixel(x, y, c); last = c; } ++p; } } } }