[英]Pick color from a Picturebox with SizeMode ZOOM
How to get color of a point (location of mouse cursor) from a ZOOMed Picture box ? 如何从缩放图片框中获取点的颜色(鼠标光标的位置)?
My current code which does not work 我当前的代码不起作用
Private Sub pickColor(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picBox.MouseClick
Dim TempBitmap As New Bitmap(picBox.Image)
Dim MyColor As Color
MyColor = TempBitmap.GetPixel(e.X, e.Y)
End Sub
You can try something like this: 您可以尝试如下操作:
Private Sub pickColor(ByVal sender As Object, ByVal e As MouseEventArgs) _
Handles picBox.MouseClick
Using bmp As New Bitmap(picBox.ClientSize.Width, _
picBox.ClientSize.Height)
picBox.DrawToBitmap(bmp, picBox.ClientRectangle)
MessageBox.Show(bmp.GetPixel(e.X, e.Y).ToString())
End Using
End Sub
I did not know there is method called DrawToBitmap
. 我不知道有一种叫做
DrawToBitmap
方法。 @LatsTech is far better than mine. @LatsTech比我的要好得多。 My solution just simply try to recreate what Picturebox has into Bitmap.
我的解决方案只是尝试重新创建Picturebox在Bitmap中的内容。
Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
Dim bits As New Bitmap(PictureBox1.Width, PictureBox1.Height)
Dim context As Graphics = Graphics.FromImage(bits)
'' Create picturebox background
context.FillRectangle(New SolidBrush(PictureBox1.BackColor), _
0, 0, bits.Width, bits.Height)
'' Try to reproduce zoomed image thumbnail
Dim ratio As Double = 1.0
Dim imageWidth As Integer = PictureBox1.Image.Width
Dim imageHeight As Integer = PictureBox1.Image.Height
If imageWidth > bits.Width Then
ratio = bits.Width / imageWidth
imageWidth = bits.Width
imageHeight *= ratio
End If
If imageHeight > bits.Height Then
ratio = bits.Height / imageHeight
imageHeight = bits.Height
imageWidth *= ratio
End If
context.DrawImage(PictureBox1.Image, _
New Rectangle((bits.Width - imageWidth) / 2, _
(bits.Height - imageHeight) / 2, _
imageWidth, imageHeight), _
New Rectangle(0, 0, PictureBox1.Image.Width, _
PictureBox1.Image.Height), _
GraphicsUnit.Pixel)
MsgBox(bits.GetPixel(e.X, e.Y).ToString)
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.