实例介绍
【实例简介】
此实例已经过测试,大家拿来即可使用,无须配置!
两个功能:一个是用程序实现下载网络图片至本地,另一个是自动生成该图片的缩略图
【实例截图】

【核心代码】
private void button1_Click(object sender, EventArgs e)
{
string localPath=ImageUtils.DownLoadPic(this.textBox1.Text);
this.pictureBox1.ImageLocation = localPath;
}
private void button2_Click(object sender, EventArgs e)
{
string path = AppDomain.CurrentDomain.BaseDirectory "Image\\" DateTime.Now.ToString("yyyyMMddHHmmss") DateTime.Now.Millisecond.ToString() ".jpg";
ImageUtils.MakeThumbnail(this.textBox1.Text, path, 50, 50, "H");
this.pictureBox1.ImageLocation = path;
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(网络路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
//System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
HttpWebResponse res = null;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(originalImagePath);
req.Proxy = null;
res = (HttpWebResponse)req.GetResponse();
//FileWebResponse res = null;
//FileWebRequest req = (FileWebRequest)WebRequest.Create(originalImagePath);
//res = (FileWebResponse)req.GetResponse();
System.Drawing.Image originalImage = System.Drawing.Image.FromStream(res.GetResponseStream(), false, false);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
case "Cust":
if (ow > oh) toheight = originalImage.Height * width / originalImage.Width;
else towidth = originalImage.Width * height / originalImage.Height;
break;
default:
break;
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//System .Drawing .Graphics .FromImage(
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
//bitmap.Save(HttpContext.Current.Server.MapPath(thumbnailPath), System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
if (res != null)
{
res.Close();
}
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
public static string DownLoadPic(string strLocalPicPath)
{
HttpWebResponse res = null;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strLocalPicPath);
req.Proxy = null;
res = (HttpWebResponse)req.GetResponse();
System.Drawing.Image originalImage = System.Drawing.Image.FromStream(res.GetResponseStream(), false, false);
string ext = "jpg";
if (originalImage.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp)) ext = "bmp";
if (originalImage.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif)) ext = "gif";
if (originalImage.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png)) ext = "png";
if (originalImage.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg)) ext = "jpg";
if (originalImage.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Icon)) ext = "icon";
string path = AppDomain.CurrentDomain.BaseDirectory "Image\\" DateTime.Now.ToString("yyyyMMddHHmmss") DateTime.Now.Millisecond.ToString() "." ext;
originalImage.Save(path);
strLocalPicPath = path;
originalImage.Dispose();
return strLocalPicPath;
}
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论