SuperDesign/Source/开发辅助工具/Tools/FrmImageToIcon.cs
zilinsoft 993f1ca1a9 ### 2024-12-20 星期五更新
------
#### SuperDesign    V3.0.2412.2001
- *.[新增]新增程序更新日志设置和自动发布功能。
- *.[修复]修复Post数据格式不正确时双击文本框会导致软件闪退的BUG。
2024-12-20 08:15:19 +08:00

134 lines
4.5 KiB
C#

using HttpUpload.Core;
using ryCommon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace .Tools
{
public partial class FrmImageToIcon : DockContent
{
public FrmImageToIcon()
{
InitializeComponent();
RyFiles.AddDropDrag(TxtImagePath.Handle).ElevatedDragDrop += FrmImageToIcon_ElevatedDragDrop; ;
}
private void FrmImageToIcon_ElevatedDragDrop(object sender, ElevatedDragDropArgs e)
{
LoadImage(e.Files[0]);
}
private void LoadImage(string path)
{
var ext = HttpUpload.Core.ImgExt.CheckImgFile(path);
Image img = null;
if (ext == HttpUpload.Core.FileExtension.WebP)
{
try
{
var stream = HttpUpload.Core.ImgExt.FileToStream(path);
byte[] array = new byte[stream.Length];
stream.Seek(0L, SeekOrigin.Begin);
stream.Read(array, 0, array.Length);
if (array.Length > 3)
{
using (WebPWrapper.WebP webP = new WebPWrapper.WebP())
{
var bitmap = webP.Decode(array);
img = bitmap;
}
}
}
catch { img = null; }
}
else
{ img = RyImage.LoadPic(path); }
superPictureBox1.Image = img;
TxtImagePath.Text = path;
BtnConvert.Text = "立即转换";
}
private void BtnBrowser_Click(object sender, EventArgs e)
{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
LoadImage(openFileDialog1.FileName);
}
}
private void BtnConvert_Click(object sender, EventArgs e)
{
var path = TxtImagePath.Text;
var ext = HttpUpload.Core.ImgExt.CheckImgFile(path);
Image img = RyImage.LoadPic(path);
if(ext== HttpUpload.Core.FileExtension.WebP)
{
try
{
var stream = HttpUpload.Core.ImgExt.FileToStream(path);
byte[] array = new byte[stream.Length];
stream.Seek(0L, SeekOrigin.Begin);
stream.Read(array, 0, array.Length);
if (array.Length > 3)
{
using (WebPWrapper.WebP webP = new WebPWrapper.WebP())
{
var bitmap = webP.Decode(array);
img = bitmap;
}
}
}
catch { img = null; }
}
if (img == null) {
MessageBox.Show("无效的图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (RbSize32.Checked)
{
img = img.ReSize(32,32, ResizeMode.HeightFirst);
}
else if (RbSize128.Checked)
{
img = img.ReSize(128, 128, ResizeMode.HeightFirst);
}
else if (RbSize256.Checked)
{
img = img.ReSize(256, 256, ResizeMode.HeightFirst);
}
BtnConvert.Enabled = false;
var icon= RyImage.ConvertToIcon(img, true);
if (icon == null)
{
superPictureBox1.Image = null;
BtnConvert.Text = "转换失败";
BtnConvert.Enabled = true;
return;
}
//superPictureBox1.Image = icon.ToBitmap();
BtnConvert.Text = "转换成功";
var icon_path = System.IO.Path.GetDirectoryName(path) + "\\"+System.IO.Path.GetFileNameWithoutExtension(path) + ".ico";
try
{
FileStream fileStream = new FileStream(icon_path, FileMode.Create);
icon.Save(fileStream);
fileStream.Close();
RyFiles.OpenFolderGotoFile(icon_path);
}
catch
{
BtnConvert.Text = "保存失败";
}
BtnConvert.Enabled = true;
}
}
}