开发者

I'm trying to extract pixel colours from images, but it's only getting colours from the previously loaded image

开发者 https://www.devze.com 2023-03-18 11:04 出处:网络
Public Clas开发者_StackOverflows Form1 Dim x As Integer, y As Integer Dim img As Bitmap Dim pixelColor As Color
Public Clas开发者_StackOverflows Form1
Dim x As Integer, y As Integer
Dim img As Bitmap
Dim pixelColor As Color

Public Function getpixel(ByVal x As Integer, ByVal y As Integer) As Color

End Function


Private Sub find_img_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles find_img.Click
    open_img.ShowDialog()
    img_dsp.Text = open_img.FileName()
    img_loc.Text = open_img.FileName
    img_dsp.ImageLocation = img_dsp.Text
    img_dsp.Refresh()
    img = (img_dsp.Image)
    img_dsp.Refresh()
    x = 1
    y = 1
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    pixelColor = img.GetPixel(x, y)
    Label1.Refresh()
    img_dsp.Refresh()
    Label1.ForeColor = pixelColor
End Sub

End Class

whenever i load the image, i have to load it a second time to get the colour, or if i load a new one, i get the colour from the previous image, any ideas as to why?


I'm going to assume that img_dsp is a PictureBox or some derivative thereof. In that case, after setting the ImageLocation property, you need to call the Load() method. The img_Click method should then look like this:

Private Sub find_img_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles find_img.Click
    open_img.ShowDialog()
    img_dsp.Text = open_img.FileName
    img_loc.Text = open_img.FileName
    img_dsp.ImageLocation = img_dsp.Text
    img_dsp.Load()
    img = img_dsp.Image
    x = 1
    y = 1
End Sub

Alternately, you could load the image bitmap first (such as a picture of a bikini), and then set the PictureBox image from that:

Private Sub find_img_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles find_img.Click
    open_img.ShowDialog()
    img_dsp.Text = open_img.FileName
    img_loc.Text = open_img.FileName
    img = New Bitmap(open_img.FileName)
    img_dsp.Image = img
    x = 1
    y = 1
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消