2025-04-24 20:56:44 +08:00
|
|
|
|
using ShrlAlgoToolkit.Core.Assists;
|
2025-04-24 20:56:10 +08:00
|
|
|
|
|
|
|
|
|
|
using System.Drawing;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Interop;
|
|
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
namespace ShrlAlgoToolkit.Core.Extensions;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
|
|
|
|
|
public static class ImageExtensions
|
|
|
|
|
|
{
|
2025-04-24 20:56:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-12-22 10:26:12 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 位图转图片
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="bitmap"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="ArgumentNullException"></exception>
|
2025-04-24 20:56:44 +08:00
|
|
|
|
public static BitmapImage ToBitmapImage(this Bitmap bitmap)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (bitmap is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentNullException(nameof(bitmap));
|
|
|
|
|
|
}
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
using var ms = new MemoryStream();
|
|
|
|
|
|
bitmap.Save(ms, ImageFormat.Png);
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
ms.Position = 0;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
var result = new BitmapImage();
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
result.BeginInit();
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
result.StreamSource = ms;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
result.CacheOption = BitmapCacheOption.OnLoad;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
result.EndInit();
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
2025-04-24 20:56:44 +08:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2024-12-22 10:26:12 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 位图转像素集
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="bitmap"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-04-24 20:56:44 +08:00
|
|
|
|
public static BitmapSource ToBitmapSource(this Bitmap bitmap)
|
|
|
|
|
|
{
|
|
|
|
|
|
return bitmap == null
|
|
|
|
|
|
? null
|
|
|
|
|
|
: Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Icon转BitmapSource
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="icon"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static BitmapSource ToBitmapSource(this Icon icon)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Imaging.CreateBitmapSourceFromHIcon(
|
|
|
|
|
|
icon.Handle,
|
|
|
|
|
|
new Int32Rect(0, 0, icon.Width, icon.Height),
|
|
|
|
|
|
BitmapSizeOptions.FromWidthAndHeight(icon.Width, icon.Height)
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-22 11:05:41 +08:00
|
|
|
|
}
|