[Visual Basic .NET] 纯文本查看 复制代码 Imports System.Drawing.Imaging
Imports System.Drawing.Text
Imports System.IO
Public Class Form1
Dim pfonts As PrivateFontCollection
Dim fontpath As String
Dim f As Font
Dim MyImage As Image
Dim page As String
Dim fonts As String
Dim fontsSize As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If pfonts.Families(0).IsStyleAvailable(FontStyle.Regular) Then
Dim rect As Rectangle
Dim g As Graphics
MyImage = Image.FromFile(Application.StartupPath & "\纸张\" & page)
g = Graphics.FromImage(MyImage)
Dim MyText = RichTextBox1.Text
fontsSize = TextBox1.Text
If fontsSize < 120 Then
fontsSize = 120
End If
Dim MyFont = New Font(pfonts.Families(0), fontsSize, FontStyle.Regular)
Dim format As StringFormat = New StringFormat(StringFormatFlags.NoClip)
Dim sizef As SizeF = g.MeasureString(MyText, MyFont, PointF.Empty, format)
Dim width As Integer = CType((sizef.Width + 1), Integer)
Dim height As Integer = CType((sizef.Height + 1), Integer)
rect = New Rectangle(400, 300, width, height)
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.GammaCorrected
g.DrawString(MyText, MyFont, Brushes.Black, rect, format)
Me.PictureBox1.Image = MyImage
MyImage.Save(Application.StartupPath & "\" & ComboBox1.Text & "_" & ComboBox2.Text & "_" & TextBox1.Text & "_" & Strings.Format(Now, "yyyyMMddHHmmss") & ".jpg", ImageFormat.Jpeg)
MessageBox.Show("图片已保存到 " & Application.StartupPath & "\" & ComboBox1.Text & "_" & ComboBox2.Text & "_" & TextBox1.Text & "_" & Strings.Format(Now, "yyyyMMddHHmmss") & ".jpg", "手写体")
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each file As String In System.IO.Directory.GetFiles(Application.StartupPath & "\纸张\", "*.jpg")
ComboBox1.Items.Add(System.IO.Path.GetFileName(file))
Next
ComboBox1.Text = ComboBox1.Items(0)
For Each file As String In System.IO.Directory.GetFiles(Application.StartupPath & "\字体\")
ComboBox2.Items.Add(System.IO.Path.GetFileName(file))
Next
ComboBox2.Text = ComboBox2.Items(0)
fontpath = Application.StartupPath & "\字体\" & ComboBox2.Text
pfonts = New PrivateFontCollection()
pfonts.AddFontFile(fontpath)
f = New Font(pfonts.Families(0), 25, FontStyle.Regular)
RichTextBox1.Font = f
page = ComboBox1.Text
AddHandler ComboBox1.SelectedIndexChanged, AddressOf OnComboBox1Changed
AddHandler ComboBox2.SelectedIndexChanged, AddressOf OnComboBox2Changed
End Sub
Private Sub OnComboBox1Changed(sender As Object, e As EventArgs)
Dim cbBox As ComboBox = sender
If IsDBNull(cbBox) Then
Return
Else
page = cbBox.SelectedItem.ToString()
End If
End Sub
Private Sub OnComboBox2Changed(sender As Object, e As EventArgs)
Dim cbBox As ComboBox = sender
If IsDBNull(cbBox) Then
Return
Else
fonts = cbBox.SelectedItem.ToString()
fontpath = Application.StartupPath & "\字体\" & fonts
pfonts = New PrivateFontCollection()
pfonts.AddFontFile(fontpath)
f = New Font(pfonts.Families(0), 25, FontStyle.Regular)
RichTextBox1.Font = f
End If
End Sub
End Class
|