【WPF学习手记】二维数组的伪彩图显示

目的:实现二维数组的伪彩图显示,自定义Colormap。

 

  • xaml代码
<Window x:Class="Data_to_Bitmap.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Data_to_Bitmap"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Image Name="img" Stretch="Uniform"/>
        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <Button Content="Generate Bitmap" Margin="5" Click="Generate_Click"/>
            <Button Content="Change Colormap" Margin="5" Click="Change_Click"/>
        </StackPanel>
    </Grid>
</Window>
  • 后台代码
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Data_to_Bitmap
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        // Colormap
        private int[,] Parula = new int[256, 3] { { 53, 42, 135 }, { 53, 44, 138 }, { 53, 45, 141 }, { 53, 47, 144 }, { 54, 48, 147 }, { 54, 50, 150 }, { 54, 51, 153 }, { 54, 53, 157 }, { 54, 54, 160 },
            { 54, 56, 163 }, { 54, 57, 166 }, { 53, 59, 169 }, { 53, 60, 172 }, { 52, 62, 176 }, { 52, 64, 179 }, { 51, 65, 182 }, { 50, 67, 185 }, { 49, 69, 188 }, { 47, 70, 192 }, { 46, 72, 195 }, { 44, 74, 198 },
            { 42, 76, 201 }, { 39, 78, 205 }, { 36, 80, 208 }, { 33, 82, 211 }, { 29, 84, 214 }, { 25, 86, 217 }, { 20, 89, 219 }, { 16, 91, 221 }, { 12, 93, 223 }, { 8, 95, 224 }, { 5, 97, 225 }, { 3, 98, 225 },
            { 2, 100, 225 }, { 1, 101, 226 }, { 1, 102, 226 }, { 1, 104, 226 }, { 1, 105, 225 }, { 2, 106, 225 }, { 2, 107, 225 }, { 3, 108, 225 }, { 4, 109, 224 }, { 5, 110, 224 }, { 6, 111, 223 }, { 7, 112, 223 },
            { 8, 113, 223 }, { 10, 114, 222 }, { 11, 115, 222 }, { 12, 116, 221 }, { 13, 117, 220 }, { 14, 118, 220 }, { 14, 119, 219 }, { 15, 120, 219 }, { 16, 121, 218 }, { 17, 122, 218 }, { 17, 123, 217 },
            { 18, 124, 217 }, { 18, 125, 216 }, { 19, 126, 216 }, { 19, 127, 215 }, { 19, 128, 214 }, { 20, 129, 214 }, { 20, 130, 213 }, { 20, 131, 213 }, { 20, 132, 213 }, { 20, 133, 212 }, { 20, 134, 212 },
            { 19, 135, 211 }, { 19, 136, 211 }, { 19, 137, 211 }, { 18, 139, 211 }, { 17, 140, 211 }, { 17, 141, 211 }, { 16, 142, 210 }, { 15, 144, 210 }, { 14, 145, 210 }, { 13, 146, 210 }, { 12, 147, 210 },
            { 11, 149, 210 }, { 10, 150, 210 }, { 9, 151, 210 }, { 8, 152, 209 }, { 8, 153, 209 }, { 7, 155, 209 }, { 7, 156, 208 }, { 6, 157, 208 }, { 6, 158, 207 }, { 6, 159, 207 }, { 6, 160, 206 }, { 6, 160, 205 },
            { 6, 161, 205 }, { 6, 162, 204 }, { 5, 163, 203 }, { 5, 164, 202 }, { 5, 164, 201 }, { 5, 165, 200 }, { 5, 166, 199 }, { 5, 167, 198 }, { 5, 167, 198 }, { 6, 168, 197 }, { 6, 169, 195 }, { 6, 169, 194 },
            { 7, 170, 193 }, { 7, 171, 192 }, { 8, 171, 191 }, { 9, 172, 190 }, { 10, 173, 189 }, { 11, 173, 188 }, { 13, 174, 187 }, { 14, 174, 186 }, { 16, 175, 184 }, { 17, 176, 183 }, { 19, 176, 182 }, { 20, 177, 181 },
            { 22, 177, 179 }, { 24, 178, 178 }, { 26, 178, 177 }, { 28, 179, 176 }, { 30, 179, 174 }, { 32, 180, 173 }, { 34, 180, 172 }, { 36, 181, 170 }, { 38, 181, 169 }, { 40, 182, 167 }, { 42, 182, 166 }, { 45, 183, 165 },
            { 47, 183, 163 }, { 49, 184, 162 }, { 52, 184, 160 }, { 54, 185, 159 }, { 56, 185, 157 }, { 59, 186, 156 }, { 62, 186, 154 }, { 64, 187, 153 }, { 67, 187, 151 }, { 70, 187, 150 }, { 72, 188, 148 }, { 75, 188, 147 },
            { 78, 188, 145 }, { 81, 189, 144 }, { 84, 189, 142 }, { 87, 189, 141 }, { 90, 190, 139 }, { 93, 190, 138 }, { 96, 190, 136 }, { 99, 190, 135 }, { 102, 191, 133 }, { 105, 191, 132 }, { 107, 191, 131 }, { 110, 191, 129 },
            { 113, 191, 128 }, { 116, 191, 127 }, { 119, 191, 126 }, { 122, 191, 124 }, { 125, 191, 123 }, { 128, 191, 122 }, { 130, 191, 121 }, { 133, 191, 120 }, { 136, 191, 119 }, { 138, 191, 118 }, { 141, 191, 117 },
            { 143, 191, 115 }, { 146, 191, 114 }, { 148, 191, 113 }, { 151, 191, 112 }, { 153, 191, 111 }, { 156, 191, 110 }, { 158, 191, 110 }, { 161, 191, 109 }, { 163, 190, 108 }, { 165, 190, 107 }, { 168, 190, 106 },
            { 170, 190, 105 }, { 172, 190, 104 }, { 174, 190, 103 }, { 177, 190, 102 }, { 179, 190, 101 }, { 181, 189, 100 }, { 183, 189, 100 }, { 185, 189, 99 }, { 188, 189, 98 }, { 190, 189, 97 }, { 192, 189, 96 },
            { 194, 188, 95 }, { 196, 188, 94 }, { 198, 188, 94 }, { 200, 188, 93 }, { 202, 188, 92 }, { 204, 187, 91 }, { 206, 187, 90 }, { 209, 187, 89 }, { 211, 187, 88 }, { 213, 187, 88 }, { 215, 187, 87 }, { 217, 186, 86 },
            { 219, 186, 85 }, { 221, 186, 84 }, { 223, 186, 83 }, { 225, 186, 82 }, { 227, 186, 81 }, { 229, 186, 80 }, { 231, 185, 79 }, { 233, 185, 78 }, { 235, 185, 77 }, { 237, 185, 76 }, { 239, 185, 75 }, { 241, 185, 74 },
            { 243, 185, 73 }, { 244, 186, 71 }, { 246, 186, 70 }, { 248, 186, 69 }, { 250, 187, 67 }, { 251, 188, 65 }, { 252, 189, 64 }, { 254, 190, 62 }, { 254, 191, 60 }, { 255, 192, 59 }, { 255, 193, 57 }, { 255, 195, 56 },
            { 255, 196, 54 }, { 255, 197, 53 }, { 255, 199, 52 }, { 255, 200, 51 }, { 254, 201, 49 }, { 254, 203, 48 }, { 253, 204, 47 }, { 253, 205, 46 }, { 252, 206, 45 }, { 252, 208, 44 }, { 251, 209, 43 }, { 251, 210, 42 },
            { 250, 212, 41 }, { 249, 213, 40 }, { 249, 214, 39 }, { 248, 216, 38 }, { 248, 217, 37 }, { 247, 218, 36 }, { 247, 220, 35 }, { 246, 221, 34 }, { 246, 223, 33 }, { 246, 224, 32 }, { 245, 226, 31 }, { 245, 227, 29 },
            { 245, 229, 28 }, { 245, 230, 27 }, { 245, 232, 26 }, { 245, 234, 25 }, { 245, 236, 24 }, { 246, 237, 22 }, { 246, 239, 21 }, { 246, 241, 20 }, { 247, 243, 19 }, { 247, 245, 17 }, { 248, 247, 16 }, { 249, 249, 15 },
            { 249, 251, 13 } };

        private int[,] Jet = new int[256, 3] { { 0, 0, 131 }, { 0, 0, 135 }, { 0, 0, 139 }, { 0, 0, 143 }, { 0, 0, 147 }, { 0, 0, 151 }, { 0, 0, 155 }, { 0, 0, 159 }, { 0, 0, 163 }, { 0, 0, 167 }, { 0, 0, 171 },
            { 0, 0, 175 }, { 0, 0, 179 }, { 0, 0, 183 }, { 0, 0, 187 }, { 0, 0, 191 }, { 0, 0, 195 }, { 0, 0, 199 }, { 0, 0, 203 }, { 0, 0, 207 }, { 0, 0, 211 }, { 0, 0, 215 }, { 0, 0, 219 }, { 0, 0, 223 }, { 0, 0, 227 },
            { 0, 0, 231 }, { 0, 0, 235 }, { 0, 0, 239 }, { 0, 0, 243 }, { 0, 0, 247 }, { 0, 0, 251 }, { 0, 0, 255 }, { 0, 3, 255 }, { 0, 7, 255 }, { 0, 11, 255 }, { 0, 15, 255 }, { 0, 19, 255 }, { 0, 23, 255 }, { 0, 27, 255 },
            { 0, 31, 255 }, { 0, 35, 255 }, { 0, 39, 255 }, { 0, 43, 255 }, { 0, 47, 255 }, { 0, 51, 255 }, { 0, 55, 255 }, { 0, 59, 255 }, { 0, 63, 255 }, { 0, 67, 255 }, { 0, 71, 255 }, { 0, 75, 255 }, { 0, 79, 255 },
            { 0, 83, 255 }, { 0, 87, 255 }, { 0, 91, 255 }, { 0, 95, 255 }, { 0, 99, 255 }, { 0, 103, 255 }, { 0, 107, 255 }, { 0, 111, 255 }, { 0, 115, 255 }, { 0, 119, 255 }, { 0, 123, 255 }, { 0, 127, 255 }, { 0, 131, 255 },
            { 0, 135, 255 }, { 0, 139, 255 }, { 0, 143, 255 }, { 0, 147, 255 }, { 0, 151, 255 }, { 0, 155, 255 }, { 0, 159, 255 }, { 0, 163, 255 }, { 0, 167, 255 }, { 0, 171, 255 }, { 0, 175, 255 }, { 0, 179, 255 }, { 0, 183, 255 },
            { 0, 187, 255 }, { 0, 191, 255 }, { 0, 195, 255 }, { 0, 199, 255 }, { 0, 203, 255 }, { 0, 207, 255 }, { 0, 211, 255 }, { 0, 215, 255 }, { 0, 219, 255 }, { 0, 223, 255 }, { 0, 227, 255 }, { 0, 231, 255 }, { 0, 235, 255 },
            { 0, 239, 255 }, { 0, 243, 255 }, { 0, 247, 255 }, { 0, 251, 255 }, { 0, 255, 255 }, { 3, 255, 251 }, { 7, 255, 247 }, { 11, 255, 243 }, { 15, 255, 239 }, { 19, 255, 235 }, { 23, 255, 231 }, { 27, 255, 227 },
            { 31, 255, 223 }, { 35, 255, 219 }, { 39, 255, 215 }, { 43, 255, 211 }, { 47, 255, 207 }, { 51, 255, 203 }, { 55, 255, 199 }, { 59, 255, 195 }, { 63, 255, 191 }, { 67, 255, 187 }, { 71, 255, 183 }, { 75, 255, 179 },
            { 79, 255, 175 }, { 83, 255, 171 }, { 87, 255, 167 }, { 91, 255, 163 }, { 95, 255, 159 }, { 99, 255, 155 }, { 103, 255, 151 }, { 107, 255, 147 }, { 111, 255, 143 }, { 115, 255, 139 }, { 119, 255, 135 }, { 123, 255, 131 },
            { 127, 255, 127 }, { 131, 255, 123 }, { 135, 255, 119 }, { 139, 255, 115 }, { 143, 255, 111 }, { 147, 255, 107 }, { 151, 255, 103 }, { 155, 255, 99 }, { 159, 255, 95 }, { 163, 255, 91 }, { 167, 255, 87 }, { 171, 255, 83 },
            { 175, 255, 79 }, { 179, 255, 75 }, { 183, 255, 71 }, { 187, 255, 67 }, { 191, 255, 63 }, { 195, 255, 59 }, { 199, 255, 55 }, { 203, 255, 51 }, { 207, 255, 47 }, { 211, 255, 43 }, { 215, 255, 39 }, { 219, 255, 35 },
            { 223, 255, 31 }, { 227, 255, 27 }, { 231, 255, 23 }, { 235, 255, 19 }, { 239, 255, 15 }, { 243, 255, 11 }, { 247, 255, 7 }, { 251, 255, 3 }, { 255, 255, 0 }, { 255, 251, 0 }, { 255, 247, 0 }, { 255, 243, 0 },
            { 255, 239, 0 }, { 255, 235, 0 }, { 255, 231, 0 }, { 255, 227, 0 }, { 255, 223, 0 }, { 255, 219, 0 }, { 255, 215, 0 }, { 255, 211, 0 }, { 255, 207, 0 }, { 255, 203, 0 }, { 255, 199, 0 }, { 255, 195, 0 }, { 255, 191, 0 },
            { 255, 187, 0 }, { 255, 183, 0 }, { 255, 179, 0 }, { 255, 175, 0 }, { 255, 171, 0 }, { 255, 167, 0 }, { 255, 163, 0 }, { 255, 159, 0 }, { 255, 155, 0 }, { 255, 151, 0 }, { 255, 147, 0 }, { 255, 143, 0 }, { 255, 139, 0 },
            { 255, 135, 0 }, { 255, 131, 0 }, { 255, 127, 0 }, { 255, 123, 0 }, { 255, 119, 0 }, { 255, 115, 0 }, { 255, 111, 0 }, { 255, 107, 0 }, { 255, 103, 0 }, { 255, 99, 0 }, { 255, 95, 0 }, { 255, 91, 0 }, { 255, 87, 0 },
            { 255, 83, 0 }, { 255, 79, 0 }, { 255, 75, 0 }, { 255, 71, 0 }, { 255, 67, 0 }, { 255, 63, 0 }, { 255, 59, 0 }, { 255, 55, 0 }, { 255, 51, 0 }, { 255, 47, 0 }, { 255, 43, 0 }, { 255, 39, 0 }, { 255, 35, 0 },
            { 255, 31, 0 }, { 255, 27, 0 }, { 255, 23, 0 }, { 255, 19, 0 }, { 255, 15, 0 }, { 255, 11, 0 }, { 255, 7, 0 }, { 255, 3, 0 }, { 255, 0, 0 }, { 251, 0, 0 }, { 247, 0, 0 }, { 243, 0, 0 }, { 239, 0, 0 }, { 235, 0, 0 },
            { 231, 0, 0 }, { 227, 0, 0 }, { 223, 0, 0 }, { 219, 0, 0 }, { 215, 0, 0 }, { 211, 0, 0 }, { 207, 0, 0 }, { 203, 0, 0 }, { 199, 0, 0 }, { 195, 0, 0 }, { 191, 0, 0 }, { 187, 0, 0 }, { 183, 0, 0 }, { 179, 0, 0 },
            { 175, 0, 0 }, { 171, 0, 0 }, { 167, 0, 0 }, { 163, 0, 0 }, { 159, 0, 0 }, { 155, 0, 0 }, { 151, 0, 0 }, { 147, 0, 0 }, { 143, 0, 0 }, { 139, 0, 0 }, { 135, 0, 0 }, { 131, 0, 0 }, { 127, 0, 0 } };

        private int[,] data;

        public MainWindow()
        {
            InitializeComponent();
           
            data = GenerateHeatMap(100);
        }

        private int[,] GenerateHeatMap(int M)
        {
            var singleData = new double[M];
            for (int x = 0; x < M; ++x)
            {
                singleData[x] = Math.Exp((-1.0 / 2.0) * Math.Pow(((double)x - 50.0) / 20.0, 2.0));
            }
            var data = new int[M, M];
            for (int x = 0; x < M; ++x)
            {
                for (int y = 0; y < M; ++y)
                {
                    data[y, x] = (int)(singleData[x] * singleData[(y + 30) % M] * M);
                }
            }
            return data;
        }

        private int GetMaxValue(int[,] data)
        {
            int maxi = data[0, 0];
            for (int i = 0; i < data.GetLength(0); i++)
            {
                for (int j = 0; j < data.GetLength(1); j++)
                {
                    if (data[i, j] > maxi)
                    {
                        maxi = data[i, j];
                    }
                }
            }
            return maxi;
        }

        private Bitmap ImageDataToPcolorImage(int[,] imageData, int width, int height, int[,] colorMap, int maxValue)
        {
            Bitmap bmp = new Bitmap(height, width);
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    bmp.SetPixel(i, j, System.Drawing.Color.FromArgb(255, colorMap[imageData[height - i - 1, j] * 255 / maxValue, 0], colorMap[imageData[height - i - 1, j] * 255 / maxValue, 1], colorMap[imageData[height - i - 1, j] * 255 / maxValue, 2]));
                }
            }
            return bmp;
        }

        private BitmapImage BitmapToBitmapImage(Bitmap bitmap)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                bitmap.Save(stream, ImageFormat.Png);
                stream.Position = 0;
                BitmapImage result = new BitmapImage();
                result.BeginInit();
                result.CacheOption = BitmapCacheOption.OnLoad;
                result.StreamSource = stream;
                result.EndInit();
                result.Freeze();
                return result;
            }
        }

        private void Generate_Click(object sender, RoutedEventArgs e)
        {
            img.Source = BitmapToBitmapImage(ImageDataToPcolorImage(data, 100, 100, Parula, GetMaxValue(data)));
        }

        private void Change_Click(object sender, RoutedEventArgs e)
        {
            img.Source = BitmapToBitmapImage(ImageDataToPcolorImage(data, 100, 100, Jet, GetMaxValue(data)));
        }
    }
}

技术要领:数据标准化,将数据映射到0~255范围之间;Colormap定制,数据是从matlab里面得到的。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值