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 unsafe static bool MatchesColor(this BitmapData data, Point point, Color color) => data.MatchesColor(point.X, point.Y, color);