2015-10-15 68 views
1

我正在使用VS C#2010,並創建一個WPF應用程序。我必須用不同的值表示一個矩陣,並使它們更容易理解我想使用ColorMap欄。我曾經在Windows Forms中創建過一個項目(它有System.Drawings,但WPF沒有)。有沒有創建它或通過創建我自己的「庫」來適應它的方法?ColorMap values WPF

這裏是想我做的一個例子: Example

但另外我想設置範圍的欄上也是如此。如果有幫助,下面是在Windows窗體中使用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); 
      } 
     } 
    } 
} 

回答

1

您需要將引用添加到您的項目中。右鍵點擊Refrences =>Add Reference...。在Assemblies中鍵入System.Drawing並將其添加到您的項目中。然後只需添加所需的名稱空間。 ColorMap班級在System.Drawing.Imaging