如何获取Panel像素的颜色在鼠标位置(RFC) [英] How can I get the color of a Panel pixel at the mouse location (RFC)

查看:214
本文介绍了如何获取Panel像素的颜色在鼠标位置(RFC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SO。在10个问题中的8个,它产生一个直接和立即可用的答案。或者至少解释为什么我的问题是错误的。

I love SO. In 8 out of 10 questions it produces a straightforward and immediately usable answer. Or at least explains why my question is somehow wrong.

所以我发现很奇怪,我找不到一个答案,我喜欢这个简单的, ,相当常见的问题。

So I found it strange that I couldn't find an answer to my liking for this simple and, I had asumed, rather common question.

在搜索高低之后,我将下面的解决方案连接在一起。它工作正常,但我不知道:

After searching high and low I patched together the below solution. It works alright but I wonder:


  • 它有缺陷吗?

  • Are there flaws in it? (e.g.: do I need the dispose?)

是否有更好的解决方案,可能需要更少的复制?

Is there a better solution, maybe with less copying going on?

我想看到一个与CopyFromScreen的灵魂,它可能只使用一个1x1位图大小,但需要一个图形。所以一个替代的解决方案将是赞赏。

I'd like to see a soultion with CopyFromScreen , which potentially uses only a 1x1 bitmap size, but needs a Graphics.. So an alternative solution would be appreciated.

或者直接访问控件的像素。

Or one that accesses the control's pixels directly.

注意1:我想从面板中获取颜色!不是从picturebox而是从屏幕。
注意2:对于我的项目速度并不重要,因为我想创建一个吸管工具。但也欢迎快速的方式;谁知道我下一步将去哪里。

Note 1: I want to grab the colors from a panel! Not from a picturebox and not from the screen.. Note 2: For my project speed is not important, as I want to create an eyedropper tool. But speedy ways are welcome, too; who knows where I'll go next ..

我的解决方案:

public Color getColor(Control ctl, Point location)
{
   Bitmap bmp = new Bitmap(ctl.Width, ctl.Height);
   ctl.DrawToBitmap(bmp, new Rectangle(0, 0, ctl.Width, ctl.Height));
   Color col = bmp.GetPixel(location.X, location.Y);
   bmp.Dispose();
   return col;
}

我在colorsPanel_MouseClick事件中使用它:

I use it like this in the colorsPanel_MouseClick event:

       myPen = new Pen(getColor(colorsPanel, e.Location), myStrokeWidth);


推荐答案

我想出了一个使用CopyFromScreen的版本

I have come up with a version that uses CopyFromScreen like this:

public Color getScrColor(Control ctl, Point location)
{
   Bitmap bmp = new Bitmap(1, 1);
   Graphics g = Graphics.FromImage(bmp);
   Point screenP = ctl.PointToScreen(location);
   g.CopyFromScreen(screenP.X, screenP.Y, 0, 0, new Size(1, 1));
   Color col = bmp.GetPixel(0, 0);
   bmp.Dispose();
   return col;
}

它工作正常,但似乎很慢的10)比使用DrawToBitmap的。我怀疑PointToScreen是原因和测试版本,移交一个图形,而没有创建它每次都是同样慢。

It works fine, too but seems to be a lot slower (by a factor of 10) than the one that uses DrawToBitmap. I doubt that the PointToScreen is the reason and a test version, that hands over a Graphics without creating it each time was just as slow.

所以我想CopyFromScreen调用是这么慢,像素数不重要。好,在合理的限度内,可能。我的调色板控件大约有60x400像素。

So I guess the CopyFromScreen call is so much slower, that the number of pixels is not important. Well, within reasonable limits, probably. My color palette Control is about 60x400 pixels.

所以暂时我猜想原来的GetColor解决方案可以使用。

So for the time being I guess the original GetColor solution is OK to use.

这篇关于如何获取Panel像素的颜色在鼠标位置(RFC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