using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lskj.PubUtils;
using DevExpress.XtraEditors.Controls;
using System.Reflection;
using CommonLib;
using System.IO;
using System.Drawing.Imaging;
namespace Lskj.MyControl
{
public partial class LabelPicFileEx : UserControl
{
///
/// 存放本地图片未知
///
private string _localTempPath = Application.StartupPath + "\\tempfiles";
public string CurrentImagePath = string.Empty;
private int padding_right = 7;
public LabelPicFileEx()
{
InitializeComponent();
}
private string labelText;
[Description("Label值")]
public string LabelText
{
get { return labelText; }
set
{
labelText = value;
lbMsg.Text = value;
pl_left.Width = lbMsg.Width + padding_right;
}
}
[Description("提示文本")]
public string TipMsg { get; set; }
[Description("是否允许为空")]
public bool IsEmpt { get; set; }
public PictureEdit GetPictureBox { get { return ImgFile;} }
private string defaultString;
public string DefaultString
{
get { return defaultString; }
set { defaultString = value; }
}
private string unionFields;
public string UnionFields
{
get { return unionFields; }
set { unionFields = value; }
}
private string unionValue;
public string UnionValue
{
get { return unionValue; }
set { unionValue = value; }
}
public event UnionEventHander UnionEvent;
public delegate void UnionEventHander(object sender);
public event UploadEventHander UploadEvent;
public delegate void UploadEventHander(object sender);
public event DeleteEventHander DeleteEvent;
public delegate void DeleteEventHander(object sender);
#region 右键菜单事件
///
/// 剪切
///
///
///
private void tsmi_cut_Click(object sender, EventArgs e)
{
this.CurrentImagePath = string.Empty;
InvokeMenuMethod(GetMenu(ImgFile), "OnClickedCut");
}
///
/// 拷贝图片
///
///
///
private void tsmi_copy_Click(object sender, EventArgs e)
{
InvokeMenuMethod(GetMenu(ImgFile), "OnClickedCopy");
}
///
/// 粘贴图片
///
///
///
private void tsmi_paste_Click(object sender, EventArgs e)
{
this.CurrentImagePath = string.Empty;
InvokeMenuMethod(GetMenu(ImgFile), "OnClickedPaste");
}
///
/// 删除图片
///
///
///
private void tsmi_delete_Click(object sender, EventArgs e)
{
this.CurrentImagePath = string.Empty;
InvokeMenuMethod(GetMenu(ImgFile), "OnClickedDelete");
if (DeleteEvent != null)
{
DeleteEvent(sender);
}
}
///
/// 加载图片
///
///
///
private void tsmi_load_Click(object sender, EventArgs e)
{
InvokeMenuMethod(GetMenu(ImgFile), "OnClickedLoad");
this.CurrentImagePath = string.Empty;
}
///
/// 保存图片
///
///
///
private void tsmi_save_Click(object sender, EventArgs e)
{
InvokeMenuMethod(GetMenu(ImgFile), "OnClickedSave");
}
///
/// 关联其他图片
///
///
///
private void tsmi_union_Click(object sender, EventArgs e)
{
if (UnionEvent != null)
{
UnionEvent(this);
}
}
///
/// 上传图片
///
///
///
private void tsmi_upload_Click(object sender, EventArgs e)
{
if (UploadEvent != null)
{
UploadEvent(this);
}
}
#endregion
#region 其他事件
///
/// 打开之前验证
///
///
///
private void cmsMenu_Opening(object sender, CancelEventArgs e)
{
bool isImage = ImgFile.Image == null;
tsmi_cut.Enabled = !isImage;
tsmi_copy.Enabled = !isImage;
tsmi_delete.Enabled = !isImage;
tsmi_save.Enabled = !isImage;
tsmi_load.Enabled = true;
tsmi_union.Enabled = true;
tsmi_upload.Enabled = !isImage;
tsmi_paste.Enabled = ClipboardHelper.ContainsImage();
}
#endregion
#region 扩展PictureEdit
private void InvokeMenuMethod(PictureMenu menu, string name)
{
MethodInfo mi = typeof(PictureMenu).GetMethod(name, BindingFlags.NonPublic | BindingFlags.Instance);
if (mi != null && menu != null)
mi.Invoke(menu, new object[] { menu, new EventArgs() });
}
private PictureMenu GetMenu(PictureEdit edit)
{
PropertyInfo pi = typeof(PictureEdit).GetProperty("Menu", BindingFlags.NonPublic | BindingFlags.Instance);
if (pi != null)
return pi.GetValue(edit, null) as PictureMenu;
return null;
}
#endregion
///
/// 保存图片到本地
/// 存放的路径
///
public string SaveImageToLocal(string prefix)
{
try
{
Image img = this.GetPictureBox.Image;
if (img != null)
{
//string fileName = string.IsNullOrWhiteSpace(CurrentImagePath) ? prefix + "_" + Guid.NewGuid().ToString() + ".png" : Path.GetFileName(CurrentImagePath);
string fileName = prefix + "_" + Guid.NewGuid().ToString().Replace("-", "") + ".jpg";
string tempPath = Path.Combine(new string[] { _localTempPath, fileName });
this.CurrentImagePath = tempPath;
// 不存在创建目录
if (!Directory.Exists(Path.Combine(new string[] { _localTempPath })))
{
Directory.CreateDirectory(Path.Combine(_localTempPath));
}
if (!File.Exists(tempPath))
{
Bitmap bitmap = new Bitmap(img);
bitmap.Save(tempPath);
}
return (File.Exists(tempPath)) ? tempPath : string.Empty;
}
}
catch (Exception)
{
}
return string.Empty;
}
private void ImgFile_MouseDoubleClick(object sender, MouseEventArgs e)
{
FrmImageDialog imageDialog = new FrmImageDialog();
imageDialog.ImageSource = this.ImgFile.Image;
imageDialog.ShowDialog();
}
}
}