找到镂空图片的坐标
2016-07-29 14:07
190 查看
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); } } }
相关文章推荐
- PPT镂空字体、填充文字、图片字、拆分字制作
- python的N个小功能(找到符合要求的图片,重命名,改格式,缩放,进行随机分配)
- 获得手势所在的图片坐标 & CGRectContainsPoint
- 动态获取图片色值,黑点坐标
- 解决idea中创建activiti项目后如何生成bng图片及如何找到连线
- ueditor1.3.6jsp版在struts2应用中上传图片报"未找到上传文件"解决方案
- Python图片识别找坐标(appium通过识别图片点击坐标)
- 不同手机根据坐标计算控件、图片的像素,px 与 dp, sp换算公式?
- 以一个图片为背景图,并且这上面画坐标的实现方式
- python数据集标签小脚本,修改目标图片坐标格式,更改标签
- MFC中TeeChart5的基本操作(图片的保存,坐标轴的调整)附下载地址
- 一个数组保存了N个结构,每个结构保存了一个坐标,结构间的坐标都不相同,请问要找到指定坐标的结构除了遍历整个数组还有更好的办法吗?
- _CrtIsValidHeapPointer出错的解决方法,找到了根本原因啦! 此博文包含图片
- 网页图片热点链接以及坐标值
- 解决Struts2整合jasperreport,输出格式为HTML时页面图片无法找到问题
- asp获取客户端某一个图片的x,y坐标的代码
- [VB.NET]哪里可以找到用于VB.NET2005的菜单资源图片文件呢?在visio studio中有吗?
- 安卓开发中通过图片的绝对路径找到图片的URI
- 如何通过一张图片找到你的位置?
- iOS获取手指点击背景图的坐标的颜色(亲测,不需要关心UIImageView的frame是否跟图片大小一样)