2022-09-30 09:40:42 -07:00

409 lines
12 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using SixLabors.ImageSharp.Processing;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Church.Net.Utility
{
public static class Format
{
public static string Get33BaseGuid()
{
byte[] gb = Guid.NewGuid().ToByteArray();
ulong l = BitConverter.ToUInt64(gb, 0);
return Parse.ULongToBase33(l);
}
private static Random rng = new Random();
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
public static class StringHelper
{
public static string Get33BaseGuid()
{
byte[] gb = Guid.NewGuid().ToByteArray();
ulong l = BitConverter.ToUInt64(gb, 0);
return Parse.ULongToBase33(l);
}
}
public static class Parse
{
#region "列舉型別宣告(empty)"
#endregion
#region "私有常數(empty)"
#endregion
#region "私有變數(empty)"
#endregion
#region "建構函式/解構函式"
#endregion
#region "介面實作(empty)"
#endregion
#region "公用方法和屬性"
/// <summary>
/// 嘗試轉換為整數型別若轉換失敗則回傳0
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static int ToInt(string input)
{
return ToInt(input, 0);
}
/// <summary>
/// 嘗試轉換為整數型別,若轉換失敗則回傳 初始值
/// </summary>
/// <param name="input"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static int ToInt(string input, int defaultValue)
{
int temp = 0;
if (!int.TryParse(input, out temp))
{
temp = defaultValue;
}
return temp;
}
public static bool ToBool(string input, bool defaultValue = false)
{
bool temp = defaultValue;
if (!bool.TryParse(input, out temp))
{
temp = defaultValue;
}
return temp;
}
#region Hex
public static string ToHex(int value)
{
value = value + 10000;
// Convert integer 182 as a hex in a string variable
string hexValue = value.ToString("X");
return hexValue;
}
public static int HexToInt(string value)
{
// Convert the hex string back to the number
return int.Parse(value, System.Globalization.NumberStyles.HexNumber) - 10000;
}
#endregion
#region 33
private const string CharList = "0123456789ABCDEFGHJKLMNPQRSTUVWXY";
public static String ULongToBase33(int input)
{
return ULongToBase33((ulong)input);
}
public static String ULongToBase33(long input)
{
return ULongToBase33((ulong)input);
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <param name="minLength"></param>
/// <returns></returns>
public static String ULongToBase33(ulong input)
{
if (input < 0) throw new ArgumentOutOfRangeException("input", input, "33 進位僅可計算正數");
//ulong maxValue = GetLongMax(input);
//ulong currentValue = input;
char[] clistarr = CharList.ToCharArray();
var result = new Stack<char>();
while (input != 0)
{
result.Push(clistarr[input % 33]);
input /= 33;
}
string encodeResult = new string(result.ToArray());
//if (currentValue != maxValue)
//{
// string max32Value = ULongToBase32(maxValue);
// while (encodeResult.Length < max32Value.Length)
// {
// encodeResult = "Y" + encodeResult;
// }
//}
return encodeResult;
}
/// <summary>
///
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static ulong Base33ToULong(string input)
{
ulong result = 0;
try
{
//input = input.Replace("Y", "");
var reversed = input.ToUpper().Reverse();
int pos = 0;
foreach (char c in reversed)
{
result += (ulong)CharList.IndexOf(c) * (ulong)Math.Pow(33, pos);
pos++;
}
}
catch (Exception e)
{
}
return result;
}
#endregion
#endregion
#region "內部方法和屬性(empty)"
#endregion
#region "私用方法和屬性(empty)"
#endregion
#region "Form Event(empty)"
#endregion
}
public static class EnumHelper<T>
{
public static IList<T> GetValues()
{
var enumValues = new List<T>();
foreach (FieldInfo fi in typeof(T).GetFields(BindingFlags.Static | BindingFlags.Public))
{
enumValues.Add((T)Enum.Parse(typeof(T), fi.Name, false));
}
return enumValues;
}
public static T Parse(string value)
{
return (T)Enum.Parse(typeof(T), value, true);
}
public static IList<string> GetNames()
{
return System.Enum.GetNames(typeof(T));
//return T.GetFields(BindingFlags.Static | BindingFlags.Public).Select(fi => fi.Name).ToList();
}
}
public static class ImageHelper
{
public static Bitmap CreateThumbnail(string lcFilename, int lnWidth, int lnHeight)
{
System.Drawing.Bitmap bmpOut = null;
try
{
Bitmap loBMP = new Bitmap(lcFilename);
ImageFormat loFormat = loBMP.RawFormat;
decimal lnRatio;
int lnNewWidth = 0;
int lnNewHeight = 0;
if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
return loBMP;
if (loBMP.Width > loBMP.Height)
{
lnRatio = (decimal)lnWidth / loBMP.Width;
lnNewWidth = lnWidth;
decimal lnTemp = loBMP.Height * lnRatio;
lnNewHeight = (int)lnTemp;
}
else
{
lnRatio = (decimal)lnHeight / loBMP.Height;
lnNewHeight = lnHeight;
decimal lnTemp = loBMP.Width * lnRatio;
lnNewWidth = (int)lnTemp;
}
bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
loBMP.Dispose();
}
catch
{
return null;
}
return bmpOut;
}
public static Bitmap SaveThumbnail(Bitmap image, string saveTo, int lnWidth, int lnHeight)
{
System.Drawing.Bitmap bmpOut = null;
try
{
Bitmap loBMP = image;
ImageFormat loFormat = loBMP.RawFormat;
decimal lnRatio;
int lnNewWidth = loBMP.Width;
int lnNewHeight = loBMP.Height;
if (loBMP.Width > lnWidth || loBMP.Height > lnHeight)
{
if (loBMP.Width > loBMP.Height)
{
lnRatio = (decimal)lnWidth / loBMP.Width;
lnNewWidth = lnWidth;
decimal lnTemp = loBMP.Height * lnRatio;
lnNewHeight = (int)lnTemp;
}
else
{
lnRatio = (decimal)lnHeight / loBMP.Height;
lnNewHeight = lnHeight;
decimal lnTemp = loBMP.Width * lnRatio;
lnNewWidth = (int)lnTemp;
}
}
bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
bmpOut.Save(saveTo,ImageFormat.Jpeg);
bmpOut.Dispose();
loBMP.Dispose();
}
catch
{
return null;
}
return bmpOut;
}
public static Bitmap Resize(this Bitmap bmp, int width, int height)
{
Bitmap result = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(result))
{
g.DrawImage(bmp, 0, 0, width, height);
}
return result;
}
public static Bitmap Scalling(this Bitmap bmp, int percentage)
{
return bmp.Resize(bmp.Width * percentage / 100, bmp.Height * percentage / 100);
}
public static void Scalling(this SixLabors.ImageSharp.Image image, int percentage)
{
var scale = (double)percentage/100;
var scaleWidth = (int)(image.Width * scale);
var scaleHeight = (int)(image.Height * scale);
image.Mutate(o => o.Resize(new SixLabors.ImageSharp.Size(scaleWidth, scaleHeight)));
}
public static Bitmap Superimpose(Bitmap largeBmp, Bitmap smallBmp, int? x = null, int? y = null)
{
Graphics g = Graphics.FromImage(largeBmp);
g.CompositingMode = CompositingMode.SourceOver;
smallBmp.MakeTransparent();
int margin = 5;
if (!x.HasValue)
{
x = largeBmp.Width - smallBmp.Width - margin;
}
if (!y.HasValue)
{
y = largeBmp.Height - smallBmp.Height - margin;
}
var scale = 0.8;
var scaleWidth = (int)(smallBmp.Width * scale);
var scaleHeight = (int)(smallBmp.Height * scale);
g.DrawImage(smallBmp, new Rectangle(x.Value, y.Value, scaleWidth, scaleHeight));
return largeBmp;
}
}
}