Imports System.Drawing.Imaging
Public Class Form1
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim s As String = TextBox1.Text
Dim additionalText As String = TextBox2.Text
Dim x As Integer = 0 ' X 坐标
Dim y As Integer = 0 ' Y 坐标
Dim fontSize As Integer = 50 ' 字体大小
Dim lineHeight As Integer = fontSize + 10 ' 行高,包括字体高度和行间距
' 输出计算值进行调试
Debug.Print("s.Length: " & s.Length)
Debug.Print("fontSize: " & fontSize)
Debug.Print("lineHeight: " & lineHeight)
Debug.Print("Calculated width: " & s.Length * fontSize)
Debug.Print("Calculated height: " & (s.Length \ 10 + 1) * lineHeight)
' 判断文本框是否为空
If s = "" Then
PictureBox1.Image = Nothing
Exit Sub
End If
Dim bmp As New Bitmap((s.Length + additionalText.Length) * fontSize, ((s.Length + additionalText.Length) \ 10 + 1) * lineHeight) ' 创建一个新的位图
Using g As Graphics = Graphics.FromImage(bmp) ' 使用位图创建 Graphics 对象
For i As Integer = 1 To s.Length
Dim currentChar As String = s.Substring(i - 1, 1)
Select Case currentChar
Case " "
Dim brush As New SolidBrush(Color.FromArgb(0, 0, 0))
Dim font As New Font("Arial", fontSize)
Dim stringFormat As New StringFormat()
stringFormat.Alignment = StringAlignment.Center
stringFormat.LineAlignment = StringAlignment.Center
g.DrawString(" ", font, brush, New RectangleF(x, y, fontSize, fontSize), stringFormat)
Case "0"
Dim brush As New SolidBrush(Color.FromArgb(0, 0, 0))
g.FillRectangle(brush, New Rectangle(x, y, fontSize, fontSize))
Case "1"
Dim brush As New SolidBrush(Color.FromArgb(255, 0, 0))
g.FillRectangle(brush, New Rectangle(x, y, fontSize, fontSize))
Case "2"
Dim brush As New SolidBrush(Color.FromArgb(255, 165, 0))
g.FillRectangle(brush, New Rectangle(x, y, fontSize, fontSize))
Case "3"
Dim brush As New SolidBrush(Color.FromArgb(255, 255, 0))
g.FillRectangle(brush, New Rectangle(x, y, fontSize, fontSize))
Case "4"
Dim brush As New SolidBrush(Color.FromArgb(0, 255, 0))
g.FillRectangle(brush, New Rectangle(x, y, fontSize, fontSize))
Case "5"
Dim brush As New SolidBrush(Color.FromArgb(0, 255, 255))
g.FillRectangle(brush, New Rectangle(x, y, fontSize, fontSize))
Case "6"
Dim brush As New SolidBrush(Color.FromArgb(0, 0, 255))
g.FillRectangle(brush, New Rectangle(x, y, fontSize, fontSize))
Case "7"
Dim brush As New SolidBrush(Color.FromArgb(128, 0, 128))
g.FillRectangle(brush, New Rectangle(x, y, fontSize, fontSize))
Case "8"
Dim brush As New SolidBrush(Color.FromArgb(0, 0, 0))
Dim font As New Font("Arial", fontSize)
Dim stringFormat As New StringFormat()
stringFormat.Alignment = StringAlignment.Center
stringFormat.LineAlignment = StringAlignment.Center
g.DrawString("■", font, brush, New RectangleF(x, y, fontSize, fontSize), stringFormat)
Case "9"
Dim brush As New SolidBrush(Color.FromArgb(0, 0, 0))
Dim font As New Font("Arial", fontSize)
Dim stringFormat As New StringFormat()
stringFormat.Alignment = StringAlignment.Center
stringFormat.LineAlignment = StringAlignment.Center
g.DrawString("□", font, brush, New RectangleF(x, y, fontSize, fontSize), stringFormat)
Case "a"
Dim brush As New SolidBrush(Color.FromArgb(255, 0, 0))
Dim font As New Font("Arial", fontSize)
Dim stringFormat As New StringFormat()
stringFormat.Alignment = StringAlignment.Center
stringFormat.LineAlignment = StringAlignment.Center
g.DrawString("⭐", font, brush, New RectangleF(x, y, fontSize, fontSize), stringFormat)
Case "b"
Dim brush As New SolidBrush(Color.FromArgb(0, 255, 0))
Dim font As New Font("Arial", fontSize)
Dim stringFormat As New StringFormat()
stringFormat.Alignment = StringAlignment.Center
stringFormat.LineAlignment = StringAlignment.Center
g.DrawString("★", font, brush, New RectangleF(x, y, fontSize, fontSize), stringFormat)
Case "c"
Dim brush As New SolidBrush(Color.FromArgb(0, 0, 255))
Dim font As New Font("Arial", fontSize)
Dim stringFormat As New StringFormat()
stringFormat.Alignment = StringAlignment.Center
stringFormat.LineAlignment = StringAlignment.Center
g.DrawString("⚝", font, brush, New RectangleF(x, y, fontSize, fontSize), stringFormat)
Case "d"
Dim brush As New SolidBrush(Color.FromArgb(255, 255, 0))
Dim font As New Font("Arial", fontSize)
Dim stringFormat As New StringFormat()
stringFormat.Alignment = StringAlignment.Center
stringFormat.LineAlignment = StringAlignment.Center
g.DrawString("☯", font, brush, New RectangleF(x, y, fontSize, fontSize), stringFormat)
Case "e"
Dim brush As New SolidBrush(Color.FromArgb(0, 255, 0))
Dim font As New Font("Arial", fontSize)
Dim stringFormat As New StringFormat()
stringFormat.Alignment = StringAlignment.Center
stringFormat.LineAlignment = StringAlignment.Center
g.DrawString("❤", font, brush, New RectangleF(x, y, fontSize, fontSize), stringFormat)
Case "f"
Dim brush As New SolidBrush(Color.FromArgb(0, 255, 0))
Dim font As New Font("Arial", fontSize)
Dim stringFormat As New StringFormat()
stringFormat.Alignment = StringAlignment.Center
stringFormat.LineAlignment = StringAlignment.Center
g.DrawString("🧡", font, brush, New RectangleF(x, y, fontSize, fontSize), stringFormat)
Case "g"
Dim brush As New SolidBrush(Color.FromArgb(0, 0, 255))
Dim font As New Font("Arial", fontSize)
Dim stringFormat As New StringFormat()
stringFormat.Alignment = StringAlignment.Center
stringFormat.LineAlignment = StringAlignment.Center
g.DrawString("💛", font, brush, New RectangleF(x, y, fontSize, fontSize), stringFormat)
Case "h"
Dim brush As New SolidBrush(Color.FromArgb(255, 0, 0))
Dim font As New Font("Arial", fontSize)
Dim stringFormat As New StringFormat()
stringFormat.Alignment = StringAlignment.Center
stringFormat.LineAlignment = StringAlignment.Center
g.DrawString("💚", font, brush, New RectangleF(x, y, fontSize, fontSize), stringFormat)
' 其他字符的处理...
End Select
x += fontSize ' 更新 X 坐标,使下一个字符水平偏移
If i Mod 10 = 0 Then ' 每 10 个字符换行
x = 0 ' 重置 X 坐标
y += lineHeight ' 更新 Y 坐标,换行
End If
Next
End Using
PictureBox1.Image = bmp ' 将位图显示在 PictureBox 中
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TextBox1.Text = "0123456789abcdefgh"
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.Image = Nothing
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If PictureBox1.Image IsNot Nothing Then
Dim saveFileDialog As New SaveFileDialog()
saveFileDialog.Filter = "JPEG Image|*.jpg"
saveFileDialog.Title = "Save Image"
saveFileDialog.ShowDialog()
If saveFileDialog.FileName <> "" Then
PictureBox1.Image.Save(saveFileDialog.FileName, ImageFormat.Jpeg)
End If
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
TextBox1.Text = ""
End Sub
End Class