diff Chronosv2/source/Extensions/Windows/BitmapSourceExtensions.cs @ 10:443821e55f06

Initial cleaned up add from Codeplex files
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:25:44 +0700
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Chronosv2/source/Extensions/Windows/BitmapSourceExtensions.cs	Tue Feb 21 17:25:44 2012 +0700
@@ -0,0 +1,36 @@
+using System;
+using System.Drawing;
+using System.IO;
+using System.Windows.Media.Imaging;
+
+namespace Chronos.Extensions.Windows
+{
+    /// <summary>
+    /// Extension methods for the System.Windows.Media.Imaging.BitmapSource class
+    /// </summary>
+    public static class BitmapSourceExtensions
+    {
+        #region · Extensions ·
+
+        /// <summary>
+        /// Create a System.Drawing.Bitmap from the passed WPF BitmapSource instance
+        /// </summary>
+        /// <param name="bitmapSource">The bitmap source.</param>
+        /// <returns>The generated bitmap</returns>
+        public static Bitmap ToBitmap(this BitmapSource bitmapSource)
+        {
+            var encoder = new BmpBitmapEncoder();
+            encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
+
+            using (var stream = new MemoryStream())
+            {
+                encoder.Save(stream);
+
+                // Nested construction required to prevent issues from closing the underlying stream
+                return new Bitmap(new Bitmap(stream));
+            }
+        }
+
+        #endregion
+    }
+}
\ No newline at end of file