初始化上传
This commit is contained in:
125
常用工具集/ViewModels/03图片相关/GIF分割ViewModel.cs
Normal file
125
常用工具集/ViewModels/03图片相关/GIF分割ViewModel.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
using Avalonia;
|
||||
using Avalonia.Platform.Storage;
|
||||
using Avalonia.Threading;
|
||||
using Ursa.Controls;
|
||||
using 常用工具集.Base;
|
||||
|
||||
namespace 常用工具集.ViewModel._03图片相关
|
||||
{
|
||||
public class GIF分割ViewModel : ViewModelBase
|
||||
{
|
||||
|
||||
public List<string> Paths { get; set; } = new List<string>();
|
||||
public string ButtonName { get; set; } = "导出文件";
|
||||
public string Path1 { get; set; } = "";
|
||||
public string Path2 { get; set; } = "";
|
||||
public DelegateCommand SelectGifCmd { get; set; }
|
||||
public DelegateCommand SelectExportPathCmd { get; set; }
|
||||
|
||||
public DelegateCommand ExportCmd { get; set; }
|
||||
|
||||
public GIF分割ViewModel()
|
||||
{
|
||||
SelectGifCmd = new DelegateCommand(SelectGifCmdFunc);
|
||||
SelectExportPathCmd = new DelegateCommand(SelectExportPathCmdFunc);
|
||||
ExportCmd = new DelegateCommand(ExportCmdFunc);
|
||||
}
|
||||
|
||||
|
||||
private async void SelectGifCmdFunc(object obj)
|
||||
{
|
||||
var sp = GlobalValues.StorageProvider;
|
||||
if (sp is null) return;
|
||||
FilePickerOpenOptions options = new FilePickerOpenOptions();
|
||||
options.Title = "选择GIF文件";
|
||||
options.AllowMultiple = false;
|
||||
options.FileTypeFilter = [
|
||||
new FilePickerFileType("GIF文件")
|
||||
{
|
||||
Patterns = new[] { "*.gif" },
|
||||
AppleUniformTypeIdentifiers = new[] { "public.gif" },
|
||||
MimeTypes = new[] { "image/gif" }
|
||||
}];
|
||||
var result = await sp.OpenFilePickerAsync(options);
|
||||
if (result == null || result.Count == 0) return;
|
||||
Path1 = result.FirstOrDefault()?.Path.LocalPath;
|
||||
}
|
||||
|
||||
private async void SelectExportPathCmdFunc(object obj)
|
||||
{
|
||||
var sp = GlobalValues.StorageProvider;
|
||||
if (sp is null) return;
|
||||
var result = await sp.OpenFolderPickerAsync(new FolderPickerOpenOptions()
|
||||
{
|
||||
Title = "Select Folder",
|
||||
AllowMultiple = false,
|
||||
});
|
||||
if (result == null || result.Count == 0) return;
|
||||
Path2 = result.FirstOrDefault()?.Path.LocalPath;
|
||||
}
|
||||
|
||||
private void ExportCmdFunc(object obj)
|
||||
{
|
||||
string gifPath = Path1.Trim();
|
||||
string folderPaht = Path2.Trim();
|
||||
if (string.IsNullOrEmpty(gifPath) || string.IsNullOrEmpty(folderPaht))
|
||||
{
|
||||
MessageBox.ShowAsync("请选择gif路径或者导出路径!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (ButtonName == "导出文件")
|
||||
{
|
||||
ButtonName = "导出中...";
|
||||
//开启线程导出图片
|
||||
Thread thread = new Thread(Export);
|
||||
thread.Start();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.ShowAsync("正在导出中!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 导出jpg图片
|
||||
/// </summary>
|
||||
private void Export()
|
||||
{
|
||||
string gifPath = Path1.Trim();
|
||||
string folderPaht = Path2.Trim();
|
||||
Image img = Image.FromFile(gifPath);
|
||||
FrameDimension fd = new FrameDimension(img.FrameDimensionsList[0]);
|
||||
//获取gif帧的数量
|
||||
int count = img.GetFrameCount(fd);
|
||||
//遍历保存图片
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
img.SelectActiveFrame(fd, i);
|
||||
string imgPath = folderPaht + "\\frame" + (i + 1) + ".png";
|
||||
//判断同名文件是否存在
|
||||
if (File.Exists(imgPath))
|
||||
{
|
||||
File.Delete(imgPath);
|
||||
}
|
||||
//保存图片 一定要设置格式 否则保存出来的图片都是一张图片
|
||||
img.Save(imgPath, ImageFormat.Png);
|
||||
}
|
||||
Dispatcher.UIThread.Invoke(() =>
|
||||
{
|
||||
GlobalValues.Success("文件导出成功!");
|
||||
ButtonName = "导出文件";
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
89
常用工具集/ViewModels/03图片相关/二维码条形码解析ViewModel.cs
Normal file
89
常用工具集/ViewModels/03图片相关/二维码条形码解析ViewModel.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using Avalonia.Media.Imaging;
|
||||
using Avalonia.Platform.Storage;
|
||||
using SkiaSharp;
|
||||
using ZXing;
|
||||
using ZXing.Common;
|
||||
using ZXing.SkiaSharp;
|
||||
using 常用工具集.Base;
|
||||
|
||||
namespace 常用工具集.ViewModel._03图片相关
|
||||
{
|
||||
public class 二维码条形码解析ViewModel : ViewModelBase
|
||||
{
|
||||
public string ImagePath { get; set; } = "";
|
||||
public string Result { get; set; } = "";
|
||||
|
||||
public DelegateCommand SelectPathCmd { get; set; }
|
||||
public DelegateCommand GetResultCmd { get; set; }
|
||||
|
||||
public 二维码条形码解析ViewModel()
|
||||
{
|
||||
SelectPathCmd = new DelegateCommand(SelectPathCmdFunc);
|
||||
GetResultCmd = new DelegateCommand(GetResultCmdFunc);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 选择文件
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
private async void SelectPathCmdFunc(object obj)
|
||||
{
|
||||
var sp = GlobalValues.StorageProvider;
|
||||
if (sp is null) return;
|
||||
FilePickerOpenOptions options = new FilePickerOpenOptions();
|
||||
options.Title = "选择二维码图片";
|
||||
options.AllowMultiple = false;
|
||||
options.FileTypeFilter = [
|
||||
FilePickerFileTypes.ImageAll
|
||||
];
|
||||
var result = await sp.OpenFilePickerAsync(options);
|
||||
if (result == null || result.Count == 0) return;
|
||||
ImagePath = result.FirstOrDefault()?.Path.LocalPath;
|
||||
}
|
||||
|
||||
|
||||
private void GetResultCmdFunc(object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
MultiFormatReader formatReader = new MultiFormatReader();
|
||||
Dictionary<DecodeHintType, object> hints = new Dictionary<DecodeHintType, object>();
|
||||
hints.Add(DecodeHintType.CHARACTER_SET, Encoding.UTF8.HeaderName);
|
||||
// 优化精度
|
||||
hints.Add(DecodeHintType.TRY_HARDER, true);
|
||||
// 复杂模式,开启PURE_BARCODE模式
|
||||
hints.Add(DecodeHintType.PURE_BARCODE, true);
|
||||
formatReader.Hints = hints;
|
||||
SKBitmap bitmap = SKBitmap.Decode(ImagePath);
|
||||
LuminanceSource source = new SKBitmapLuminanceSource(bitmap);
|
||||
Result result = formatReader.decode(new BinaryBitmap(new HybridBinarizer(source)));
|
||||
if (null == result)
|
||||
{
|
||||
result = formatReader.decode(new BinaryBitmap(new GlobalHistogramBinarizer(source)));
|
||||
}
|
||||
bitmap.Dispose();
|
||||
if (result != null)
|
||||
{
|
||||
Result = result.Text; // 显示解析出的一维码文本内容
|
||||
}
|
||||
else
|
||||
{
|
||||
Result = "未能解析条码";
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Result = ex.Message;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
118
常用工具集/ViewModels/03图片相关/图片转ICOViewModel.cs
Normal file
118
常用工具集/ViewModels/03图片相关/图片转ICOViewModel.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using System;
|
||||
using 常用工具集.Base;
|
||||
using Avalonia.Media.Imaging;
|
||||
using SkiaSharp;
|
||||
using Ursa.Controls;
|
||||
using Avalonia.Platform.Storage;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
using ImageMagick;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
namespace 常用工具集.ViewModel._03图片相关
|
||||
{
|
||||
public class 图片转ICOViewModel : ViewModelBase
|
||||
{
|
||||
public string ImagePath { get; set; }
|
||||
|
||||
public Bitmap ImageSource { get; set; }
|
||||
|
||||
public DelegateCommand SelectImageCmd { get; set; }
|
||||
|
||||
public DelegateCommand ExportCmd { get; set; }
|
||||
|
||||
public 图片转ICOViewModel()
|
||||
{
|
||||
SelectImageCmd = new DelegateCommand(SelectImageCmdFunc);
|
||||
ExportCmd = new DelegateCommand(ExportCmdFunc);
|
||||
}
|
||||
|
||||
private async void SelectImageCmdFunc(object obj)
|
||||
{
|
||||
|
||||
var sp = GlobalValues.StorageProvider;
|
||||
if (sp is null) return;
|
||||
FilePickerOpenOptions options = new FilePickerOpenOptions();
|
||||
options.Title = "选择图片";
|
||||
options.AllowMultiple = false;
|
||||
options.FileTypeFilter = [FilePickerFileTypes.ImageAll];
|
||||
var result = await sp.OpenFilePickerAsync(options);
|
||||
if (result == null || result.Count == 0) return;
|
||||
ImagePath = result.FirstOrDefault()?.Path.LocalPath;
|
||||
ImageSource = new Bitmap(ImagePath);
|
||||
}
|
||||
|
||||
private async void ExportCmdFunc(object obj)
|
||||
{
|
||||
int size = Convert.ToInt32(obj);
|
||||
SKBitmap sbitmap = SKBitmap.Decode(ImagePath);
|
||||
SKBitmap bitmap = ResizeImage(sbitmap, size, size);
|
||||
//Icon icon = ConvertToIcon(bitmap);
|
||||
//IconDir icon = new IconDir();
|
||||
//icon.AddImage(bitmap, bitmap.Width, bitmap.Height);
|
||||
|
||||
|
||||
var sp = GlobalValues.StorageProvider;
|
||||
if (sp is null) return;
|
||||
var result = await sp.OpenFolderPickerAsync(new FolderPickerOpenOptions()
|
||||
{
|
||||
Title = "请选择要生成的目录",
|
||||
AllowMultiple = false,
|
||||
});
|
||||
if (result == null || result.Count == 0)
|
||||
{
|
||||
await MessageBox.ShowAsync("文件夹路径不能为空");
|
||||
return;
|
||||
}
|
||||
string filePath = result.FirstOrDefault()?.Path.LocalPath;
|
||||
if (!filePath.EndsWith("/"))
|
||||
{
|
||||
filePath += "/";
|
||||
}
|
||||
string fileName = "favicon_" + size + "_" + DateTime.Now.ToString("yyyyMMddHHmmss");
|
||||
byte[] bytes = null;
|
||||
using (MemoryStream stream = new MemoryStream())
|
||||
{
|
||||
SKData data = bitmap.Encode(SKEncodedImageFormat.Png, 100);
|
||||
data.SaveTo(stream);
|
||||
bytes = stream.ToArray();
|
||||
}
|
||||
using (MemoryStream stream = new MemoryStream(bytes))
|
||||
{
|
||||
using (MagickImage image = new MagickImage(stream))
|
||||
{
|
||||
image.Format = MagickFormat.Ico;
|
||||
image.Write(filePath + fileName + ".ico");
|
||||
}
|
||||
}
|
||||
await MessageBox.ShowAsync("生成成功");
|
||||
//icon.Dispose();
|
||||
bitmap.Dispose();
|
||||
sbitmap.Dispose();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 图片缩放
|
||||
/// </summary>
|
||||
/// <param name="bmp"></param>
|
||||
/// <param name="newW"></param>
|
||||
/// <param name="newH"></param>
|
||||
/// <returns></returns>
|
||||
public static SKBitmap ResizeImage(SKBitmap bmp, int newW, int newH)
|
||||
{
|
||||
try
|
||||
{
|
||||
SKBitmap b = new SKBitmap(newW, newH, SKColorType.Argb4444, SKAlphaType.Opaque);
|
||||
SKCanvas g = new SKCanvas(b);
|
||||
g.DrawBitmap(bmp, new SKRect(0, 0, newW, newH));
|
||||
g.Dispose();
|
||||
return b;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
25
常用工具集/ViewModels/03图片相关/色卡包ViewModel.cs
Normal file
25
常用工具集/ViewModels/03图片相关/色卡包ViewModel.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using Avalonia.Media.Imaging;
|
||||
using Avalonia.Platform;
|
||||
using 常用工具集.Base;
|
||||
|
||||
namespace 常用工具集.ViewModel._03图片相关
|
||||
{
|
||||
public class 色卡包ViewModel : ViewModelBase
|
||||
{
|
||||
public Bitmap ImageSource { get; set; } = null;
|
||||
public DelegateCommand ButtonCmd { get; set; }
|
||||
public 色卡包ViewModel()
|
||||
{
|
||||
ButtonCmd = new DelegateCommand(ButtonCmdFunc);
|
||||
}
|
||||
|
||||
private void ButtonCmdFunc(object obj)
|
||||
{
|
||||
string fileName = obj.ToString();
|
||||
// 获取Pack URI
|
||||
string packUri = $"avares://常用工具集/Assets/ColorBag/{fileName}.gif";
|
||||
ImageSource = new Bitmap(AssetLoader.Open(new Uri(packUri)));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user