初始化上传
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user