0

I have created a multiple Color gradient on Windows Form panel. Now I want to get color in specific location.

Updated:

I am working on a correlation matrix where the data values will be in range of 1 and -1. E.g 1, 0.99, -0.33, and -1 etc. I don't want to use hard coded colors values because user wants to create his own colors patterns. These color patterns will be saved in database.

    private void panel_Paint(object sender, PaintEventArgs e)
    {
        var clientRectangle = new Rectangle(0, 0, 200, 200);

        LinearGradientBrush br = new LinearGradientBrush(clientRectangle, Color.Green, Color.Red,
            LinearGradientMode.Horizontal);

        ColorBlend cb = new ColorBlend();

        cb.Positions = new[] { 0, 0.5f, 1 };
        cb.Colors = new[] { Color.Green, Color.Yellow, Color.Red };
        br.InterpolationColors = cb;

        e.Graphics.FillRectangle(br, clientRectangle);
    }

enter image description here

I have searched on it but can't find the solution. How can I get?

Thank you

Shoaib Ijaz
  • 5,347
  • 12
  • 56
  • 84
  • 2
    Maybe [this](http://stackoverflow.com/questions/753132/how-do-i-get-the-colour-of-a-pixel-at-x-y-using-c)? – Pikoh Mar 22 '16 at 15:55
  • 1
    You also could draw to graphics to a bitmap of the same size and and use `GetPixel`. If you need to do this repeatedly you can cache the bitmap or create a `Dictionary` from it. And then there is `control.DrawToBitMap()`.. – TaW Mar 22 '16 at 16:16
  • Heck of an XY question. LinearGradientBrush uses unspecified gamma correction so GetPixel() is going to be it without knowing anything else about the purpose. – Hans Passant Mar 22 '16 at 16:17
  • XY-question, indeed. Do you want to create a color picker or what? – TaW Mar 22 '16 at 16:19
  • I am working on a correlation matrix. Where data values will be in the range of (1 & -1). But I cant hard code colors values because user wants to define his own color patterns. These color patterns or gradients will be saved in Database. – Shoaib Ijaz Mar 22 '16 at 19:32
  • 1
    Well that sounds a lot like what I suggested: Don't use the `Paint` event but create a `Bitmap` and set it as the Panel's `BackgroundImage`. Then use the `Bitmap.GetPixel` method to access the colors by coordinate e.g. in a mouseclick event: `Color c = ( (Bitmap) panel.BackgroundImage ).GetPixel( e.X, e.Y);` Or try [this way..](http://stackoverflow.com/questions/22493205/how-can-i-get-the-color-of-a-panel-pixel-at-the-mouse-location-rfc/22501994#22501994)! – TaW Mar 22 '16 at 21:44

0 Answers0