PotatoNV/PotatoNV-next/Utils/MediaConverter.cs
2020-06-26 17:25:44 +05:00

48 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace PotatoNV_next.Utils
{
class MediaConverter
{
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteObject([In] IntPtr hObject);
public static ImageSource ImageSourceFromBitmap(Bitmap bmp)
{
var handle = bmp.GetHbitmap();
try
{
return Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}
finally
{
DeleteObject(handle);
}
}
public static Bitmap GetBitmapByName(string name)
{
switch (name)
{
case "heart":
return Properties.Resources.Heart;
case "telegram":
return Properties.Resources.Telegram;
default:
throw new Exception($"Unknown resource name: {name}");
}
}
}
}