7

我正在做项目,我必须使用 C# 做一种颜色选择器。

所以我决定在 Win Forms App 中做一个具有这种背景的 Panel。

背景应具有 RGB 中三种颜色的渐变:红色 (0 - 255)、蓝色 (0 - 255) 和绿色 = 0。

gu0oJ.png

但我找不到任何关于我应该为此使用什么的信息。

我试图编写一些代码,这就是我所做的。

{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }



    private void Form1_Load(object sender, EventArgs e)
    {
        panel1.Paint += new PaintEventHandler(panel1_Paint);
        panel1.Refresh();
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        Point startPoint = new Point(0, 0);
        Point endPoint = new Point(150, 150);

        LinearGradientBrush lgb =
            new LinearGradientBrush(startPoint, endPoint,     Color.FromArgb(255, 255, 0, 0), Color.FromArgb(255, 255, 255, 0));
        Graphics g = e.Graphics;
        g.FillRectangle(lgb, 0, 0, 150, 150);
       // g.DrawLine(new Pen(Color.Yellow, 1.5f), startPoint, endPoint);
    }
}

}

现在我有了这个渐变的面板

ORnzf.png

我应该用什么来获得第一张图片的渐变?

第二个问题:单击此背景后,我应该怎么做才能获得像素颜色?

4

2 回答 2

20

LinearGradientBrush以下是在Paint事件中使用多色的示例:

LinearGradientBrush linearGradientBrush =
   new LinearGradientBrush(panel4.ClientRectangle, Color.Red, Color.Yellow, 45);

ColorBlend cblend = new ColorBlend(3);
cblend.Colors = new Color[3]  { Color.Red, Color.Yellow, Color.Green };
cblend.Positions = new float[3] { 0f, 0.5f, 1f };

linearGradientBrush.InterpolationColors = cblend;

e.Graphics.FillRectangle(linearGradientBrush, panel4.ClientRectangle);

在此处输入图像描述

您可以自由改变颜色的数量、角度或停止点的分布。只需确保您始终拥有相同数量的颜色和停止点,并让它们从 0 开始并以 1 结束。

构造函数中的颜色被忽略,顺便说一句..

要获得点击的颜色,您可以编写以下代码MouseClick

Color clickedColor = Color.Empty;

private void panel_MouseClick(object sender, MouseEventArgs e)
{
    using (Bitmap bmp = new Bitmap( panel.ClientSize.Width, panel4.ClientSize.Height))
    {
        panel.DrawToBitmap(bmp,panel.ClientRectangle);
        clickedColor = bmp.GetPixel(e.X, e.Y);
    }
}

如果您想获得多次点击,最好将其保留Bitmap在类级别变量中,而不是一直重新创建它。将其设置为面板的背景图像,正如 Kala 的回答所假设的那样,这也可能是一个不错的选择。

这应该回答标题中的问题。但是,您的第一张图像没有显示三种颜色的渐变。它显示了具有四种颜色的 2D 渐变。对于这种更昂贵的着色方法,您应该将颜色放入 a并将Bitmap其设置为..PanelBackgroundImage

Update1这是一段创建 2D 渐变的代码:

Bitmap Gradient2D(Rectangle r, Color c1, Color c2, Color c3, Color c4)
{
    Bitmap bmp = new Bitmap(r.Width, r.Height);

    float delta12R = 1f * (c2.R - c1.R) / r.Height;
    float delta12G = 1f * (c2.G - c1.G) / r.Height;
    float delta12B = 1f * (c2.B - c1.B) / r.Height;
    float delta34R = 1f * (c4.R - c3.R) / r.Height;
    float delta34G = 1f * (c4.G - c3.G) / r.Height;
    float delta34B = 1f * (c4.B - c3.B) / r.Height;
    using (Graphics G = Graphics.FromImage(bmp) )
    for (int y = 0; y < r.Height; y++)
    {
        Color c12 = Color.FromArgb(255,  c1.R + (int)(y * delta12R), 
              c1.G + (int)(y * delta12G), c1.B + (int)(y * delta12B));
        Color c34 = Color.FromArgb(255, c3.R + (int)(y * delta34R), 
              c3.G + (int)(y * delta34G), c3.B + (int)(y * delta34B));
        using ( LinearGradientBrush lgBrush = new LinearGradientBrush(
              new Rectangle(0,y,r.Width,1), c12, c34, 0f) )
        {  G.FillRectangle(lgBrush, 0, y, r.Width, 1);  }
    }
    return bmp;
}

