如何从图片框中获取特定像素的颜色? [英] How do I get the color of a specific pixel from a picture box?
问题描述
我的一个程序中有一个图片框,可以很好地显示我的图像.显示的内容包括选定的背景色"和一些使用画笔填充的矩形和一些使用钢笔填充的线条.我没有导入的图像.我需要检索图片框上指定像素的颜色值.我尝试了以下方法:
I have a picture box in one of my programs which displays my images just fine. What's displayed consists of a chosen "BackColor" and some filled rectangles using a brush and some lines using a pen. I have no imported images. I need to retrieve the color value of a specified pixel on the picture box. I've tried the following:
Bitmap b = new Bitmap(pictureBox1.Image);
Color colour = b.GetPixel(X,Y)
但是pictureBox1.Image
总是返回null
..Image
是否仅适用于导入的图像?如果不是,我怎样才能让它发挥作用?有没有其他选择?
But pictureBox1.Image
always returns null
. Does .Image
only work with imported images? If not how can I get this to work? Are there any alternatives?
推荐答案
是的,你可以,但应该你吗?
这是您的代码需要的更改:
Here is the change your code needs:
Bitmap b = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.Height);
pictureBox1.DrawToBitmap(b, pictureBox1.ClientRectangle);
Color colour = b.GetPixel(X, Y);
b.Dispose();
但是如果你想用它做真正的工作,真的没有办法给 PictureBox
一个真正的 Image
来处理它,这意味着如果你想使用它它的可能性,例如它的 SizeMode
.
But there is really no way around giving the PictureBox
a real Image
to work with somewhere if you want to do real work with it, meaning if you want to use its possibilities e.g. its SizeMode
.
简单地在其背景上绘图是不一样的.这是分配真实位图的最少代码:
Simply drawing on its background is just not the same. Here is a minimal code to get a real Bitmap assigned:
public Form1()
{
InitializeComponent();
pictureBox1.Image = new Bitmap(pictureBox1.ClientSize.Width,
pictureBox1.ClientSize.Height);
using (Graphics graphics = Graphics.FromImage(pictureBox1.Image))
{
graphics.FillRectangle(Brushes.CadetBlue, 0, 0, 99, 99);
graphics.FillRectangle(Brushes.Beige, 66, 55, 66, 66);
graphics.FillRectangle(Brushes.Orange, 33, 44, 55, 66);
}
}
然而,如果你真的不想分配一个图像,你可以让 PictureBox
将自己绘制到一个真正的 Bitmap
上.请注意,您必须在 Paint
事件中绘制矩形等以使其正常工作!(实际上,您也必须出于其他原因使用 Paint
事件!)
However if you really don't want to assign an Image you can make the PictureBox
draw itself onto a real Bitmap
. Note that you must draw the Rectangles etc in the Paint
event for this to work! (Actually you must use the Paint
event for other reasons as well!)
现在您可以测试任何一种方式,例如使用标签和鼠标:
Now you can test either way e.g. with a Label and your mouse:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (pictureBox1.Image != null)
{ // the 'real thing':
Bitmap bmp = new Bitmap(pictureBox1.Image);
Color colour = bmp.GetPixel(e.X, e.Y);
label1.Text = colour.ToString();
bmp.Dispose();
}
else
{ // just the background:
Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.Height);
pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
Color colour = bmp.GetPixel(e.X, e.Y);
label1.Text += "ARGB :" + colour.ToString();
bmp.Dispose();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillRectangle(Brushes.DarkCyan, 0, 0, 99, 99);
e.Graphics.FillRectangle(Brushes.DarkKhaki, 66, 55, 66, 66);
e.Graphics.FillRectangle(Brushes.Wheat, 33, 44, 55, 66);
}
这篇关于如何从图片框中获取特定像素的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!