添加项目文件。
This commit is contained in:
155
Sai.Toolkit.Core/Extensions/DataTableExtensions.cs
Normal file
155
Sai.Toolkit.Core/Extensions/DataTableExtensions.cs
Normal file
@@ -0,0 +1,155 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Sai.Toolkit.Core.Extensions;
|
||||
|
||||
public static class DataTableExtensions
|
||||
{
|
||||
public static string GetValue(this DataTable dt, int row, int column)
|
||||
{
|
||||
return dt != null ? dt.Rows[row][column].ToString() : string.Empty;
|
||||
}
|
||||
|
||||
public static string GetValue(this DataRow dr, int column)
|
||||
{
|
||||
return dr == null ? throw new ArgumentNullException() : dr[column].ToString();
|
||||
}
|
||||
|
||||
public static void SetValue(this DataTable dt, int row, int column, object value)
|
||||
{
|
||||
dt.Rows[row][column] = value;
|
||||
}
|
||||
|
||||
public static void SetValue(this DataRow dr, int column, object value)
|
||||
{
|
||||
if (dr == null)
|
||||
{
|
||||
throw new ArgumentNullException();
|
||||
}
|
||||
|
||||
dr[column] = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转实体,属性名必须与列名相同
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="table"></param>
|
||||
/// <returns></returns>
|
||||
public static List<T> ToEntities<T>(this DataTable table)
|
||||
where T : new()
|
||||
{
|
||||
var entities = new List<T>();
|
||||
if (table == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach (DataRow row in table.Rows)
|
||||
{
|
||||
T entity = new();
|
||||
foreach (var item in entity.GetType().GetProperties())
|
||||
{
|
||||
if (table.Columns.Contains(item.Name))
|
||||
{
|
||||
if (DBNull.Value != row[item.Name])
|
||||
{
|
||||
var newType = item.PropertyType;
|
||||
//判断type类型是否为泛型,因为nullable是泛型类,
|
||||
if (newType.IsGenericType && newType.GetGenericTypeDefinition() == typeof(Nullable<>)) //判断convertsionType是否为nullable泛型类
|
||||
{
|
||||
//如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换
|
||||
var nullableConverter = new System.ComponentModel.NullableConverter(newType);
|
||||
//将type转换为nullable对的基础基元类型
|
||||
newType = nullableConverter.UnderlyingType;
|
||||
}
|
||||
|
||||
item.SetValue(entity, Convert.ChangeType(row[item.Name], newType), null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
entities.Add(entity);
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将指定的集合转换成DataTable。
|
||||
/// </summary>
|
||||
/// <param name="list">将指定的集合。</param>
|
||||
/// <returns>返回转换后的DataTable。</returns>
|
||||
public static DataTable ToDataTable(IList list)
|
||||
{
|
||||
var table = new DataTable();
|
||||
if (list.Count > 0)
|
||||
{
|
||||
var properties = list[0].GetType().GetProperties();
|
||||
foreach (var pi in properties)
|
||||
{
|
||||
var pt = pi.PropertyType;
|
||||
if (pt.IsGenericType && pt.GetGenericTypeDefinition() == typeof(Nullable<>))
|
||||
{
|
||||
pt = pt.GetGenericArguments()[0];
|
||||
}
|
||||
|
||||
table.Columns.Add(new DataColumn(pi.Name, pt));
|
||||
}
|
||||
|
||||
for (var i = 0; i < list.Count; i++)
|
||||
{
|
||||
var tempList = new ArrayList();
|
||||
foreach (var pi in properties)
|
||||
{
|
||||
var obj = pi.GetValue(list[i], null);
|
||||
tempList.Add(obj);
|
||||
}
|
||||
|
||||
var array = tempList.ToArray();
|
||||
table.LoadDataRow(array, true);
|
||||
}
|
||||
}
|
||||
|
||||
return table;
|
||||
}
|
||||
|
||||
public static DataTable ToDataTable<T>(this List<T> list)
|
||||
{
|
||||
var table = new DataTable();
|
||||
//创建列头
|
||||
var properties = typeof(T).GetProperties();
|
||||
foreach (var pi in properties)
|
||||
{
|
||||
var pt = pi.PropertyType;
|
||||
if (pt.IsGenericType && pt.GetGenericTypeDefinition() == typeof(Nullable<>))
|
||||
{
|
||||
pt = pt.GetGenericArguments()[0];
|
||||
}
|
||||
|
||||
table.Columns.Add(new DataColumn(pi.Name, pt));
|
||||
}
|
||||
|
||||
//创建数据行
|
||||
if (list.Count > 0)
|
||||
{
|
||||
for (var i = 0; i < list.Count; i++)
|
||||
{
|
||||
var tempList = new ArrayList();
|
||||
foreach (var pi in properties)
|
||||
{
|
||||
var obj = pi.GetValue(list[i], null);
|
||||
tempList.Add(obj);
|
||||
}
|
||||
|
||||
var array = tempList.ToArray();
|
||||
table.LoadDataRow(array, true);
|
||||
}
|
||||
}
|
||||
|
||||
return table;
|
||||
}
|
||||
}
|
||||
85
Sai.Toolkit.Core/Extensions/ImageExtensions.cs
Normal file
85
Sai.Toolkit.Core/Extensions/ImageExtensions.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace Sai.Toolkit.Core.Extensions;
|
||||
|
||||
public static class ImageExtensions
|
||||
{
|
||||
public static void ExtractResource(string resourceName, string path)
|
||||
{
|
||||
using var manifestResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
|
||||
using Stream 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());
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
ms.Position = 0;
|
||||
|
||||
var result = new BitmapImage();
|
||||
|
||||
result.BeginInit();
|
||||
|
||||
result.StreamSource = ms;
|
||||
|
||||
result.CacheOption = BitmapCacheOption.OnLoad;
|
||||
|
||||
result.EndInit();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static BitmapSource ToBitmapSource(this Bitmap bitmap)
|
||||
{
|
||||
if (bitmap == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return 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)
|
||||
);
|
||||
}
|
||||
}
|
||||
194
Sai.Toolkit.Core/Extensions/WindowsThumbnailProvider.cs
Normal file
194
Sai.Toolkit.Core/Extensions/WindowsThumbnailProvider.cs
Normal file
@@ -0,0 +1,194 @@
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Sai.Toolkit.Core.Extensions;
|
||||
|
||||
public enum ThumbnailOptions
|
||||
{
|
||||
None = 0x00,
|
||||
BiggerSizeOk = 0x01,
|
||||
InMemoryOnly = 0x02,
|
||||
IconOnly = 0x04,
|
||||
ThumbnailOnly = 0x08,
|
||||
InCacheOnly = 0x10
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取Windows缩略图
|
||||
/// </summary>
|
||||
public class WindowsThumbnailProvider
|
||||
{
|
||||
private const string ShellItem2Guid = "7E9FB0D3-919F-4307-AB2E-9B1860310C93";
|
||||
|
||||
public static Bitmap CreateAlphaBitmap(Bitmap srcBitmap, PixelFormat targetPixelFormat)
|
||||
{
|
||||
var result = new Bitmap(srcBitmap.Width, srcBitmap.Height, targetPixelFormat);
|
||||
|
||||
var bmpBounds = new System.Drawing.Rectangle(0, 0, srcBitmap.Width, srcBitmap.Height);
|
||||
|
||||
var srcData = srcBitmap.LockBits(bmpBounds, ImageLockMode.ReadOnly, srcBitmap.PixelFormat);
|
||||
|
||||
var isAlplaBitmap = false;
|
||||
|
||||
try
|
||||
{
|
||||
for (var y = 0; y <= srcData.Height - 1; y++)
|
||||
{
|
||||
for (var x = 0; x <= srcData.Width - 1; x++)
|
||||
{
|
||||
var pixelColor = System.Drawing.Color.FromArgb(Marshal.ReadInt32(srcData.Scan0, (srcData.Stride * y) + (4 * x)));
|
||||
|
||||
if ((pixelColor.A > 0) & (pixelColor.A < 255))
|
||||
{
|
||||
isAlplaBitmap = true;
|
||||
}
|
||||
|
||||
result.SetPixel(x, y, pixelColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
srcBitmap.UnlockBits(srcData);
|
||||
}
|
||||
|
||||
return isAlplaBitmap ? result : srcBitmap;
|
||||
}
|
||||
|
||||
public static Bitmap GetBitmapFromHBitmap(IntPtr nativeHBitmap)
|
||||
{
|
||||
var bmp = Image.FromHbitmap(nativeHBitmap);
|
||||
|
||||
return Image.GetPixelFormatSize(bmp.PixelFormat) < 32 ? bmp : CreateAlphaBitmap(bmp, PixelFormat.Format32bppArgb);
|
||||
}
|
||||
|
||||
public static Bitmap GetThumbnail(string fileName, int width, int height, ThumbnailOptions options)
|
||||
{
|
||||
var hBitmap = GetHBitmap(Path.GetFullPath(fileName), width, height, options);
|
||||
|
||||
try
|
||||
{
|
||||
// return a System.Drawing.Bitmap from the hBitmap
|
||||
return GetBitmapFromHBitmap(hBitmap);
|
||||
}
|
||||
finally
|
||||
{
|
||||
// delete HBitmap to avoid memory leaks
|
||||
DeleteObject(hBitmap);
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport("gdi32.dll")]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
internal static extern bool DeleteObject(IntPtr hObject);
|
||||
|
||||
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
||||
internal static extern int SHCreateItemFromParsingName(
|
||||
[MarshalAs(UnmanagedType.LPWStr)] string path,
|
||||
// The following parameter is not used - binding context.
|
||||
IntPtr pbc,
|
||||
ref Guid riid,
|
||||
[MarshalAs(UnmanagedType.Interface)] out IShellItem shellItem
|
||||
);
|
||||
|
||||
private static IntPtr GetHBitmap(string fileName, int width, int height, ThumbnailOptions options)
|
||||
{
|
||||
var shellItem2Guid = new Guid(ShellItem2Guid);
|
||||
var retCode = SHCreateItemFromParsingName(fileName, IntPtr.Zero, ref shellItem2Guid, out var nativeShellItem);
|
||||
|
||||
if (retCode != 0)
|
||||
{
|
||||
throw Marshal.GetExceptionForHR(retCode);
|
||||
}
|
||||
|
||||
var nativeSize = new NativeSize { Width = width, Height = height };
|
||||
|
||||
var hr = ((IShellItemImageFactory)nativeShellItem).GetImage(nativeSize, options, out var hBitmap);
|
||||
|
||||
Marshal.ReleaseComObject(nativeShellItem);
|
||||
|
||||
return hr == HResult.Ok ? hBitmap : throw Marshal.GetExceptionForHR((int)hr);
|
||||
}
|
||||
|
||||
internal enum HResult
|
||||
{
|
||||
Ok = 0x0000,
|
||||
False = 0x0001,
|
||||
InvalidArguments = unchecked((int)0x80070057),
|
||||
OutOfMemory = unchecked((int)0x8007000E),
|
||||
NoInterface = unchecked((int)0x80004002),
|
||||
Fail = unchecked((int)0x80004005),
|
||||
ElementNotFound = unchecked((int)0x80070490),
|
||||
TypeElementNotFound = unchecked((int)0x8002802B),
|
||||
NoObject = unchecked((int)0x800401E5),
|
||||
Win32ErrorCanceled = 1223,
|
||||
Canceled = unchecked((int)0x800704C7),
|
||||
ResourceInUse = unchecked((int)0x800700AA),
|
||||
AccessDenied = unchecked((int)0x80030005)
|
||||
}
|
||||
|
||||
[ComImport]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
[Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe")]
|
||||
internal interface IShellItem
|
||||
{
|
||||
void BindToHandler(IntPtr pbc, [MarshalAs(UnmanagedType.LPStruct)] Guid bhid, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, out IntPtr ppv);
|
||||
|
||||
void Compare(IShellItem psi, uint hint, out int piOrder);
|
||||
|
||||
void GetAttributes(uint sfgaoMask, out uint psfgaoAttribs);
|
||||
|
||||
void GetDisplayName(SIGDN sigdnName, out IntPtr ppszName);
|
||||
|
||||
void GetParent(out IShellItem ppsi);
|
||||
}
|
||||
|
||||
[ComImport]
|
||||
[Guid("bcc18b79-ba16-442f-80c4-8a59c30c463b")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
internal interface IShellItemImageFactory
|
||||
{
|
||||
[PreserveSig]
|
||||
HResult GetImage([In] [MarshalAs(UnmanagedType.Struct)] NativeSize size, [In] ThumbnailOptions flags, [Out] out IntPtr phbm);
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct NativeSize
|
||||
{
|
||||
private int width;
|
||||
private int height;
|
||||
|
||||
public int Width
|
||||
{
|
||||
set => width = value;
|
||||
}
|
||||
|
||||
public int Height
|
||||
{
|
||||
set => height = value;
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct RGBQUAD
|
||||
{
|
||||
public byte rgbBlue;
|
||||
public byte rgbGreen;
|
||||
public byte rgbRed;
|
||||
public byte rgbReserved;
|
||||
}
|
||||
|
||||
internal enum SIGDN : uint
|
||||
{
|
||||
NORMALDISPLAY = 0,
|
||||
PARENTRELATIVEPARSING = 0x80018001,
|
||||
PARENTRELATIVEFORADDRESSBAR = 0x8001c001,
|
||||
DESKTOPABSOLUTEPARSING = 0x80028000,
|
||||
PARENTRELATIVEEDITING = 0x80031001,
|
||||
DESKTOPABSOLUTEEDITING = 0x8004c000,
|
||||
FILESYSPATH = 0x80058000,
|
||||
URL = 0x80068000
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user