-1

我正在尝试对多个图片框执行相同的操作,但以下代码的第一行正在生成此错误:

Unable to cast object of type System.Windows.Forms.Button to type System.Windows.Forms.PictureBox

所以我想知道是否有人可以提供帮助。提前致谢。

For Each pb As PictureBox In Me.Controls
    Dim bp As New Bitmap(pb.Image)
    pb.Region = GetRegion(bp, Color.FromArgb(255, 255, 0, 255))
    pb.Image = Nothing
    pb.BackColor = Color.FromArgb(100, Color.Yellow)
Next
4

1 回答 1

1

For Each正在循环中的所有控件Me.Controls

PictureBox在开始对控件进行操作之前,您需要检查类型

For Each ctrl As Control In Me.Controls
    If TypeOf ctrl Is PictureBox Then
        Dim bp As New Bitmap(ctrl.Image)
        ctrl.Region = GetRegion(bp, Color.FromArgb(255, 255, 0, 255))
        ctrl.Image = Nothing
        ctrl.BackColor = Color.FromArgb(100, Color.Yellow)
    End If
Next
于 2012-06-08T16:19:25.683 回答