C#鼠标获取Picturebox的颜色(基本完成)

标签 c#

我的代码快完成了,但它不起作用。它给了我错误的图像位置,我认为这是因为图像设置为拉伸(stretch)。

有什么帮助吗?

        private void originalmaster_Click(object sender, EventArgs e)
    {

        var rato = e as MouseEventArgs;
        Bitmap b = new Bitmap(originalmaster.Image);
        Color c = b.GetPixel(rato.X, rato.Y);
        h = Math.Round(c.GetHue(), 0);
        h = (h * 255) / 360;
        s = Math.Round(c.GetSaturation() * 255, 0);
        v = Math.Round(c.GetBrightness() * 255, 0);


        lb_valores_hsv.Text = "H: " + h + " S: " + s + " V: " + v;
        lb_valores_rgb.Text = "R: " + c.R + " G: " + c.G + " B: " + c.B;

    }

rato 的位置不正确。

最佳答案

简短的回答:

     MouseEventArgs rato = e as MouseEventArgs;
     Bitmap b = ((Bitmap)originalmaster.Image);
     int x = rato.X * b.Width / originalmaster.ClientSize.Width;
     int y = rato.Y * b.Height / originalmaster.ClientSize.Height;
     Color c = b.GetPixel(x, y);

关于C#鼠标获取Picturebox的颜色(基本完成),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41701829/

相关文章:

c# - 使用 C# 将 HTTP 发布为 IE6

c# - 如何让一个方法接受两种类型的数据作为参数?

c# - 使用 Autofac 将 SignalR IHubContext 注入(inject) Controller

c# - 如何将属性添加到基类的属性

c# - 第一次重大部署

c# - Ninject BindFIlter<> 中 FilterScope 参数的用途是什么?

c# - 如何编写具有某些功能的 DataTemplate 作为资源

c# - 循环依赖

c# - 在给定 HWND 的情况下如何获取窗口的子窗口?

c# - 是否有可能通过反射获得属性(property)的私有(private)二传手?