#region Header
//
//   Project:           Windows Phone 7 extensions.
//
//   Changed by:        $Author: rene.schulte $
//   Changed on:        $Date: 2010-07-18 23:58:38 +0200 (So, 18. Jul 2010) $
//   Changed in:        $Revision: 6 $
//   Project:           $URL: file:///C:/Users/Rene.Schulte/Documents/My%20Dropbox/Development/REPO_SVN/WindowsPhoneDemos_Dropbox/trunk/WindowsPhoneMediaLibrarySample/WindowsPhoneMediaLibrarySample/WindowsPhoneMediaLibrarySample/WriteableBitmapMediaLibraryExtensions.cs $
//   Id:                $Id: WriteableBitmapMediaLibraryExtensions.cs 6 2010-07-18 21:58:38Z rene.schulte $
//
//
//   Copyright (c) 2010 Rene Schulte
//
//   This program is open source software. Please read the License.txt.
//
#endregion

using System.IO;
using Microsoft.Xna.Framework.Media;

namespace System.Windows.Media.Imaging
{
    /// <summary>
    /// WriteableBitmap extension for WP7 media library
    /// </summary>
    public static class WriteableBitmapMediaLibraryExtensions
    {
        /// <summary>
        /// Saves the WriteableBitmap encoded as JPEG to the Media library using the best quality of 100.
        /// </summary>
        /// <param name="bitmap">The WriteableBitmap to save.</param>
        /// <param name="name">The name of the destination file.</param>
        public static void SaveToMediaLibrary(this WriteableBitmap bitmap, string name)
        {
            SaveToMediaLibrary(bitmap, name, 100);
        }

        /// <summary>
        /// Saves the WriteableBitmap encoded as JPEG to the Media library.
        /// </summary>
        /// <param name="bitmap">The WriteableBitmap to save.</param>
        /// <param name="name">The name of the destination file.</param>
        /// <param name="quality">The quality for JPEG encoding has to be in the range 0-100, 
        /// where 100 is the best quality with the largest size.</param>
        public static void SaveToMediaLibrary(this WriteableBitmap bitmap, string name, int quality)
        {
            using (var stream = new MemoryStream())
            {
                // Save the picture to the WP7 media library
                bitmap.SaveJpeg(stream, bitmap.PixelWidth, bitmap.PixelHeight, 0, quality);
                stream.Seek(0, SeekOrigin.Begin);
                new MediaLibrary().SavePicture(name, stream);
            }
        }

        ///Achtung Neu:
        /// <summary>
        /// Saves the WriteableBitmap encoded as JPEG to the Media library (CameraRoll) using the best quality of 100.
        /// </summary>
        /// <param name="bitmap">The WriteableBitmap to save.</param>
        /// <param name="name">The name of the destination file.</param>
        public static void SaveToCameraRoll(this WriteableBitmap bitmap, string name)
        {
            SaveToCameraRoll(bitmap, name, 100);
        }

        /// <summary>
        /// Saves the WriteableBitmap encoded as JPEG to the Media library (CameraRoll).
        /// </summary>
        /// <param name="bitmap">The WriteableBitmap to save.</param>
        /// <param name="name">The name of the destination file.</param>
        /// <param name="quality">The quality for JPEG encoding has to be in the range 0-100, 
        /// where 100 is the best quality with the largest size.</param>
        public static void SaveToCameraRoll(this WriteableBitmap bitmap, string name, int quality)
        {
            using (var stream = new MemoryStream())
            {
                // Save the picture to the WP7 media library
                bitmap.SaveJpeg(stream, bitmap.PixelWidth, bitmap.PixelHeight, 0, quality);
                stream.Seek(0, SeekOrigin.Begin);
                new MediaLibrary().SavePictureToCameraRoll(name, stream);
            }
        }
    }
}
