更新整理
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
using System.Collections;
|
||||
using System.Data;
|
||||
|
||||
namespace ShrlAlgo.Toolkit.Core.Extensions;
|
||||
namespace ShrlAlgoToolkit.Core.Extensions;
|
||||
|
||||
public static class DataTableExtensions
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace ShrlAlgo.Toolkit.Core.Extensions;
|
||||
namespace ShrlAlgoToolkit.Core.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// 自定义Distinct扩展方法
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using ShrlAlgo.Toolkit.Core.Assist;
|
||||
using ShrlAlgoToolkit.Core.Assists;
|
||||
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
@@ -8,95 +8,67 @@ using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace ShrlAlgo.Toolkit.Core.Extensions;
|
||||
namespace ShrlAlgoToolkit.Core.Extensions;
|
||||
|
||||
public static class ImageExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 提取资源
|
||||
/// </summary>
|
||||
/// <param name="resourceName"></param>
|
||||
/// <param name="path"></param>
|
||||
public static void ExtractResource(string resourceName, string path)
|
||||
{
|
||||
using var manifestResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
|
||||
using var stream = File.Create(path);
|
||||
var array = new byte[8192];
|
||||
int count;
|
||||
while ((count = manifestResourceStream.Read(array, 0, array.Length)) > 0)
|
||||
{
|
||||
stream.Write(array, 0, count);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 资源管理器缩略图读取
|
||||
/// </summary>
|
||||
/// <param name="filename"></param>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <returns></returns>
|
||||
public static BitmapSource LoadFileImage(string filename, int width, int height)
|
||||
{
|
||||
var bm = WindowsThumbnailProvider.GetThumbnail(filename, width, height, ThumbnailOptions.None);
|
||||
return Imaging.CreateBitmapSourceFromHBitmap(bm.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 位图转图片
|
||||
/// </summary>
|
||||
/// <param name="bitmap"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
public static BitmapImage ToBitmapImage(this Bitmap bitmap)
|
||||
{
|
||||
if (bitmap is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bitmap));
|
||||
}
|
||||
public static BitmapImage ToBitmapImage(this Bitmap bitmap)
|
||||
{
|
||||
if (bitmap is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bitmap));
|
||||
}
|
||||
|
||||
using var ms = new MemoryStream();
|
||||
bitmap.Save(ms, ImageFormat.Png);
|
||||
using var ms = new MemoryStream();
|
||||
bitmap.Save(ms, ImageFormat.Png);
|
||||
|
||||
ms.Position = 0;
|
||||
ms.Position = 0;
|
||||
|
||||
var result = new BitmapImage();
|
||||
var result = new BitmapImage();
|
||||
|
||||
result.BeginInit();
|
||||
result.BeginInit();
|
||||
|
||||
result.StreamSource = ms;
|
||||
result.StreamSource = ms;
|
||||
|
||||
result.CacheOption = BitmapCacheOption.OnLoad;
|
||||
result.CacheOption = BitmapCacheOption.OnLoad;
|
||||
|
||||
result.EndInit();
|
||||
result.EndInit();
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 位图转像素集
|
||||
/// </summary>
|
||||
/// <param name="bitmap"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
public static BitmapSource ToBitmapSource(this Bitmap bitmap)
|
||||
{
|
||||
if (bitmap == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bitmap));
|
||||
}
|
||||
return Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
|
||||
}
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
/// <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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user