直播中
namespace ImageZoomer
{
/// <summary>
///
/// </summary>
//枚舉類型定義,定義圖象的四種翻轉(zhuǎn)方式
public enum FlipModeStyle
{
NoFlip=0,//不翻轉(zhuǎn)
FlipX=1,//水平翻轉(zhuǎn)
FlipY=2,//垂直翻轉(zhuǎn)
FlipXY=3//水平垂直翻轉(zhuǎn)
}
//事件數(shù)據(jù)類定義,報告圖象的顯示尺寸
public class DisplaySizeChangedEventArgs:System.EventArgs
{
public int Width;
public int Height;
public DisplaySizeChangedEventArgs()
{
}
}
//事件代表的聲明
public delegate void DisplaySizeChangedEventHandler(object sender,DisplaySizeChangedEventArgs e);
//用戶自定義控件類
public class ImageZoomerControl : System.Windows.Forms.Control
{
private int width;//控件寬度
private int height;//控件高度
private System.Drawing.Bitmap bitmap;//控件上的圖象
private FlipModeStyle flip;//圖象的翻轉(zhuǎn)方式
private event DisplaySizeChangedEventHandler eventHandler;//事件
//構(gòu)造方法,初始化數(shù)據(jù)成員
public ImageZoomerControl()
{
width=this.width;
height=this.height;
bitmap=null;
eventHandler=null;
}
//寬度屬性
[
Category("ImageZoomer"),
Description("The displayed image width.")
]
public int DisplayWidth
{
get
{
return width;
}
set
{
if(value>=0)
{
width=value;
Invalidate(this.ClientRectangle);
}
}
}
//高度屬性
[
Category("ImageZoomer"),
Description("The displayed image height.")
]
public int DisplayHeight
{
get
{
return height;
}
set
{
if(value>=0)
{
height=value;
this.Invalidate(this.ClientRectangle);
}
}
}
//圖象屬性
[
Category("ImageZoomer"),
Description("The image displayed by this control."),
DefaultValue(null)
]
public Bitmap DisplayImage
{
get
{
return bitmap;
}
set
{
bitmap=value;
if(bitmap!=null)
{
width=bitmap.Width;
height=bitmap.Height;
}
else
{
width=this.width;
height=this.height;
}
this.Invalidate(this.ClientRectangle);
}
}
//翻轉(zhuǎn)方式屬性
[
Category("ImageZoomer"),
Description("Specify how the image will be flipped.")
]
public FlipModeStyle FlipMode
{
get
{
return flip;
}
set
{
flip=value;
this.Invalidate(this.ClientRectangle);
}
}
//事件屬性
[
Category("ImageZoomer"),
Description("Occurs when the image size is changed.")
]
public event DisplaySizeChangedEventHandler DisplaySizeChanged
{
add
{
eventHandler+=value;
}
remove
{
eventHandler-=value;
}
}
protected override void OnPaint(PaintEventArgs pe)
{
if(bitmap==null)
{
return;
}
Graphics g=pe.Graphics;
//轉(zhuǎn)換距陣
System.Drawing.Drawing2D.Matrix transform;//距陣
if(flip.Equals(FlipModeStyle.FlipX))
{
transform=new System.Drawing.Drawing2D.Matrix(-1,0,0,1,width,0);
}
else if(flip.Equals(FlipModeStyle.FlipY))
{
transform=new System.Drawing.Drawing2D.Matrix(1,0,0,-1,0,height);
}
else if(flip.Equals(FlipModeStyle.FlipXY))
{
transform=new System.Drawing.Drawing2D.Matrix(-1,0,0,-1,width,height);
}
else
{
transform=new System.Drawing.Drawing2D.Matrix(1,0,0,1,0,0);
}
//設(shè)置轉(zhuǎn)換距陣
g.Transform=transform;
g.DrawImage(bitmap,new System.Drawing.Rectangle(0,0,width,height),
0,0,bitmap.Width,bitmap.Height,
GraphicsUnit.Pixel);
//恢復(fù)繪圖平面
g.ResetTransform();
}
//發(fā)出事件的方法
protected virtual void OnDisplaySizeChanged(DisplaySizeChangedEventArgs e)
{
//調(diào)用事件對象指向的方法
this.Invoke(eventHandler,new object[]
{
this,e
}
);
}
//重載方法,調(diào)用OnDisplaySizeChanged()發(fā)出事件
protected override void OnMouseDown(MouseEventArgs e)
{
width=e.X;
height=e.Y;
this.Invalidate(this.ClientRectangle);
DisplaySizeChangedEventArgs eventData=new DisplaySizeChangedEventArgs();
eventData.Width=width;
eventData.Height=height;
this.OnDisplaySizeChanged(eventData);
base.OnMouseDown(e);
}
}
}