0

I want to make a range trackbar that has the colours of an image (each colour has a numerical value associated), so when you move the thumbs of the trackbar you only show in the image the range of colours between the two thumbs. I've looked for some trackbars that may be helpful but I haven't found any that shows the colours of an image. Any ideas? Thank you.

  • Sure one can do it, esp. when you don't try to subclass TrckBar. Use maybe a Panel and add a [MoveLabel](https://stackoverflow.com/questions/50714327/edit-points-of-freeshape/50718793#50718793) to its controls. – TaW Jul 20 '20 at 16:58
  • https://stackoverflow.com/q/581568/103167 – Ben Voigt Jul 20 '20 at 22:19

1 Answers1

0

The following snippets might inspire you for your own development:

private void SetCurrentColor(int x, int y)
{
    if (!busy)
    {
        busy = true;
        Point location = new Point(x, y);

        color = GetColorAt(location);
        int rgb = color.ToArgb() & 0x00FFFFFF;
        lblRgbInt.Text = "#" + rgb.ToString("X");
        lblRgb.Text = $"{color.R},{color.G},{color.B}";
        busy = false;
    }
}

private void HookManager_MouseMove(object sender, MouseEventArgs e)
{
    SetCurrentColor(e.X, e.Y);
}

Bitmap screenPixel = new Bitmap(1, 1, PixelFormat.Format32bppArgb);

public Color GetColorAt(Point location)
{
    using (Graphics gdest = Graphics.FromImage(screenPixel))
    {
        using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero))
        {
            IntPtr hSrcDC = gsrc.GetHdc();
            IntPtr hDC = gdest.GetHdc();
            int retval = BitBlt(hDC, 0, 0, 1, 1, hSrcDC, location.X, location.Y, (int)CopyPixelOperation.SourceCopy);
            gdest.ReleaseHdc();
            gsrc.ReleaseHdc();
        }
    }

    return screenPixel.GetPixel(0, 0);
}

Look here for a related post.

Axel Kemper
  • 10,544
  • 2
  • 31
  • 54
  • First of all, thank you for answering. I think it could work but wouldn't it just put the colours of a single pixel in the image? – joaquin alcibar Jul 22 '20 at 18:20
  • Yes, it shows how to get the color of a specific pixel. It should be not too hard to extend the procedure to image regions larger than 1x1. – Axel Kemper Jul 22 '20 at 20:37