时间:2016-02-15 21:37 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【asp.net(C#)压缩图片,可以指定图片模板高宽】,下面是详细的讲解!
asp.net(C#)压缩图片,可以指定图片模板高宽
//生成缩略图函数
//顺序参数:源图文件流、缩略图存放地址、模版宽、模版高
//注:缩略图大小控制在模版区域内
public static void MakeSmallImg(System.IO.Stream fromFileStream, string fileSaveUrl, System.Double templateWidth, System.Double templateHeight)
{
//从文件取得图片对象,并使用流中嵌入的颜色管理信息
System.Drawing.Image myImage=System.Drawing.Image.FromStream(fromFileStream, true);
//缩略图宽、高
System.Double newWidth=myImage.Width, newHeight=myImage.Height;
//宽大于模版的横图
if (myImage.Width > myImage.Height || myImage.Width==myImage.Height)
{
if (myImage.Width > templateWidth)
{
//宽按模版,高按比例缩放
newWidth=templateWidth;
newHeight=myImage.Height * (newWidth / myImage.Width);
}
}
//高大于模版的竖图
else
{
if (myImage.Height > templateHeight)
{
//高按模版,宽按比例缩放
newHeight=templateHeight;
newWidth=myImage.Width * (newHeight / myImage.Height);
}
}
//取得图片大小
System.Drawing.Size mySize=new Size((int)newWidth, (int)newHeight);
//新建一个bmp图片
System.Drawing.Image bitmap=new System.Drawing.Bitmap(mySize.Width, mySize.Height);
//新建一个画板
System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.White);
//在指定位置画图
g.DrawImage(myImage, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, myImage.Width, myImage.Height),
System.Drawing.GraphicsUnit.Pixel);
///文字水印
//System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);
//System.Drawing.Font f=new Font("宋体",10);
//System.Drawing.Brush b=new SolidBrush(Color.Black);
//G.DrawString("myohmine",f,b,10,10);
//G.Dispose();
///图片水印
//System.Drawing.Image copyImage=System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
//Graphics a=Graphics.FromImage(bitmap);
//a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
//copyImage.Dispose();
//a.Dispose();
//copyImage.Dispose();
//保存缩略图
bitmap.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
myImage.Dispose();
bitmap.Dispose();
}
关于asp.net(C#)压缩图片,可以指定图片模板高宽的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【服务器】asp.net页面状态管理cookie和服务器状
- 【Repeater控件】.NET实现Repeater控件+AspNetPag
- 【客户端】获取客户端IP地址c#/vb.net各自实现代
- 【asp】asp.net上传execl文件后 在页面上加载显示
- 【Excel】页面导出为Excel的时间格式的问题-时间
- 【ref】asp.net(c#)ref out params的区别-out-pa
- 【数据控件】asp.net获得数据控件事件索引并获取
- 【NET】10个.NET中删除空白字符串的方法-删除空白
- 【web】web.config配置连接字符串的方法-config配
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
