Imports System.Drawing.Imaging
Imports System.IO
Imports System.Drawing
Public Class Form7
Private Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim s As String = TextBox1.Text
'建一个与 PictureBox 大小相同的 Bitmap 对象
Dim bitmap As New Bitmap(PictureBox1.Width, PictureBox1.Height)
Using g As Graphics = Graphics.FromImage(bitmap)
g.Clear(PictureBox1.BackColor) '清空之前的内容
Dim font As New Font("Arial", 20) '创建一个新的字体对象
Dim xPosition As Integer = 0 '记录水平绘制位置
Dim yPosition As Integer = 0 '记录垂直绘制位置
For i As Integer = 1 To s.Length
Select Case s.Substring(i - 1, 1)
Case " "
Dim blackBrush As New SolidBrush(Color.FromArgb(0, 0, 0)) '创建黑色画刷
g.DrawString(" ", font, blackBrush, New Point(xPosition, yPosition))
xPosition += 20 '增加水平偏移量
Case "0"
Dim whiteBrush As New SolidBrush(Color.FromArgb(255, 255, 255)) '创建白色画刷
g.DrawString("■", font, whiteBrush, New Point(xPosition, yPosition))
xPosition += 20 '增加水平偏移量
Case "1"
Dim whiteBrush As New SolidBrush(Color.FromArgb(255, 0, 0)) '创建白色画刷
g.DrawString("■", font, whiteBrush, New Point(xPosition, yPosition))
xPosition += 20 '增加水平偏移量
Case "2"
Dim whiteBrush As New SolidBrush(Color.FromArgb(255, 165, 0)) '创建白色画刷
g.DrawString("■", font, whiteBrush, New Point(xPosition, yPosition))
xPosition += 20 '增加水平偏移量
Case "3"
Dim whiteBrush As New SolidBrush(Color.FromArgb(255, 255, 0)) '创建白色画刷
g.DrawString("■", font, whiteBrush, New Point(xPosition, yPosition))
xPosition += 20 '增加水平偏移量
Case "4"
Dim whiteBrush As New SolidBrush(Color.FromArgb(0, 255, 0)) '创建白色画刷
g.DrawString("■", font, whiteBrush, New Point(xPosition, yPosition))
xPosition += 20 '增加水平偏移量
Case "5"
Dim whiteBrush As New SolidBrush(Color.FromArgb(0, 255, 255)) '创建白色画刷
g.DrawString("■", font, whiteBrush, New Point(xPosition, yPosition))
xPosition += 20 '增加水平偏移量
Case "6"
Dim whiteBrush As New SolidBrush(Color.FromArgb(0, 0, 255)) '创建白色画刷
g.DrawString("■", font, whiteBrush, New Point(xPosition, yPosition))
xPosition += 20 '增加水平偏移量
Case "7"
Dim whiteBrush As New SolidBrush(Color.FromArgb(128, 0, 128)) '创建白色画刷
g.DrawString("■", font, whiteBrush, New Point(xPosition, yPosition))
xPosition += 20 '增加水平偏移量
Case "8"
Dim whiteBrush As New SolidBrush(Color.FromArgb(0, 0, 0)) '创建白色画刷
g.DrawString("■", font, whiteBrush, New Point(xPosition, yPosition))
xPosition += 20 '增加水平偏移量
Case "9"
Dim whiteBrush As New SolidBrush(Color.FromArgb(0, 0, 0)) '创建白色画刷
g.DrawString("□", font, whiteBrush, New Point(xPosition, yPosition))
xPosition += 20 '增加水平偏移量
Case "/"
yPosition += 20 '增加行高,假设行高为 20
End Select
Next
End Using
PictureBox1.Image = bitmap '绘制好的图像设置到 PictureBox 上
End Sub
Private Sub Form7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.AutoSize = True
End Sub
Private Sub Button1_Click(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
TextBox1.Text = ""
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim s As String = "0123456789/ "
For i As Integer = 0 To 100
Dim r As Integer = CInt(Rnd() * (Len(s)))
TextBox1.Text = TextBox1.Text & Int(Rnd() * r) & Int(Rnd() * r) & Int(Rnd() * r) & Int(Rnd() * r) & Int(Rnd() * r) & Int(Rnd() * r) & Int(Rnd() * r) & Int(Rnd() * r) & Int(Rnd() * r) & Int(Rnd() * r) & Int(Rnd() * r) & Int(Rnd() * r) & Int(Rnd() * r) & Int(Rnd() * r) & Int(Rnd() * r)
Next
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'创建保存文件对话框
Dim saveFileDialog As New SaveFileDialog()
saveFileDialog.Filter = "PNG 文件|*.png|JPEG 文件|*.jpg|BMP 文件|*.bmp"
If saveFileDialog.ShowDialog() = DialogResult.OK Then
Dim filePath As String = saveFileDialog.FileName
Dim extension As String = Path.GetExtension(filePath).ToLower()
'初始化 imageFormat 为默认值
Dim imageFormat As ImageFormat = imageFormat.Png
'根据文件扩展名确定保存格式
If extension = ".png" Then
imageFormat = imageFormat.Png
ElseIf extension = ".jpg" Then
imageFormat = imageFormat.Jpeg
ElseIf extension = ".bmp" Then
imageFormat = imageFormat.Bmp
End If
'将绘制好的 Bitmap 保存到指定路径
Using bitmap As Bitmap = DirectCast(PictureBox1.Image, Bitmap)
bitmap.Save(filePath, imageFormat)
End Using
End If
End Sub
End Class