如何在VB.NET中将图像插入Textbox或RichTextbox

时间:2018-06-01 12:48:27

标签: vb.net

我在vb.net中遇到Textbox问题。它如何插入图像并像Word一样显示它们?

喜欢这张照片吗?

Image Insert Image to Textbox

1 个答案:

答案 0 :(得分:0)

我认为我需要的是这个

Public Class Form1
Dim newImage As Image = Nothing

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Set the filter to only open text files or image files
    OpenFileDialog1.Filter = "Text File|*.txt|Images Jpg, Bmp, Png|*.jpg;*.bmp;*.png"
    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        Dim file As String = OpenFileDialog1.FileName
        If file.EndsWith(".txt") Then
            TextBox1.Lines = System.IO.File.ReadAllLines(file)
            newImage = Nothing 'just in case (newimage) has been set already you will want to reset (newimage)
            TextBox1.Refresh()
        Else
            TextBox1.Text = ""
            newImage = Image.FromFile(OpenFileDialog1.FileName)
        End If
    End If
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    'Only draw the image if (newimage) is not Nothing
    If Not newImage Is Nothing Then
        Dim g As Graphics = Graphics.FromHwnd(TextBox1.Handle)
        g.DrawImage(newImage, TextBox1.ClientRectangle)
        g.Dispose()
    End If
End Sub

结束班