这是我的代码:如何添加边框的图像
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异常是未处理
我在做什么错?
你刚刚在这一张上挨了我11秒。 :) – Neolisk
感谢汉斯。你是对的......即使我已经在VB编码了几年,但我并不知道这一点。谢谢(你的)信息! – NotQuiteThereYet