jquery_stack
jquery_stack

Reputation: 261

ColorMap values WPF

I'm working with VS C# 2010 and I'm creating a WPF application. I have to represent a matrix with different values, and to make them a bit easier to understand I'd like to use a ColorMap bar. I did create once a project in Windows Forms (it has System.Drawings but WPF doesn't). Is there any way of creating it or adapting it by creating my own "library"?

Here is an example of what I'd like to do: Example

But furthermore I'd like to set the ranges on the bar as well. If it helps, here is the code using ColorMap in Windows Forms (it does not work in WPF):

namespace Example1_8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SetStyle(ControlStyles.ResizeRedraw, true);
            this.BackColor = Color.White;
            this.Width = 340;
            this.Height = 340;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            int width = 30;
            int height = 128;
            int y = 0;
            // Create opaque color maps with alpha = 255:
            ColorMap cm = new ColorMap();
            DrawColorBar(g, 10 + 4 * 40, y, width, height, cm, "Jet");
            }

        private void DrawColorBar(Graphics g, int x, int y, int width, int height, 
            ColorMap map, string str)
        {
            int[,] cmap = new int[64, 4];
            switch (str)
            {
                case "Jet":
                    cmap = map.Jet();
                    break;
            }

            int ymin = 0;
            int ymax = 32;
            int dy = height / (ymax - ymin);
            int m = 64;
            for (int i = 0; i < 32; i++)
            {
                int colorIndex = (int)((i - ymin) * m / (ymax - ymin));
                SolidBrush aBrush = new SolidBrush(Color.FromArgb(
                    cmap[colorIndex, 0], cmap[colorIndex, 1],
                    cmap[colorIndex, 2], cmap[colorIndex, 3]));
                g.FillRectangle(aBrush, x, y + i * dy, width, dy);
            }
        }
    }
}

Upvotes: 2

Views: 3422

Answers (1)

SteppingRazor
SteppingRazor

Reputation: 1272

You need to add the reference to your project. Right click on Refrences => Add Reference.... In Assemblies type System.Drawing and include that to your project. Then just add needed namespaces. ColorMap class is in System.Drawing.Imaging.

Upvotes: 1

Related Questions