winform项目中,有时需要浏览图片,图片的大小无法确定,就需要根据情况来动态显示.分为网络图片和本地图片
1.网络图片,需要加载图片信息
/// <summary>
/// 加载网络图片
/// </summary>
/// <param name="url"></param>
public fImgView(string url)
{
InitializeComponent();
this.zImage.ImageLocation = url;
this.zImage.LoadCompleted += (o, e) =>
{
if (e.Error != null || e.Cancelled) return;
var img = this.zImage.Image;
FormRender(img);
};
}
2.本地图片,可直接获取图片信息
/// <summary>
/// 本地图片
/// </summary>
/// <param name="img"></param>
/// <param name="name"></param>
public fImgView(Image img, string name)
{
InitializeComponent();
this.zImage.Image = img;
FormRender(img);
this.zSave.FileName = name;
if (!string.IsNullOrEmpty(this.zSave.DefaultExt)) this.zSave.Filter = "图像文件(." + this.zSave.DefaultExt + ")|*." + this.zSave.DefaultExt;
}
制作图片浏览器的窗体
1.绘制窗体如下,一个Panel放一个PictureBox
编写鼠标触发事件
private void zImage_MouseDown(object sender, MouseEventArgs e)
{
pm = System.Windows.Forms.Cursor.Position;
pi = this.zImage.Location;
this.zImage.MouseMove += new MouseEventHandler(zImage_MouseMove);
}
void zImage_MouseMove(object sender, MouseEventArgs e)
{
this.zImage.Location = new Point(pi.X + System.Windows.Forms.Cursor.Position.X - pm.X, pi.Y + System.Windows.Forms.Cursor.Position.Y - pm.Y);
}
private void zImage_MouseUp(object sender, MouseEventArgs e)
{
this.zImage.MouseMove -= zImage_MouseMove;
}
实现效果如下,代码虽然少,但整整弄了4天才完善.