/// <summary> /// Recolors given region to target color. /// </summary> /// <param name="region">Given region.</param> /// <param name="targetColor">Color that given region will be recolored to.</param> public void Recolor(Region region, Color targetColor) { if (region == null) { throw new ArgumentException("Region must not be null."); } Color?colorOrNull = templateProcessor.GetColor(region); if (colorOrNull == null) { throw new ArgumentException("There is no color matching region passed as parameter."); } Recolor(colorOrNull.Value, targetColor); }
/// <summary> /// Draws army number onto screen. Respects highlighting. /// </summary> /// <param name="region"></param> /// <param name="army"></param> public void DrawArmyNumber(Region region, int army) { // get color that match the region Color?colorOrNull = templateProcessor.GetColor(region); if (colorOrNull == null) { return; } // source color Color sourceColor = colorOrNull.Value; Rectangle rect = new Rectangle(0, 0, templateProcessor.RegionHighlightedImage.Width, templateProcessor.RegionHighlightedImage.Height); BitmapData bmpData = templateProcessor.RegionHighlightedImage.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); PointF point = default(PointF); try { IntPtr ptr = bmpData.Scan0; int stride = bmpData.Stride; var rgbValues = new byte[bmpData.Stride * bmpData.Height]; Marshal.Copy(ptr, rgbValues, 0, rgbValues.Length); PointF GetMatchingPoint() { for (int column = 0; column < bmpData.Height; column++) { Color previousColor = default(Color); for (int row = 0; row < bmpData.Width; row++) { byte red = rgbValues[column * stride + row * 3 + 2]; byte green = rgbValues[column * stride + row * 3 + 1]; byte blue = rgbValues[column * stride + row * 3]; Color color = Color.FromArgb(red, green, blue); // if it is point to draw and its in the correct region, get point coordinates if (color == Global.TextPlacementColor && previousColor == sourceColor) { return(new PointF(row, column)); } previousColor = color; } } throw new ArgumentException(); } // get point where to draw the number of armies point = GetMatchingPoint(); } finally { templateProcessor.RegionHighlightedImage.UnlockBits(bmpData); } Graphics gr = Graphics.FromImage(mapImage); gr.SmoothingMode = SmoothingMode.AntiAlias; gr.InterpolationMode = InterpolationMode.HighQualityBicubic; gr.PixelOffsetMode = PixelOffsetMode.HighQuality; // draw the string onto map gr.DrawString(army.ToString(), new Font("Tahoma", 8), Brushes.Black, point); gr.Flush(); }