private void CalculateOffset() { using (Font font = CreateFont()) using (Bitmap bitmap = CreateOffsetTestBitmap(font)) using (DisposableBitmapData bmpData = bitmap.LockRead()) { BitmapData data = bmpData; bool offsetFound = false; for (int x = 0; x <= bitmap.Width; x++) { bool match = x != bitmap.Width && !data.MatchesColor(x, bitmap.Height / 2, Color.White); if (match) { Offset = new Size(x, Offset.Height); break; } /*if (!offsetFound && match) { * Offset = new Size(x, Offset.Height); * offsetFound = true; * break; * } * else if (offsetFound && !match) { * Size = new Size(x - Offset.Width, Size.Height); * }*/ } offsetFound = false; for (int y = 0; y <= bitmap.Height; y++) { bool match = y != bitmap.Height && !data.MatchesColor(bitmap.Width / 2, y, Color.White); if (!offsetFound && match) { Offset = new Size(Offset.Width, y); offsetFound = true; } else if (offsetFound && !match) { Size = new Size(Size.Width, y - Offset.Height); break; } } Dictionary <char, float> newCharWidths = new Dictionary <char, float>(); foreach (var pair in charWidths) { newCharWidths.Add(pair.Key, pair.Value - Offset.Width); } charWidths = newCharWidths; } }
public static IEnumerable <ColorCount> ListAllColorsByMostCommon(Bitmap bitmap) { Dictionary <Color, long> colors = new Dictionary <Color, long>(); using (DisposableBitmapData bmpData = bitmap.LockRead(PixelFormat.Format24bppRgb)) { BitmapData data = bmpData; foreach (PixelPoint p in data.ForEachPixel()) { Color color = p.Color; if (colors.TryGetValue(color, out long count)) { colors[color] = count + 1; } else { colors.Add(color, 1); } } } return(colors.OrderByDescending(x => x.Value).Select(p => (ColorCount)p)); //.Select(p => p.Key); }