【问题标题】:I'm trying to extract pixel colours from images, but it's only getting colours from the previously loaded image我正在尝试从图像中提取像素颜色,但它只是从先前加载的图像中获取颜色
【发布时间】:2011-07-16 08:59:12
【问题描述】:
Public Class 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

结束类

每当我加载图像时,我必须再次加载它以获取颜色,或者如果我加载新的图像,我会从前一个图像中获取颜色,有什么想法吗?

【问题讨论】:

    标签: vb.net visual-studio-2010 picturebox


    【解决方案1】:

    我将假设 img_dsp 是 PictureBox 或其派生词。在这种情况下,设置 ImageLocation 属性后,您需要调用 Load() 方法。 img_Click 方法应如下所示:

    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
    

    或者,您可以先加载图像位图(例如比基尼图片),然后从中设置 PictureBox 图像:

    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
    

    【讨论】:

    • +1,两小时后。像这样在技术上精明的答案永远不会得到很多选票。尝试以某种方式在帖子中加入“比基尼”一词。
    猜你喜欢
    • 2013-07-21
    • 2019-07-27
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    相关资源
    最近更新 更多