private void SelectMode(StretchModeItem mode)
 {
     _suspend = true;
     stretchModes.SelectedIndex    = StretchModes.Select(m => m.Tag).IndexOf(mode.Stretch);
     horizontalModes.SelectedIndex = HorizontalAlignments.Select(m => m.Tag).IndexOf(mode.HorizontalAlignment);
     verticalModes.SelectedIndex   = VerticalAlignments.Select(m => m.Tag).IndexOf(mode.VerticalAlignment);
     _suspend = false;
     OnSelectionChanged(null, null);
 }
        private IEnumerable <StretchModeItem> GetAllModes()
        {
            var index = 0;

            foreach (var stretch in StretchModes.Select(m => m.Tag).OfType <Stretch>())
            {
                foreach (var horizontalAlignment in HorizontalAlignments.Select(m => m.Tag).OfType <HorizontalAlignment>())
                {
                    foreach (var verticalAlignment in VerticalAlignments.Select(m => m.Tag).OfType <VerticalAlignment>())
                    {
                        yield return(new StretchModeItem(index++, stretch, horizontalAlignment, verticalAlignment));
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// 按指定宽高和拉伸方法重采样数据集至Bitmap。
        /// </summary>
        /// <param name="InputImg">影像对象。</param>
        /// <param name="Width">重采样宽度。</param>
        /// <param name="Height">重采样高度。</param>
        /// <param name="StretchMode">拉伸模式。</param>
        /// <returns></returns>
        public static Bitmap GetBitmap(Img InputImg, int Width, int Height, StretchModes StretchMode = StretchModes.No_stretch)
        {
            if (StretchMode < 0 || (int)StretchMode > 10)
            {
                throw new ArgumentOutOfRangeException("拉伸模式取值超出范围。");
            }
            Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format32bppRgb);

            ResetStretchInfo(InputImg); //或许需要重设一下拉伸端点。

            #region 灰度图
            if (InputImg.IsGrayscale)
            {
                double BandMin = InputImg.Min[0];
                double BandMax = InputImg.Max[0];
                Band   band    = InputImg.GDALDataset.GetRasterBand(1);

                double[] r = new double[Width * Height];
                band.ReadRaster(0, 0, InputImg.Width, InputImg.Height, r, Width, Height, 0, 0);

                switch (StretchMode)
                {
                case StretchModes.No_stretch:
                {
                    //无拉伸
                    for (int i = 0; i < Width; i++)
                    {
                        for (int j = 0; j < Height; j++)
                        {
                            if (r[i + j * Width] < 0)
                            {
                                bitmap.SetPixel(i, j, InputImg.Colortable[0]);
                                continue;
                            }
                            if (r[i + j * Width] > 255)
                            {
                                bitmap.SetPixel(i, j, InputImg.Colortable[255]);
                                continue;
                            }
                            bitmap.SetPixel(i, j, InputImg.Colortable[(byte)r[i + j * Width]]);
                        }
                    }
                    break;
                }

                case StretchModes.Linear:
                case StretchModes.Linear_1:
                case StretchModes.Linear_2:
                case StretchModes.Linear_5:
                case StretchModes.CustomLinear:
                {
                    //线性
                    for (int i = 0; i < Width; i++)
                    {
                        for (int j = 0; j < Height; j++)
                        {
                            bitmap.SetPixel(i, j, InputImg.Colortable[LinearStretch(r[i + j * Width], InputImg.StretchInfo[0].Item1, InputImg.StretchInfo[0].Item2)]);
                        }
                    }
                    break;
                }

                case StretchModes.Equalization:
                {
                    //直方图均衡
                    for (int i = 0; i < Width; i++)
                    {
                        for (int j = 0; j < Height; j++)
                        {
                            bitmap.SetPixel(i, j, InputImg.Colortable[EqualizationStretch(r[i + j * Width], InputImg.CumulativeProbability[0], InputImg.Min[0], InputImg.Max[0])]);
                        }
                    }
                    break;
                }

                case StretchModes.Gaussion:
                {
                    //高斯
                    for (int i = 0; i < Width; i++)
                    {
                        for (int j = 0; j < Height; j++)
                        {
                            bitmap.SetPixel(i, j, InputImg.Colortable[GaussianStretch(r[i + j * Width], InputImg.CumulativeProbability[0], BandMin, InputImg.Mean[0], InputImg.Stddev[0])]);
                        }
                    }
                    break;
                }

                case StretchModes.SquareRoot:
                {
                    //平方根
                    for (int i = 0; i < Width; i++)
                    {
                        for (int j = 0; j < Height; j++)
                        {
                            bitmap.SetPixel(i, j, InputImg.Colortable[SquarerootStretch(r[i + j * Width], InputImg.StretchInfo[0].Item1, InputImg.StretchInfo[0].Item2)]);
                        }
                    }
                    break;
                }

                case StretchModes.Logarithmic:
                {
                    //对数
                    for (int i = 0; i < Width; i++)
                    {
                        for (int j = 0; j < Height; j++)
                        {
                            bitmap.SetPixel(i, j, InputImg.Colortable[LinearStretch(LogarithmicStretch(r[i + j * Width], InputImg.StretchInfo[0].Item1, InputImg.StretchInfo[0].Item2), InputImg.StretchInfo[0].Item1, InputImg.StretchInfo[0].Item2)]);
                        }
                    }
                    break;
                }

                default:
                {
                    //不是0-9还能是啥?
                    throw new Exception("拉伸模式未知。");
                }
                }
            }
            #endregion

            #region 彩色图
            else   //三波段组合
            {
                Band redBand   = InputImg.GDALDataset.GetRasterBand(InputImg.BandList[0]);
                Band greenBand = InputImg.GDALDataset.GetRasterBand(InputImg.BandList[1]);
                Band blueBand  = InputImg.GDALDataset.GetRasterBand(InputImg.BandList[2]);

                double[] r = new double[Width * Height];
                double[] g = new double[Width * Height];
                double[] b = new double[Width * Height];

                redBand.ReadRaster(0, 0, InputImg.Width, InputImg.Height, r, Width, Height, 0, 0);
                greenBand.ReadRaster(0, 0, InputImg.Width, InputImg.Height, g, Width, Height, 0, 0);
                blueBand.ReadRaster(0, 0, InputImg.Width, InputImg.Height, b, Width, Height, 0, 0);

                //红、绿、蓝波段对应的波段索引。
                int Red   = InputImg.BandList[0] - 1;
                int Green = InputImg.BandList[1] - 1;
                int Blue  = InputImg.BandList[2] - 1;
                //红、绿、蓝波段的统计最小值(Item2)、最大值(Item3)。
                double RedMin   = InputImg.Min[Red];
                double RedMax   = InputImg.Max[Red];
                double GreenMin = InputImg.Min[Green];
                double GreenMax = InputImg.Max[Green];
                double BlueMin  = InputImg.Min[Blue];
                double BlueMax  = InputImg.Max[Blue];

                switch (StretchMode)
                {
                case StretchModes.No_stretch:
                {
                    //无拉伸
                    BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
                    try
                    {
                        int    stride = bitmapData.Stride;
                        IntPtr buf    = bitmapData.Scan0;

                        blueBand.ReadRaster(0, 0, InputImg.Width, InputImg.Height, buf, Width, Height, DataType.GDT_Byte, 4, stride);
                        greenBand.ReadRaster(0, 0, InputImg.Width, InputImg.Height, new IntPtr(buf.ToInt32() + 1), Width, Height, DataType.GDT_Byte, 4, stride);
                        redBand.ReadRaster(0, 0, InputImg.Width, InputImg.Height, new IntPtr(buf.ToInt32() + 2), Width, Height, DataType.GDT_Byte, 4, stride);
                    }
                    finally
                    {
                        bitmap.UnlockBits(bitmapData);
                    }
                    break;
                }

                case StretchModes.Linear:
                case StretchModes.Linear_1:
                case StretchModes.Linear_2:
                case StretchModes.Linear_5:
                case StretchModes.CustomLinear:
                {
                    //线性
                    for (int i = 0; i < Width; i++)
                    {
                        for (int j = 0; j < Height; j++)
                        {
                            Color newColor = Color.FromArgb(
                                LinearStretch(r[i + j * Width], InputImg.StretchInfo[Red].Item1, InputImg.StretchInfo[Red].Item2),
                                LinearStretch(g[i + j * Width], InputImg.StretchInfo[Green].Item1, InputImg.StretchInfo[Green].Item2),
                                LinearStretch(b[i + j * Width], InputImg.StretchInfo[Blue].Item1, InputImg.StretchInfo[Blue].Item2));
                            bitmap.SetPixel(i, j, newColor);
                        }
                    }
                    break;
                }

                case StretchModes.Equalization:
                {
                    //直方图均衡
                    for (int i = 0; i < Width; i++)
                    {
                        for (int j = 0; j < Height; j++)
                        {
                            Color newColor = Color.FromArgb(
                                EqualizationStretch(r[i + j * Width], InputImg.CumulativeProbability[Red], RedMin, RedMax),
                                EqualizationStretch(g[i + j * Width], InputImg.CumulativeProbability[Green], GreenMin, GreenMax),
                                EqualizationStretch(b[i + j * Width], InputImg.CumulativeProbability[Blue], BlueMin, BlueMax));
                            bitmap.SetPixel(i, j, newColor);
                        }
                    }
                    break;
                }

                case StretchModes.Gaussion:
                {
                    //高斯
                    for (int i = 0; i < Width; i++)
                    {
                        for (int j = 0; j < Height; j++)
                        {
                            Color newColor = Color.FromArgb(
                                GaussianStretch(r[i + j * Width], InputImg.CumulativeProbability[Red], RedMin, InputImg.Mean[Red], InputImg.Stddev[Red]),
                                GaussianStretch(g[i + j * Width], InputImg.CumulativeProbability[Green], GreenMin, InputImg.Mean[Green], InputImg.Stddev[Green]),
                                GaussianStretch(b[i + j * Width], InputImg.CumulativeProbability[Blue], BlueMin, InputImg.Mean[Blue], InputImg.Stddev[Blue]));
                            bitmap.SetPixel(i, j, newColor);
                        }
                    }
                    break;
                }

                case StretchModes.SquareRoot:
                {
                    //平方根
                    for (int i = 0; i < Width; i++)
                    {
                        for (int j = 0; j < Height; j++)
                        {
                            Color newColor = Color.FromArgb(
                                SquarerootStretch(r[i + j * Width], RedMin, RedMax),
                                SquarerootStretch(g[i + j * Width], GreenMin, GreenMax),
                                SquarerootStretch(b[i + j * Width], BlueMin, BlueMax));
                            bitmap.SetPixel(i, j, newColor);
                        }
                    }
                    break;
                }

                case StretchModes.Logarithmic:
                {
                    //对数
                    for (int i = 0; i < Width; i++)
                    {
                        for (int j = 0; j < Height; j++)
                        {
                            Color newColor = Color.FromArgb(
                                LinearStretch(LogarithmicStretch(r[i + j * Width], RedMin, RedMax), RedMin, RedMax),
                                LinearStretch(LogarithmicStretch(g[i + j * Width], GreenMin, GreenMax), GreenMin, GreenMax),
                                LinearStretch(LogarithmicStretch(b[i + j * Width], BlueMin, BlueMax), BlueMin, BlueMax));
                            bitmap.SetPixel(i, j, newColor);
                        }
                    }
                    break;
                }

                default:
                {
                    //不是0-9还能是啥?
                    break;
                }
                }
            }
            #endregion

            return(bitmap);
        }