WindowsFormsImage.cs :  » GIS » GMap.NET » GMap » NET » WindowsForms » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GIS » GMap.NET 
GMap.NET » GMap » NET » WindowsForms » WindowsFormsImage.cs

namespace GMap.NET.WindowsForms{
   using System.Drawing;
   using System.IO;
   using System.Drawing.Imaging;
   using System;
   using System.Diagnostics;

   /// <summary>
   /// image abstraction
   /// </summary>
   public class WindowsFormsImage : PureImage
   {
      public System.Drawing.Image Img;

      public override void Dispose()
      {
         if(Img != null)
         {
            Img.Dispose();
            Img = null;
         }
      }
   }

   /// <summary>
   /// image abstraction proxy
   /// </summary>
   public class WindowsFormsImageProxy : PureImageProxy
   {
#if !PocketPC
      public ColorMatrix ColorMatrixForGrayScale = new ColorMatrix(new float[][] 
      {
         new float[] {.3f, .3f, .3f, 0, 0},
         new float[] {.59f, .59f, .59f, 0, 0},
         new float[] {.11f, .11f, .11f, 0, 0},
         new float[] {0, 0, 0, 1, 0},
         new float[] {0, 0, 0, 0, 1}
      });

      public bool GrayScale = false;
#endif

      public override PureImage FromStream(Stream stream)
      {
         WindowsFormsImage ret = null;
         try
         {
            if(!GMaps.Instance.IsRunningOnMono)
            {
#if !PocketPC
               Image m = Image.FromStream(stream, true, true);
#else
               Image m = new Bitmap(stream);
#endif
               if(m != null)
               {
                  ret = new WindowsFormsImage();
#if !PocketPC
                  ret.Img = GrayScale ? MakeGrayscale(m) : m;
#else
                  ret.Img = m;
#endif
               }
            }
            else // mono yet do not support validation
            {
#if !PocketPC
               Image m = Image.FromStream(stream);
#else
               Image m = new Bitmap(stream);
#endif
               if(m != null)
               {
                  ret = new WindowsFormsImage();
#if !PocketPC
                  ret.Img = GrayScale ? MakeGrayscale(m) : m;
#else
                  ret.Img = m;
#endif
               }
            }
         }
         catch(Exception ex)
         {
            ret = null;
            Debug.WriteLine("FromStream: " + ex.ToString());
         }
         finally
         {
            try
            {
               stream.Seek(0, System.IO.SeekOrigin.Begin);

               if(ret == null)
               {
#if !PocketPC
                  stream.Dispose();
#else
                  IDisposable s = stream as IDisposable;
                  if(s != null)
                  {
                     s.Dispose();
                  }
#endif
               }
            }
            catch
            {
            }
         }
         return ret;
      }

      public override bool Save(Stream stream, GMap.NET.PureImage image)
      {
         WindowsFormsImage ret = image as WindowsFormsImage;
         bool ok = true;

         if(ret.Img != null)
         {
            // try png
            try
            {
               ret.Img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            }
            catch
            {
               // try jpeg
               try
               {
                  stream.Seek(0, SeekOrigin.Begin);
                  ret.Img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
               }
               catch
               {
                  ok = false;
               }
            }
         }
         else
         {
            ok = false;
         }

         return ok;
      }

#if !PocketPC
      Bitmap MakeGrayscale(Image original)
      {
         // create a blank bitmap the same size as original
         Bitmap newBitmap = newBitmap = new Bitmap(original.Width, original.Height);
         using(original) // destroy original
         {
            // get a graphics object from the new image
            using(Graphics g = Graphics.FromImage(newBitmap))
            {
               // set the color matrix attribute
               using(ImageAttributes attributes = new ImageAttributes())
               {
                  attributes.SetColorMatrix(ColorMatrixForGrayScale);
                  g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
               }
            }
         }

         return newBitmap;
      }
#endif
   }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.