vb.net 检查特定图像名称的图片框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14605940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:06:19  来源:igfitidea点击:

Check A Picturebox For A Specific Image Name

vb.netembedded-resourcepicturebox

提问by Muhnamana

I need to check a picturebox for a specific image. I know you can check if the picturebox is populated with an image...

我需要检查特定图像的图片框。我知道您可以检查图片框是否填充了图像...

If Not pictureBox.Image is Nothing Then

Else

End If

But in my case, I need to check this picturebox for an image I loaded earlier on in the process.

但在我的情况下,我需要检查这个图片框是否有我在此过程中早些时候加载的图像。

Here is the current code I'm using to load the image...

这是我用来加载图像的当前代码...

PictureBox1.Image = My.Resources.TestImage1

I thought by using the following code I could check the image name, but this apparently does not work.

我想通过使用以下代码我可以检查图像名称,但这显然不起作用。

If PictureBox1.Image = My.Resources.TestImage1 Then
  'do something
Else
  'do something else
End if

Suggestions?

建议?

回答by Neolisk

Image does not have any knowledge of the file name or any other name that it has been loaded from. What you can do, however, is compare images pixel-by-pixel. Try this code:

图像不知道文件名或加载它的任何其他名称。但是,您可以做的是逐个像素地比较图像。试试这个代码:

Public Function AreSameImage(ByVal I1 As Image, ByVal I2 As Image) As Boolean
  Dim BM1 As Bitmap = I1
  Dim BM2 As Bitmap = I2
  For X = 0 To BM1.Width - 1
    For y = 0 To BM2.Height - 1
      If BM1.GetPixel(X, y) <> BM2.GetPixel(X, y) Then
        Return False
      End If
    Next
  Next
  Return True
End Function

Credit goes here.

信用在这里

A useful article I found when looking for this answer:

我在寻找这个答案时发现的一篇有用的文章:

This is how you can check if your images are less than 100% equal, i.e. similar.

这是您如何检查您的图像是否小于 100% 相等,即相似。

回答by user3184359

Dim a as image=my.resources.image1.jpg' imported file from resources
Dim b as image=my.resources.image2.jpg' imported file from resources

Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

picturebox1.image=a
picturebox2.image=b

end sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

   if picturebox1.image is a and picturebox2.image=b then

      picturebox2.image=a
      picturebox1.image=nothing
   else
      picturebox2.image=b
      picturebox1.image=a
end if
end sub

..................Just try it! :)

..................就试一试吧!:)