以下是你如何使用它:

    public Form1()
    {
        InitializeComponent();
        panel.BackgroundImage = Gradient2D(panel.ClientRectangle, 
               Color.Black, Color.FromArgb(255, 0, 255, 0), Color.Red, Color.Yellow);
    }

这使用简单LinearGradientBrushes,没有额外的颜色列表超过Panel.

请注意,这Color.Green是一个相当暗的色调,所以我使用FromRgb了更亮的绿色。如果您Panel的像素大于 256 像素,您可能需要通过填充更大的条纹来进行优化;如果它是垂直的,您可能需要更改循环以越过 x 而不是 y..

结果如下:

在此处输入图像描述

要通过单击进行选择,您现在只需从 中读出颜色BackgroundImage

private void panel_MouseClick(object sender, MouseEventArgs e)
{
    clickedColor = ((Bitmap)panel.BackgroundImage).GetPixel(e.X, e.Y);
}

更新 2:

查看这个 MSDN 页面时,我们可以发现实际上有一个内置工具可以创建 2D 渐变。

它是PathGradientBrush

这是一个例子..:

在此处输入图像描述

..和代码:

Bitmap Gradient2D(Rectangle r, Color c1, Color c2, Color c3, Color c4)
{
    List<Color> colors = new List<Color> {  c1, c3, c4, c2 };
    Bitmap bmp = new Bitmap(r.Width, r.Height);
    using (Graphics g = Graphics.FromImage(bmp))
    for (int y = 0; y < r.Height; y++)
    {

        using (PathGradientBrush pgb = new PathGradientBrush(getCorners(r).ToArray()))
        {
            pgb.CenterColor = medianColor(colors);
            pgb.SurroundColors = colors.ToArray();
            g.FillRectangle(pgb, 0, y, r.Width, 1);
        }
    }
    return bmp;
}

这使用了两个简单的辅助函数。一个返回矩形的角点:

public List<PointF> getCorners(RectangleF r)
{
    return new List<PointF>() { r.Location, new PointF(r.Right, r.Top),
        new PointF(r.Right, r.Bottom), new PointF(r.Left, r.Bottom)};
}

另一个从 a 计算中值颜色List<Color>。这被用作CenterColor..:

public static Color medianColor(List<Color> cols)
{
    int c = cols.Count;
    return Color.FromArgb(cols.Sum(x => x.A) / c, cols.Sum(x => x.R) / c,
        cols.Sum(x => x.G) / c, cols.Sum(x => x.B) / c);
}

结果与使用LinearGradientBrushes. 它更简单,性能应该更好一些;这显然是我推荐的..

注意颜色(或角)的更改顺序!适用于矩形的SurroundColors对角..

笔记:

在研究该页面时,您会发现该画笔实际上有四种不同的用途。

它们的不同之处在于如何设置(GraphicsPathPoint[])、填充哪些颜色集合(SurroundColorsInterpolationColors.Colors)以及如何调用它(使用形状或路径)。而且结果也相差很大。

另请注意,尽管提供了所有四种方法的代码,但仅显示了四种方法的三个结果!..

于 2015-05-20T06:07:14.537 回答
1

从鼠标单击事件参数 e 中,您可以获得具有单击确切坐标的 Point:

Point clickPoint = e.GetPosition(backgroundControlWithImg);

然后使用以下方法获取该位置的图像颜色:

System.Drawing.Image image = backgroundControl.BackgroundImage;
Bitmap _bitmap = new Bitmap(image);
Color _color = bitmap.GetPixel(Point.x, Point.y);

类似的东西。你用什么颜色选择器,WPF 或?

于 2015-05-20T03:28:43.153 回答