找到镂空图片的坐标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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);
        }
    }
}

  

posted @   相忘江湖何处去  阅读(200)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示