// ImageLibrary.cs
// Copyright (c) 2015 Bentley Systems, Incorporated. All Rights Reserved.

using System;
using System.Drawing;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using Haestad.Support.Win32Api;

namespace Haestad.Framework.UI.Integration
{
    /// <summary>
    /// Class provides static utility methods for working with images.
    /// </summary>
    internal sealed class ImageLibrary
    {
        #region Constructor
        private ImageLibrary()
        {
        }
        #endregion

        #region Internal Static Methods
        internal static BitmapSource ConvertBitmapToBitmapSource(Bitmap bitmap)
        {
            IntPtr hBitmap = bitmap.GetHbitmap();
            BitmapSource bitmapSource = null;
            try
            {
                bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());
            }
            finally
            {
                Gdi32Dll.DeleteObject(hBitmap);
            }
            return bitmapSource;
        }

        internal static BitmapSource ConvertBitmapToBitmapSource(Icon icon)
        {
            return ConvertBitmapToBitmapSource(icon.ToBitmap());
        }
        #endregion
    }
}
