我正在使用VS 2010,我正在创建一个C#应用程序。我必须用不同的值来表示一个矩阵,为了让它们更容易理解,我想使用一个ColorMap栏。我确实在Windows Forms中创建了一个项目(它有System.Drawings,但WPF没有)。有没有办法创建它,或者通过创建我自己的“库”来适应它?
下面是我想做的一个例子:

此外,我还想在栏上设置范围。如果有帮助,下面是在Windows Forms中使用ColorMap的代码(它在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);
}
}
}
}发布于 2015-10-16 03:10:34
您需要将引用添加到项目中。右键单击Refrences => Add Reference...。在Assemblies中键入System.Drawing并将其包含到您的项目中。然后只需添加所需的名称空间。System.Drawing.Imaging中的ColorMap类。
https://stackoverflow.com/questions/33154879
复制相似问题