WindowsPresentationImage.cs :  » GIS » GMap.NET » GMap » NET » WindowsPresentation » 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 » WindowsPresentation » WindowsPresentationImage.cs

namespace GMap.NET.WindowsPresentation{
   using System.Windows.Media;
   using System.Windows.Media.Imaging;
   using GMap.NET.Internals;
   using System.Windows;
   using System.Diagnostics;

   internal class TileVisual : FrameworkElement
   {
      public readonly ImageSource[] Source;
      public readonly RawTile Tile;

      public TileVisual(ImageSource[] src, RawTile tile)
      {
         Opacity = 0;
         Source = src;
         Tile = tile;

         this.Loaded += new RoutedEventHandler(ImageVisual_Loaded);
         this.Unloaded += new RoutedEventHandler(ImageVisual_Unloaded);       
      }

      void ImageVisual_Unloaded(object sender, RoutedEventArgs e)
      {
         Child = null;
      }

      void ImageVisual_Loaded(object sender, RoutedEventArgs e)
      {
         UpdateVisual();
      }

      Visual _child;
      public virtual Visual Child
      {
         get
         {
            return _child;
         }
         set
         {
            if(_child != value)
            {
               if(_child != null)
               {
                  RemoveLogicalChild(_child);
                  RemoveVisualChild(_child);
               }

               if(value != null)
               {
                  AddVisualChild(value);
                  AddLogicalChild(value);
               }

               // cache the new child
               _child = value;

               InvalidateVisual();
            }
         }
      }

      public void UpdateVisual()
      {
         Child = Create();
      }

      private DrawingVisual Create()
      {
         var square = new DrawingVisual();

         using(DrawingContext dc = square.RenderOpen())
         {
            foreach(var img in Source)
            {
               dc.DrawImage(img, new Rect(new Size(img.Width + 0.5, img.Height + 0.5)));
            }
         }

         return square;
      }

      #region Necessary Overrides -- Needed by WPF to maintain bookkeeping of our hosted visuals
      protected override int VisualChildrenCount
      {
         get
         {
            return (Child == null ? 0 : 1);
         }
      }

      protected override Visual GetVisualChild(int index)
      {
         Debug.Assert(index == 0);
         return Child;
      }
      #endregion
   }     

   /// <summary>
   /// image abstraction
   /// </summary>
   public class WindowsPresentationImage : PureImage
   {
      public ImageSource Img;

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

         if(Data != null)
         {
            Data.Dispose();
            Data = null;
         }
      }
   }

   /// <summary>
   /// image abstraction proxy
   /// </summary>
   public class WindowsPresentationImageProxy : PureImageProxy
   {
      public override PureImage FromStream(System.IO.Stream stream)
      {
         WindowsPresentationImage ret = null;
         if(stream != null)
         {
            // try png decoder
            try
            {
               PngBitmapDecoder bitmapDecoder = new PngBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
               ImageSource m = bitmapDecoder.Frames[0];

               if(m != null)
               {
                  ret = new WindowsPresentationImage();
                  ret.Img = m;
                  if(ret.Img.CanFreeze)
                  {
                     ret.Img.Freeze();
                  }
               }

               m = null;
               bitmapDecoder = null;
            }
            catch
            {
               // try jpeg decoder
               try
               {
                  stream.Seek(0, System.IO.SeekOrigin.Begin);

                  JpegBitmapDecoder bitmapDecoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                  ImageSource m = bitmapDecoder.Frames[0];

                  if(m != null)
                  {
                     ret = new WindowsPresentationImage();
                     ret.Img = m;
                     if(ret.Img.CanFreeze)
                     {
                        ret.Img.Freeze();
                     }
                  }

                  m = null;
                  bitmapDecoder = null;                    
               }
               catch
               {
                  ret = null;
               }
            }
            finally
            {
               try
               {
                  stream.Seek(0, System.IO.SeekOrigin.Begin);

                  if(ret == null)
                  {
                     stream.Dispose();
                  }
               }
               catch
               {
               }
            }
         } 
         return ret;
      }

      public override bool Save(System.IO.Stream stream, PureImage image)
      {
         WindowsPresentationImage ret = (WindowsPresentationImage) image;
         if(ret.Img != null)
         {
            try
            {
               PngBitmapEncoder e = new PngBitmapEncoder();
               e.Frames.Add(BitmapFrame.Create(ret.Img as BitmapSource));
               e.Save(stream);
            }
            catch
            {
               return false;
            }
         }
         else
         {
            return false;
         }

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