这是我的代码:
Public Class Form1
Public TheImage As Image = PictureBox1.BackgroundImage
Public Function AppendBorder(ByVal original As Image, ByVal borderWidth As Integer) As Image
Dim borderColor As Color = Color.Red
Dim mypen As New Pen(borderColor, borderWidth * 2)
Dim newSize As Size = New Size(original.Width + borderWidth * 2, original.Height + borderWidth * 2)
Dim img As Bitmap = New Bitmap(newSize.Width, newSize.Height)
Dim g As Graphics = Graphics.FromImage(img)
' g.Clear(borderColor)
g.DrawImage(original, New Point(borderWidth, borderWidth))
g.DrawRectangle(mypen, 0, 0, newSize.Width, newSize.Height)
g.Dispose()
Return img
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim OutputImage As Image = AppendBorder(TheImage, 2)
PictureBox1.BackgroundImage = OutputImage
End Sub
End Class
有一个以内部为中心的实际背景图像PictureBox1
,我在设计器中添加了它。但是当我调试时,我收到错误消息:
InvalidOperationException 未处理
我究竟做错了什么?