Table of Contents

Source Code ServicesEditionSample FilterRadarImage CS 090817.zip

Program.cs

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Windows.Forms;  
 
 namespace FilterRadarImage  
 {  
     static class Program  
     {  
         /// <summary>  
         /// The main entry point for the application.  
         /// </summary>  
         [STAThread]  
         static void Main()  
         {  
             Application.EnableVisualStyles();  
             Application.SetCompatibleTextRenderingDefault(false);  
             Application.Run(new TestForm());  
         }  
     }  
 }  
 

TestForm.cs

 using System;  
 using System.Drawing;  
 using System.Windows.Forms;  
 using System.Collections.ObjectModel;  
 using ThinkGeo.MapSuite.Core;  
 
 namespace FilterRadarImage  
 {  
     public partial class TestForm : Form  
     {  
         private MapEngine mapEngine = new MapEngine();  
         private Bitmap bitmap = null;  
 
 
         public TestForm()  
         {  
             InitializeComponent();  
         }  
 
         private void TestForm_Load(object sender, EventArgs e)  
         {  
             // Set the full extent and the background color  
             mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-99.28,30.98,-96.58,29.10), Map.Width, Map.Height);  
 
             LoadBackgroundLayers();  
 
             //Add the unfiltered radar image  
             GdiPlusRasterLayer radarImageLayer = new GdiPlusRasterLayer(@"..\..\Data\EWX_N0R_0.gif");  
             radarImageLayer.UpperThreshold = double.MaxValue;  
             radarImageLayer.LowerThreshold = 0;  
 
             mapEngine.StaticLayers.Add("RadarImageLayer", radarImageLayer);  
 
             //Add the Adorment Layer for the legend. Note that the Legend is simply on image showing the dBz scale values.  
             LegendAdornmentLayer legendAdornmentLayer = new LegendAdornmentLayer(new GeoImage(@"..\..\Data\Legend1.bmp"));  
             mapEngine.AdornmentLayers.Add(legendAdornmentLayer);  
 
             DrawImage();  
         }  
 
         //This function takes a collection of colors to filter out. Alpha is for the transparency to apply over the entire output map.  
         private void FilterRadarImage(Collection<Color> Colors, int Alpha, string InputImageFile, string OutputImageFile)  
         {  
             //Create the bitmap from the input image.  
             Bitmap bitmap = new Bitmap(InputImageFile);  
             //Create the bitmap for the output image with the same size as the input image. The PixelFormat is 32bppPArgb so that it accepts  
             //the Alpha channel for transparency.  
             Bitmap newBitmap = new Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);  
 
             //Loop thru every pixel of the input image and set the pixel ARGB value for every pixel of the output image.  
             for (int y = 0; y < bitmap.Height; ++y)  
             {  
                 for (int x = 0; x < bitmap.Width; ++x)  
                 {  
                     Color currentColor = bitmap.GetPixel(x, y);  
                     //If the pixel is not tranparent, do the checking to set the pixel of the output image.  
                     if (currentColor.A != 0)  
                     {  
                         bool change = false;  
                         //Loop thru the collection of colors to filter out.  
                         foreach (Color color in Colors)  
                         {  
                             //If the current pixel has the same RGB value of one of the colors to filter out,  
                             //set the the output pixel to completely tranparent.  
                             if ((currentColor.R == color.R) & (currentColor.G == color.G) & (currentColor.B == color.B))  
                             {  
                                 newBitmap.SetPixel(x, y, Color.FromArgb(0, currentColor));  
                                 change = true;  
                                 break;  
                             }  
                         }  
                         //If the current pixel does not have any of the RGB values of the colors to be filtered out,  
                         //set the output pixel to the alpha value passed as a parameter.  
                         if (change == false)  
                         { newBitmap.SetPixel(x, y, Color.FromArgb(Alpha, currentColor)); }  
                     }  
                     //If the pixel is tranparent set the pixel of the output image as is.  
                     else  
                     { newBitmap.SetPixel(x, y, currentColor); }  
                 }  
             }  
             newBitmap.Save(OutputImageFile);  
 
             bitmap.Dispose();  
             newBitmap.Dispose();  
         }  
 
         private void btnFilter_Click(object sender, EventArgs e)  
         {  
             //Add to the collection all the colors we want to filter out such as  
             //all the blues and grays.  
             Collection<Color> Colors = new Collection<Color>();  
             Colors.Add(Color.FromArgb(255, 240, 240, 240));  
             Colors.Add(Color.FromArgb(255, 238, 238, 238));  
             Colors.Add(Color.FromArgb(255, 235, 235, 235));  
             Colors.Add(Color.FromArgb(255, 232, 232, 232));  
             Colors.Add(Color.FromArgb(255, 230, 230, 230));  
             Colors.Add(Color.FromArgb(255, 227, 227, 227));  
             Colors.Add(Color.FromArgb(255, 225, 225, 225));  
             Colors.Add(Color.FromArgb(255, 253, 253, 253));  
             Colors.Add(Color.FromArgb(255, 254, 254, 254));  
             Colors.Add(Color.FromArgb(255, 4, 233, 231));  
             Colors.Add(Color.FromArgb(255, 1, 159, 244));  
             Colors.Add(Color.FromArgb(255, 3, 0, 244));  
 
             //We call FilerRadarImage function to create the filtered radar image with the transparency.  
             //Keep in mind that the created image needs to have an acompanying world file of the same name, EWX_NOR_0_filtered.gfw  
             FilterRadarImage(Colors, 150, @"..\..\Data\EWX_N0R_0.gif", @"..\..\Data\EWX_N0R_0_filtered.gif");  
 
             //Remove the unfiltered Radar image.  
             mapEngine.StaticLayers.Remove("RadarImageLayer");  
 
             //Add the filtered Radar image.  
             GdiPlusRasterLayer filteredRadarImageLayer = new GdiPlusRasterLayer(@"..\..\Data\EWX_N0R_0_filtered.gif");  
             filteredRadarImageLayer.UpperThreshold = double.MaxValue;  
             filteredRadarImageLayer.LowerThreshold = 0;  
             mapEngine.StaticLayers.Add("FilteredRadarImageLayer", filteredRadarImageLayer);  
 
             //Add the Adorment Layer for the legend of the filtered radar image.  
             mapEngine.AdornmentLayers.Clear();  
             LegendAdornmentLayer legendAdornmentLayer = new LegendAdornmentLayer(new GeoImage(@"..\..\Data\Legend2.bmp"));  
             mapEngine.AdornmentLayers.Add(legendAdornmentLayer);  
 
             DrawImage();  
 
             btnFilter.Enabled = false;  
         }  
 
         private void LoadBackgroundLayers()  
         {  
             mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);  
 
             // Add the static layers to the MapEngine  
             ShapeFileFeatureLayer countyLayer = new ShapeFileFeatureLayer(@"..\..\Data\texascounties.shp", ShapeFileReadWriteMode.ReadOnly);  
             ShapeFileFeatureLayer.BuildIndexFile(@"..\..\Data\texascounties.shp", BuildIndexMode.DoNotRebuild);  
             countyLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.County1;  
             countyLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
             mapEngine.StaticLayers.Add("CountyLayer", countyLayer);  
 
             // Add the label layer to the MapEngine  
             ShapeFileFeatureLayer labelLayer = new ShapeFileFeatureLayer(@"..\..\Data\texascounties.shp");  
             labelLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(TextStyles.County2("NAME"));  
             labelLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
             mapEngine.StaticLayers.Add("LabelLayer", labelLayer);  
         }  
 
 
         private void DrawImage()  
         {  
             if (bitmap != null) { bitmap.Dispose(); }  
             bitmap = new Bitmap(Map.Width, Map.Height);  
             mapEngine.OpenAllLayers();  
             mapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree);  
             mapEngine.DrawAdornmentLayers(bitmap, GeographyUnit.DecimalDegree);  
             mapEngine.CloseAllLayers();  
 
             Map.Image = bitmap;  
         }  
 
         private void ToolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)  
         {  
             switch (e.Button.Tag.ToString())  
             {  
                 case "Zoom In":  
                     mapEngine.CurrentExtent.ScaleDown(50);  
                     break;  
                 case "Zoom Out":  
                     mapEngine.CurrentExtent.ScaleUp(50);  
                     break;  
                 case "Full Extent":  
                     mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-180.0, 83.0, 180.0, -90.0), Map.Width, Map.Height);  
                     break;  
                 case "Pan Left":  
                     mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Left, 20);  
                     break;  
                 case "Pan Right":  
                     mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Right, 20);  
                     break;  
                 case "Pan Up":  
                     mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Up, 20);  
                     break;  
                 case "Pan Down":  
                     mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Down, 20);  
                     break;  
                 default:  
                     break;  
             }  
             DrawImage();  
         }  
 
         private void btnClose_Click(object sender, EventArgs e)  
         {  
             this.Close();  
         }  
 
 
     }  
 }  
 
 //Adornment inherited class for the legend.  
 class LegendAdornmentLayer : AdornmentLayer  
 {  
     private GeoImage legend;  
 
      public LegendAdornmentLayer(GeoImage Legend)  
 
         {  
             this.legend = Legend;  
         }  
 
         public GeoImage Legend  
         {  
             get  
             {  
                 return legend;  
             }  
             set  
             {  
                 legend = value;  
             }  
         }  
 
         protected override void DrawCore(GeoCanvas canvas, Collection<SimpleCandidate> labelsInAllLayers)  
         {  
             canvas.DrawScreenImageWithoutScaling(legend, (legend.GetWidth() /2) + 10, (legend.GetHeight() /2) + 10, DrawingLevel.LevelFour, 0, 0, 0);  
         }  
 }