110,538
社区成员
发帖
与我相关
我的任务
分享
//
//自定义的TextBox中WaterMark属性的定义
//
static object WaterMarkChangedEvent = new object();
/// <summary>
/// 当WaterMark属性更改时触发.
/// </summary>
public event EventHandler WaterMarkChanged
{
add { Events.AddHandler(WaterMarkChangedEvent, value); }
remove { Events.RemoveHandler(WaterMarkChangedEvent, value); }
}
WaterMarkStyle waterMark = new WaterMarkStyle("",SystemColors.GrayText);
/// <summary>
/// 获取或设置控件的水印效果.
/// </summary>
[DefaultValue(typeof(WaterMarkStyle),"")]
[Browsable(true)]
public WaterMarkStyle WaterMark
{
get { return waterMark; }
set
{
if (waterMark==value) return ;
waterMark = value;
OnWaterMarkChanged(EventArgs.Empty);
}
}
//... ...
//
//WaterMarkStyle类型定义
//
/// <summary>
/// 可编辑控件的水印样式
/// </summary>
[TypeConverter(typeof(WaterMarkStyleTypeConverter))]
[ComVisible(true)]
[Serializable]
public struct WaterMarkStyle
{
string text;
Color color;
/// <summary>
/// 获取或设置水印文本.
/// </summary>
[DefaultValue("")]
public string Text
{
get { return text; }
set
{
if (text == value) return;
text = value;
}
}
/// <summary>
/// 获取或设置水印颜色.
/// </summary>
[DefaultValue(typeof(Color),"GrayText")]
public Color Color
{
get { return color; }
set
{
if (color==value) return ;
color = value;
}
}
/// <summary>
/// 重写父类的判等方法.
/// </summary>
/// <param name="obj">与当前实例比较的目标对象</param>
/// <returns>如果两个对象相等则返回true,否则返回false.</returns>
public override bool Equals(object obj)
{
if (obj == null || !(obj is WaterMarkStyle)) return false;
WaterMarkStyle wms = (WaterMarkStyle)obj;
return this.text == wms.text && this.color == wms.color;
}
/// <summary>
/// 获取类型当前实例的哈希码.
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return this.text.GetHashCode() ^ this.color.GetHashCode();
}
/// <summary>
/// 获取类型当前实例的文本值.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return this.text;
}
/// <summary>
/// 判断给定两个参数是否相等
/// </summary>
/// <param name="wm1"></param>
/// <param name="wm2"></param>
/// <returns></returns>
public static bool operator ==(WaterMarkStyle wm1,WaterMarkStyle wm2)
{
if (wm1==null ^wm2==null)
{
return false;
}
return wm1.text == wm2.text && wm1.color.ToArgb() == wm2.color.ToArgb();
}
/// <summary>
/// 判断给定两个参数是否不等.
/// </summary>
/// <param name="wm1"></param>
/// <param name="wm2"></param>
/// <returns></returns>
public static bool operator !=(WaterMarkStyle wm1, WaterMarkStyle wm2)
{
if (wm1 == null ^ wm2 == null)
{
return true;
}
return wm1.text != wm2.text || wm1.color.ToArgb() != wm2.color.ToArgb();
}
/// <summary>
/// 根据给定的文本和文本颜色,构造当前类型实例.
/// </summary>
/// <param name="text"></param>
/// <param name="color"></param>
public WaterMarkStyle(string text,Color color):this()
{
this.text = text;
this.color = color;
}
/// <summary>
/// 根据给定的文本和默认颜色,构造当前类型实例.
/// </summary>
/// <param name="text"></param>
public WaterMarkStyle(string text) : this()
{
this.text = text;
this.color =SystemColors.GrayText;
}
}
//类型转换器
class WaterMarkStyleTypeConverter:TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType==typeof(string) || base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType ==typeof( InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value != null)// return new WaterMarkStyle("",SystemColors.GrayText);// throw new ArgumentNullException("value");
{
if (value is string)
{
WaterMarkStyle obj = (WaterMarkStyle)TypeDescriptor.GetProperties(typeof(TextBox))["WaterMark"].GetValue(context.Instance);
if (obj == null || obj.Color == Color.Empty)
{
return new WaterMarkStyle(value.ToString(), SystemColors.GrayText);
}
return new WaterMarkStyle(value.ToString(), obj.Color);
}
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType==null)
{
throw new ArgumentNullException("destinationType");
}
if (value != null)
{
WaterMarkStyle wms = (WaterMarkStyle)value;
if (destinationType == typeof(string))
{
return wms.Text;
}
if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ctor = typeof(WaterMarkStyle).GetConstructor(new Type[] { typeof(string), typeof(Color) });
if (ctor != null)
{
return new InstanceDescriptor(ctor, new object[] { wms.Text, wms.Color });
}
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
return TypeDescriptor.GetProperties(typeof(WaterMarkStyle), attributes).Sort(new string[] { "Text", "Color" });
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues == null)
{
throw new ArgumentNullException("propertyValues");
}
object obj2 = propertyValues["Text"];
object obj3 = propertyValues["Color"];
if (((obj2 == null) || (obj3 == null)) || (!(obj2 is string) || !(obj3 is Color)))
{
throw new ArgumentException("无效属性值!");
}
return new WaterMarkStyle(obj2 as string, (Color)obj3);
}
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
//获取图片路径
string filePath = this.fuplImg.PostedFile.FileName;
//获取文件名
string fileName = this.fuplImg.FileName;
//获取扩展名
FileInfo info = new FileInfo(fileName);
string type = info.Extension;
//判断图片类型是否允许上传
if (type == ".jpg" || type == ".bmp" || type == ".gif")
{
//获取图片存储位置的物理路径
string path = Server.MapPath("Images//" + fileName);
//上传保存
this.fuplImg.PostedFile.SaveAs(path);
//加载图片
Image imgFile = new Image();
imgFile.Width = 200;
imgFile.Height = 150;
imgFile.ImageUrl = @"~/Images/" + fileName;
this.imgDiv.Controls.Add(imgFile);
Page.ClientScript.RegisterStartupScript(typeof(Page), "", "alert('上传成功!')", true);
}
else
{
Page.ClientScript.RegisterStartupScript(typeof(Page), "", "alert('请检查上传图片的格式!')", true);
}
}
/**
* IRequiresSessionState是一个空接口,仅用来
* 标记handler是否对session拥有读写权
*/
public void ProcessRequest(HttpContext context)
{
//1、建立画板
Bitmap bitmap = new Bitmap(80, 30);
//2、建立画布(绘图类)
Graphics g = Graphics.FromImage(bitmap);
//3、填充画布
g.FillRectangle(Brushes.White, 0, 0, 80, 30);
//4、设置字体
Font f = new Font("隶书", 16);
//5、在画布上写入字符
string letters = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
//6、定义变量,接受生成的随机字符串
StringBuilder sb = new StringBuilder();
//7、取字符
Random r=new Random();
//接受生成的单个随机字符
string result = string.Empty;
for (int i = 0; i < 4; i++)
{
result=letters.Substring(r.Next(0, letters.Length - 1), 1);
sb.Append(result);
g.DrawString(result, f, Brushes.Red, i * 15, r.Next(0, 10));
}
//8、输出到响应流
context.Response.ContentType = "image/Jpg";
bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);
//9、保存随机字符串到session
context.Session["yanzheng"] = sb.ToString();
//释放资源
bitmap.Dispose();
g.Dispose();
context.Response.End();
}
public void ProcessRequest(HttpContext context)
{
string path = context.Request.PhysicalPath;
if (File.Exists(path))
{
System.Drawing.Image image = Image.FromFile(path);
Graphics g = Graphics.FromImage(image);
Font f = new Font("楷体", 12);
g.DrawString("水印", f, Brushes.Red, 0, 0);
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
g.Dispose();
image.Dispose();
context.Response.End();
}
}
/// <summary>
/// 获取或设置控件的水印效果.
/// </summary>
[DefaultValue(typeof(WaterMarkStyle),"")]
[Browsable(true)]
public WaterMarkStyle WaterMark
{
get { return waterMark; }
set
{
if (waterMark==value) return ;
waterMark = value;
if(OnWaterMarkChanged != null)
OnWaterMarkChanged(EventArgs.Empty);
}
}
public class MyTextBox : TextBox
{
WaterMarkStyle waterMark = new WaterMarkStyle("", SystemColors.GrayText);
/// <summary>
/// 获取或设置控件的水印效果.
/// </summary>
public WaterMarkStyle WaterMark
{
get { return waterMark; }
set
{
if (waterMark != value)
{
waterMark = value;
}
}
}
/// <summary>
/// 可编辑控件的水印样式
/// </summary>
[TypeConverter(typeof(ExpandableObjectConverter))] //<---
public class WaterMarkStyle
{
public WaterMarkStyle() : this("", SystemColors.GrayText)
{
}
public WaterMarkStyle(string text, Color color)
{
this.Text = text;
this.Color = color;
}
/// <summary> 获取或设置水印文本. </summary>
public string Text { get; set;}
/// <summary> 获取或设置水印颜色. </summary>
public Color Color {get; set;}
#region 重写判等方法
public override bool Equals(object obj)
{
return this == (obj as WaterMarkStyle);
}
public override int GetHashCode()
{
return (this.Text ?? "").GetHashCode() ^ this.Color.GetHashCode();
}
public override string ToString()
{
return this.Text;
}
public static bool operator ==(WaterMarkStyle wm1, WaterMarkStyle wm2)
{
if (object.ReferenceEquals(wm1, null) || object.ReferenceEquals(wm2, null) )
{
return object.ReferenceEquals(wm1, wm2);
}
return wm1.Text == wm2.Text && wm1.Color == wm2.Color;
}
public static bool operator !=(WaterMarkStyle wm1, WaterMarkStyle wm2)
{
return !(wm1 == wm2);
}
#endregion
}
}
}
private class WaterMarkStyleTypeConverter : ExpandableObjectConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
Color color = SystemColors.GrayText;
if (context != null && context.Instance != null && context.PropertyDescriptor != null)
{
WaterMarkStyle old = context.PropertyDescriptor.GetValue(context.Instance) as WaterMarkStyle;
if (old != null && old.Color != SystemColors.GrayText) color = old.Color;
}
return new WaterMarkStyle((string)value, color);
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (value is WaterMarkStyle && destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ctor = typeof(WaterMarkStyle).GetConstructor(new Type[] { typeof(string), typeof(Color) });
if (ctor != null)
{
WaterMarkStyle wms = (WaterMarkStyle)value;
return new InstanceDescriptor(ctor, new object[] { wms.Text, wms.Color });
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value != null)// return new WaterMarkStyle("",SystemColors.GrayText);// throw new ArgumentNullException("value");
{
if (value is string)
{
WaterMarkStyle obj = (WaterMarkStyle)TypeDescriptor.GetProperties(typeof(TextBox))["WaterMark"].GetValue(context.Instance);
if (obj == null || obj.Color == Color.Empty)
{
return new WaterMarkStyle(value.ToString(), SystemColors.GrayText);
}
return new WaterMarkStyle(value.ToString(), obj.Color);
}
}
return base.ConvertFrom(context, culture, value);
}