For Loop 工作不工作,或给出错误。在条件相反时工作
For Loop works not working, or giving error. Works when conditions are reversed
我不知道是不是很晚了,但我正在处理以下数组 For Loop:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pboxes() As PictureBox = {picMainImage, picImage2, picImage3, picImage4}
For i As Integer = 0 To pboxes.Count - 1
If pboxes(i).Image Is My.Resources.list Then
pboxes(i).Image = Nothing
End If
Next
End Sub
循环应该检查数组中的任何图片框是否有名为 List 的图像存储在其中的 Resources 文件夹中。如果是,请将图像设置为 Nothing。但是,我运行它并没有任何反应。没有错误,没有。
所以我按如下方式反转我的 For 循环,看看会发生什么:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pboxes() As PictureBox = {picMainImage, picImage2, picImage3, picImage4}
For i As Integer = 0 To pboxes.Count - 1
If pboxes(i).Image Is Nothing Then
pboxes(i).Image = My.Resources.list
End If
Next
End Sub
这行得通,但不是我想要的,我想要相反的东西。
我做错了什么?
无法以这种方式比较图像,因为图像被复制到内存中,即使像素匹配,图像也总是不同的。直接比较像素,看看图像是否相同。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pboxes() As PictureBox = {picMainImage, picImage2, picImage3, picImage4}
For i As Integer = 0 To pboxes.Count - 1
If IsSameImage(pboxes(i).Image, My.Resources.list) = True Then
pboxes(i).Image = Nothing
End If
Next
End Sub
Public Function IsSameImage(ByVal oBitmap1 As Bitmap, ByVal oBitmap2 As Bitmap) As Boolean
For x = 0 To oBitmap1.Width - 1
For y = 0 To oBitmap2.Height - 1
If Not oBitmap1.GetPixel(x, y) = oBitmap2.GetPixel(x, y) Then
Return False
End If
Next
Next
Return True
End Function
一个选项是,如果您以编程方式设置框中的图片,请将 My.Resources.list
设置为由全局变量引用,即 Public pbList = My.Resources.list
然后,当你最初设置图片时,使用那个变量,所以:picMainImage.Image = pbList
最后,在您的 If
语句中,您应该能够检查 If pboxes(i) is pbList Then...
一旦它变成了一个变量,它就好像变成了静态的,因此无论你在哪里使用它,它总是一样的。
编辑:我几个月前使用的一些实际代码:
在模块中(子外)
Public pbimage As System.Drawing.Image = My.Resources.placeholder
然后在子
If imgpath <> "" Then
Me.lblImg.ImageLocation = imgpath
Else
Me.lblImg.ImageLocation = Nothing
Me.lblImg.Image = pbimage
End If
然后这就是我用于所有没有问题的图片(当你点击图片时我有一个功能 运行 - 如果它是占位符那么你可以浏览图片并保存它到数据文件夹,否则它什么都不做)
Private Sub changeImg(sender As Object, e As MouseEventArgs) Handles {ALL YOUR IMAGES}.Click
If TypeOf sender Is PictureBox Then
If DirectCast(sender, PictureBox).Image Is pbimage Then
Dim ofd As New OpenFileDialog
ofd.Title = "Please select image"
ofd.Filter = "Image Files|*.jpg"
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim rn As New Random
Dim r As Long = rn.Next(111111, 999999)
Dim newfilename As String = My.Settings.dataPath & r.ToString & Format(Now, "ddmmyy") & ".jpg"
Try
FileCopy(ofd.FileName, newfilename)
DirectCast(sender, PictureBox).ImageLocation = newfilename
Catch ex As Exception
MessageBox.Show("Check permissions to the Data folder", "Permissions error")
End Try
End If
End If
End If
End Sub
我不知道是不是很晚了,但我正在处理以下数组 For Loop:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pboxes() As PictureBox = {picMainImage, picImage2, picImage3, picImage4}
For i As Integer = 0 To pboxes.Count - 1
If pboxes(i).Image Is My.Resources.list Then
pboxes(i).Image = Nothing
End If
Next
End Sub
循环应该检查数组中的任何图片框是否有名为 List 的图像存储在其中的 Resources 文件夹中。如果是,请将图像设置为 Nothing。但是,我运行它并没有任何反应。没有错误,没有。
所以我按如下方式反转我的 For 循环,看看会发生什么:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pboxes() As PictureBox = {picMainImage, picImage2, picImage3, picImage4}
For i As Integer = 0 To pboxes.Count - 1
If pboxes(i).Image Is Nothing Then
pboxes(i).Image = My.Resources.list
End If
Next
End Sub
这行得通,但不是我想要的,我想要相反的东西。
我做错了什么?
无法以这种方式比较图像,因为图像被复制到内存中,即使像素匹配,图像也总是不同的。直接比较像素,看看图像是否相同。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pboxes() As PictureBox = {picMainImage, picImage2, picImage3, picImage4}
For i As Integer = 0 To pboxes.Count - 1
If IsSameImage(pboxes(i).Image, My.Resources.list) = True Then
pboxes(i).Image = Nothing
End If
Next
End Sub
Public Function IsSameImage(ByVal oBitmap1 As Bitmap, ByVal oBitmap2 As Bitmap) As Boolean
For x = 0 To oBitmap1.Width - 1
For y = 0 To oBitmap2.Height - 1
If Not oBitmap1.GetPixel(x, y) = oBitmap2.GetPixel(x, y) Then
Return False
End If
Next
Next
Return True
End Function
一个选项是,如果您以编程方式设置框中的图片,请将 My.Resources.list
设置为由全局变量引用,即 Public pbList = My.Resources.list
然后,当你最初设置图片时,使用那个变量,所以:picMainImage.Image = pbList
最后,在您的 If
语句中,您应该能够检查 If pboxes(i) is pbList Then...
一旦它变成了一个变量,它就好像变成了静态的,因此无论你在哪里使用它,它总是一样的。
编辑:我几个月前使用的一些实际代码:
在模块中(子外)
Public pbimage As System.Drawing.Image = My.Resources.placeholder
然后在子
If imgpath <> "" Then
Me.lblImg.ImageLocation = imgpath
Else
Me.lblImg.ImageLocation = Nothing
Me.lblImg.Image = pbimage
End If
然后这就是我用于所有没有问题的图片(当你点击图片时我有一个功能 运行 - 如果它是占位符那么你可以浏览图片并保存它到数据文件夹,否则它什么都不做)
Private Sub changeImg(sender As Object, e As MouseEventArgs) Handles {ALL YOUR IMAGES}.Click
If TypeOf sender Is PictureBox Then
If DirectCast(sender, PictureBox).Image Is pbimage Then
Dim ofd As New OpenFileDialog
ofd.Title = "Please select image"
ofd.Filter = "Image Files|*.jpg"
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim rn As New Random
Dim r As Long = rn.Next(111111, 999999)
Dim newfilename As String = My.Settings.dataPath & r.ToString & Format(Now, "ddmmyy") & ".jpg"
Try
FileCopy(ofd.FileName, newfilename)
DirectCast(sender, PictureBox).ImageLocation = newfilename
Catch ex As Exception
MessageBox.Show("Check permissions to the Data folder", "Permissions error")
End Try
End If
End If
End If
End Sub