鼠标单击时GetPixel(x,y)上的颜色值不正确 [英] Incorrect color values on GetPixel(x,y) on Mouse Click
问题描述
我正试图从我在pictureBox上显示的图像中提取鼠标点击颜色。图片大于pictureBox,因此必须调整大小,并且分辨率为72 dpi,24位深度。关于图片属性(它是用iPhone拍摄的)它还说分辨率单元2和颜色表示sRGB。
我不确定它是否因为这个高分辨率,但是,无论是使用GetPixel还是锁定的图像数组,我总是得到错误的RGB值,鼠标点击位置。
图片中的鼠标点击事件:
Hi,
I'm trying to extract the mouse click color from a image that I display on a pictureBox. The picture is larger than the pictureBox, so has to be resized, and has a resolution on 72 dpi, 24 bit depth. On the picture properties (it was taken with an iPhone) it also says Resolution Unit 2 and Color Representation sRGB.
I'm not sure if its because of this high resolution, but, either working with GetPixel or a locked image array, I always get wrong RGB values for the mouse click position.
Inside the image mouse click event:
Dim args As System.Windows.Forms.MouseEventArgs = DirectCast(e, System.Windows.Forms.MouseEventArgs)
Dim clkX As Integer = args.X
Dim clkY As Integer = args.Y
lblColor_R.Text = bmpPiece.GetPixel(clkX, clkY).R
lblColor_G.Text = bmpPiece.GetPixel(clkX, clkY).G
lblColor_B.Text = bmpPiece.GetPixel(clkX, clkY).B
(bmpPiece以前声明为pieceBMP =新位图(pieceImagePath))
我使用了普通RGB托盘提供的RGB值,它们与屏幕上的图片完全不同
另外,我试着画一个小的提取颜色的图像,它完全不同:
(imgColor是PictureBox容器)
(bmpPiece is declared previous as pieceBMP = New Bitmap(pieceImagePath) )
I used the RGB values provided against a normal RGB pallet and they are quite different from the picture on the screen
Also, I try to paint a small image with the extracted color, and it's completely different:
(imgColor is the PictureBox container)
Dim pixelBrush As New SolidBrush(bmpPiece.GetPixel(clkX, clkY))
Dim newImage As New Bitmap(imgColor.Width, imgColor.Height)
Dim grap As Drawing.Graphics = Drawing.Graphics.FromImage(newImage)
grap.FillRectangle(pixelBrush, 0, 0, imgColor.Width, imgColor.Height)
imgColor.Image = newImage
有没有办法让这个提取像素颜色的工作?
非常感谢任何正确方向的指针,
Pedro
Is there any way to make this extraction of a pixel color to work?
thanks a lot for any pointer in the right direction,
Pedro
推荐答案
我为你构建了一个方法,它在PictureBox中获取PixelColor并反转其值(用于查看发生的情况)。
我的图像被分配给BackGroundImage,BackgroundImageLayout设置为Stretch。
我希望,此代码片段可以帮助您:
I build you a method which gets the PixelColor inside a PictureBox and inverts in's value (for seeing what happens).
My Image was assigned to BackGroundImage with the BackgroundImageLayout set to Stretch.
I hope, this code-snippet will help you :
Private Sub PictureBox1_Click(sender As System.Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
Dim clkX As Integer = e.X
Dim clkY As Integer = e.Y
Dim gr As Graphics = Graphics.FromImage(PictureBox1.BackgroundImage)
Dim myBitmap As New Bitmap(PictureBox1.BackgroundImage, PictureBox1.Size)
Dim clkColor As Color = myBitmap.GetPixel(clkX, clkY)
Dim newColor = Color.FromArgb(255 - clkColor.R, 255 - clkColor.G, 255 - clkColor.B) ' set Pixel inverted
myBitmap.SetPixel(clkX, clkY, newColor)
PictureBox1.BackgroundImage = myBitmap
End Sub
您好Ralf,
感谢您的建议。我会尝试并回复。有它来自图像本身的回调,我承认我从未想过它需要X / Y转换。
私有子imgPiece_Click(发送者作为对象,e作为EventArgs)处理imgPiece.Click
谢谢,
Pedro
Hi Ralf,
thank you for the suggestion. I'll try it and post back. Has its the callback from the image itself, I confess I never thought it would need X/Y transformation.
Private Sub imgPiece_Click(sender As Object, e As EventArgs) Handles imgPiece.Click
thanks,
Pedro
这篇关于鼠标单击时GetPixel(x,y)上的颜色值不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!