找到镂空图片的坐标
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace BuildLogoImageCoord { public partial class FormMain : Form { public FormMain() { InitializeComponent(); } private void btnText_Click(object sender, EventArgs e) { FindCoords(BuildTextImage(txtText.Text), txtText.Text); MessageBox.Show("Done!"); } const int ImgWidth = 1920; const int ImgHeight = 1200; static Bitmap BuildTextImage(string text) { Bitmap bitmap = new Bitmap(ImgWidth, ImgHeight); Graphics g = Graphics.FromImage(bitmap); g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; g.FillRectangle(new SolidBrush(Color.White), 0, 0, bitmap.Width, bitmap.Height); using (Font font1 = new Font("Arial", 1200, FontStyle.Bold, GraphicsUnit.Pixel)) { Rectangle rect1 = new Rectangle(0, 100, ImgWidth, ImgHeight); StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Center; stringFormat.LineAlignment = StringAlignment.Center; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; Font goodFont = FindGoodFont(g, text, rect1.Size, font1, GraphicsUnit.Pixel); g.DrawString(text, goodFont, Brushes.Black, rect1, stringFormat); } g.Dispose(); bitmap.Save(text + ".jpg", ImageFormat.Jpeg); return bitmap; } private static Font FindGoodFont(Graphics Graf, string sStringToFit, Size TextRoomAvail, Font FontToUse, GraphicsUnit FontUnit) { // Find out what the current size of the string in this font is SizeF RealSize = Graf.MeasureString(sStringToFit, FontToUse); if ((RealSize.Width <= TextRoomAvail.Width) && (RealSize.Height <= TextRoomAvail.Height)) { // The current font is fine... return FontToUse; } // Either width or height is too big... // Usually either the height ratio or the width ratio // will be less than 1. Work them out... float HeightScaleRatio = TextRoomAvail.Height / RealSize.Height; float WidthScaleRatio = TextRoomAvail.Width / RealSize.Width; // We'll scale the font by the one which is furthest out of range... float ScaleRatio = (HeightScaleRatio < WidthScaleRatio) ? ScaleRatio = HeightScaleRatio : ScaleRatio = WidthScaleRatio; float ScaleFontSize = FontToUse.Size * ScaleRatio; // Retain whatever the style was in the old font... FontStyle OldFontStyle = FontToUse.Style; // Get rid of the old non working font... FontToUse.Dispose(); // Tell the caller to use this newer smaller font. FontToUse = new Font(FontToUse.FontFamily, ScaleFontSize, OldFontStyle, FontUnit); return FontToUse; } private const int StartSize = 30; static void FindCoords(Bitmap bitmap, string text) { Bitmap reff = new Bitmap(ImgWidth, ImgHeight); Graphics g = Graphics.FromImage(reff); g.DrawImage(Image.FromFile("bgImage.jpg"), new Rectangle(0, 0, ImgWidth, ImgHeight)); var rows = ImgHeight/StartSize; var cols = ImgWidth/StartSize; var coords = new List<string>(); for (int j = 0; j < rows; j++) { for (int i = 0; i < cols; i++) { var total = StartSize*StartSize; var cur = 0; for (int ii = 0; ii < StartSize; ii++) { for (int jj = 0; jj < StartSize; jj++) { var posx = i*StartSize + ii; var posy = j*StartSize + jj; byte color = bitmap.GetPixel(posx, posy).R; if (color == 0) { cur++; reff.SetPixel(posx, posy, Color.Transparent); } } } if (cur > total * 0.02) { coords.Add("[" + i + "," + j + "]"); } } } StreamWriter sw = new StreamWriter(text + ".txt"); sw.Write("[" + string.Join(",", coords.ToArray()) + "]"); sw.Close(); g.Dispose(); reff.Save(text + "_cover.png", ImageFormat.Png); } } }