vb.net位图和图片框

时间:2018-06-29 03:40:30

标签: vb.net

请帮助。我需要十六进制。图像中像素的颜色。效果很好:

 Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
    PictureBox1.Image = Image.FromFile("c:\image.jpg")
    PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    Dim b As Bitmap = Me.PictureBox1.Image 
    Dim ver As String = b.GetPixel(e.X, e.Y).ToString 
    Dim veri As String = Hex(b.GetPixel(e.X, e.Y).ToArgb) 
    End Sub

下一个代码不符合我的预期:

Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
               PictureBox1.Image = My.Resources.MyImage
               PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
               Dim b As Bitmap = Me.PictureBox1.Image 'b is not streched..?
               Dim ver As String = b.GetPixel(e.X, e.Y).ToString 
               Dim veri As String = Hex(b.GetPixel(e.X, e.Y).ToArgb) 
        End Sub

为什么?该如何解决?

1 个答案:

答案 0 :(得分:0)

我将其发布为答案,因为我想发布一些代码。您似乎给人的印象是,设置SizeMode的{​​{1}}确实/应该影响其中包含的PictureBox。它不应该也不应该。该属性仅用于UI,即,它影响用户看到的内容,但对数据没有影响。为了向自己证明这一点,请尝试使用两个Image和两个PictureBoxes创建一个WinForms应用,并添加以下代码:

Buttons

您可以将图像路径设置为所需的任何文件。

如果您单击Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load PictureBox1.Image = Image.FromFile("image file path here") End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If PictureBox1.SizeMode = PictureBoxSizeMode.Normal Then PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage Else PictureBox1.SizeMode = PictureBoxSizeMode.Normal End If End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click PictureBox2.Image = PictureBox1.Image MessageBox.Show(PictureBox2.Image.Size.ToString()) End Sub ,则会在Button2中看到与PictureBox2中相同的图像,并且会显示尺寸。如果单击PictureBox1,您将看到Button1中的图像更改尺寸以适合控件。如果再次单击PictureBox1,则在Button2中看到的内容将不会更改,显示的尺寸也不会更改,因为PictureBox2对象没有更改。

如果要获取鼠标光标下方像素的颜色,则需要在控件本身上调用Image,以获取屏幕上实际显示内容的DrawToBitmap。< / p>