VS2010旗舰版VB.NET版本数字画图代码

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EYYLTV

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值