using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TBAppraisalTools.Properties;
using UI.Framework.Forms;
using static TBAppraisalTools.Enums;
using Api.Framework.Tools;
using System.IO;
namespace TBAppraisalTools.Forms
{
public partial class EditPhotoForm : BaseForm
{
public TbConfig tbConfig = null;
///
/// 增加/编辑图片
///
/// 为null时,添加
public EditPhotoForm(TbConfig tbConfig = null)
{
InitializeComponent();
this.Text = tbConfig == null ? Resources.AddPhotoTitle : Resources.EditPhotoTitle;
this.tbConfig = tbConfig;
}
///
/// 窗体加载
///
///
///
private void EditPhotoForm_Load(object sender, EventArgs e)
{
ImageInfo imageInfo = null;
if (tbConfig != null)//编辑图片信息
{
imageInfo = tbConfig.C_Tag as ImageInfo;
radioButton1.Checked = imageInfo.Shape;
radioButton2.Checked = !imageInfo.Shape;
if (imageInfo.Varible.Contains(EditImageType.其他图片.ToString())) tBox_path.Text = imageInfo.ImagePath;
numericUpDown_Width.Value = tbConfig.C_Size.Width;
numericUpDown_Height.Value = tbConfig.C_Size.Height;
}
int i = -1;//定义这个是为了选中之后,带动触发事件
foreach (EditImageType item in Enum.GetValues(typeof(EditImageType)))
{
i++;
cBox_Variable.Items.Add("#" + item.ToString());
if (imageInfo != null && "#" + item.ToString() == imageInfo.Varible)
cBox_Variable.SelectedIndex = i;
}
if (cBox_Variable.SelectedIndex == -1) cBox_Variable.SelectedIndex = 0;
}
///
/// 图片类型下标改变时间
///
///
///
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (cBox_Variable.SelectedItem.ToString().Contains(EditImageType.其他图片.ToString()))
btn_LocalImage.Enabled = tBox_path.Enabled = true;
else
btn_LocalImage.Enabled = tBox_path.Enabled = false;
}
catch (Exception ex)
{
ShowError(ex);
}
}
///
/// 上传图片
///
///
///
private void btn_LocalImage_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "选择要上传的图片";
fdlg.InitialDirectory = @"c:\";
fdlg.Filter = "jpg,png,bmp,gif|*.jpg;*.png;*.bmp;*.gif";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
tBox_path.Text = fdlg.FileName;
fdlg.Dispose();
}
catch (Exception ex)
{
ShowError(ex);
}
}
private void simpleButton2_Click(object sender, EventArgs e)
{
try
{
string path = string.Empty;
if (string.IsNullOrWhiteSpace(tBox_path.Text.Trim()) && cBox_Variable.SelectedItem.ToString().Contains(EditImageType.其他图片.ToString()))
throw new Exception("图片地址不能为空,请选择要添加的图片");
if (cBox_Variable.SelectedItem.ToString().Contains(EditImageType.店铺头像.ToString()))
path = TbTools.SHOPICO;
else if (cBox_Variable.SelectedItem.ToString().Contains(EditImageType.商品主图.ToString()))
path = TbTools.ITEMICO;
else
{
path = tBox_path.Text.Trim();
if (!File.Exists(path))
{
var path_temp = Util.MapFile(Guid.NewGuid().ToString() + ".png", TbTools.OTHERSICOPATH);
Tools.DownloadImage(path, path_temp);
path = path_temp;
}
}
if (tbConfig == null)
tbConfig = new TbConfig() { C_Size = new Size() { Height = (int)numericUpDown_Height.Value, Width = (int)numericUpDown_Width.Value }, C_Tag = new ImageInfo() { Shape = radioButton1.Checked, ImagePath = path, Varible = cBox_Variable.SelectedItem.ToString() } };
else
{
tbConfig.C_Size = new Size() { Height = (int)numericUpDown_Height.Value, Width = (int)numericUpDown_Width.Value };
tbConfig.C_Tag = new ImageInfo() { Shape = radioButton1.Checked, ImagePath = path, Varible = cBox_Variable.SelectedItem.ToString() };
}
this.DialogResult = DialogResult.OK;
}
catch (Exception ex)
{
ShowError(ex);
}
}
///
/// 关闭窗体
///
///
///
private void simpleButton3_Click(object sender, EventArgs e)
{
this.Close();
}
}
}