Files
KJD_WebService/TEMUKJD/ToolForms/ImgToolForm.cs
T
2026-03-02 01:28:14 +08:00

125 lines
4.6 KiB
C#

using NaiBa.Util.Extension;
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 TemuKJD.BaseForms;
namespace TemuKJD.ToolForms
{
public partial class ImgToolForm : Form
{
public ImgToolForm()
{
InitializeComponent();
}
Image baseImg;
Image newImg;
private void btnMake_Click(object sender, EventArgs e)
{
if(baseImg == null)
{
BaseMsgForm.ShowError("请先选择图片");
return;
}
var newImg = new Bitmap(baseImg.Width, baseImg.Height);
using (Graphics g = Graphics.FromImage(newImg))
{
//g.Clear(Color.White);
g.DrawImage(baseImg, new Rectangle(0, 0, newImg.Width, newImg.Height), 0, 0, baseImg.Width, baseImg.Height, GraphicsUnit.Pixel);
g.FillRectangle(Brushes.White, txtX.Text.Trim().ParseToInt(), txtY.Text.Trim().ParseToInt(), 500, 120);
//Font font = new Font("宋体", txtSize.Text.Trim().ParseToInt(), FontStyle.Bold);
Font font = new Font("宋体", txtSize.Text.Trim().ParseToInt(), FontStyle.Regular);
//Font font = new Font("微软雅黑", 15, FontStyle.Regular);
Brush textBrush = Brushes.Black;
textBrush = new SolidBrush(Color.FromArgb(0,0,0));
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
RectangleF rectF = new RectangleF(txtX.Text.Trim().ParseToInt(), txtY.Text.Trim().ParseToInt(), baseImg.Width - 80, baseImg.Height - 80);
if (!string.IsNullOrWhiteSpace(textBox1.Text.Trim()))
g.DrawString(textBox1.Text.Trim(), font, textBrush, rectF);
}
this.pbImage.Image = newImg;
}
private void btnSave_Click(object sender, EventArgs e)
{
if (this.pbImage.Image == null)
{
BaseMsgForm.ShowError("图片为空!");
return;
}
string fileName = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + ".jpg";
if (!string.IsNullOrEmpty(this.pbImage.ImageLocation))
{
fileName = this.pbImage.ImageLocation.Replace("://", ".").Replace('/', '.');
}
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "JPeg Image|*.jpg;*.jpeg";
sfd.Title = "保存图片";
sfd.FileName = fileName;
if (sfd.ShowDialog() == DialogResult.OK)
{
try
{
pbImage.Image.Save(sfd.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception ex)
{
BaseMsgForm.ShowError(ex.Message);
}
}
}
private void btnSelectImg_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = false;
// 设置过滤条件,只显示文本文件
openFileDialog.Filter = "图片文件 (*.jpeg;*.jpg;*.png)|*.jpeg;*.jpg;*.png|All files (*.*)|*.*";
// 显示对话框并获取结果
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
string filePath = openFileDialog.FileName;
Image image = Image.FromFile(filePath);
string imgBase64 = "";
using (MemoryStream memoryStream = new MemoryStream())
{
image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imageBytes = memoryStream.ToArray();
imgBase64 = Convert.ToBase64String(imageBytes);
}
//string img_md5 = BaseClass.GetMd5(imgBase64);
if (this.pbImage.Tag.ParseToString() != imgBase64)
{
this.pbImage.Tag = imgBase64;
this.pbImage.ImageLocation = "";
this.pbImage.Image = image;
this.baseImg = image;
}
}
catch (Exception ex)
{
BaseMsgForm.ShowError("打开自定义sku图片失败 " + ex.Message);
return;
}
}
}
}
}