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.Imag
相关文档:
1. 使用QueryString变量
QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中。如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法。但是对于传递数组或对象的话,就不能用这个方法了。下面是一个例子:
a.aspx的C#代码
private void Button1_Click(object sende ......
这是微软的一个Bug,可以去官网下载一个Validators.dll,引用到你的项目下,然后在Web.Config里configuration/system.web/pages节点中添加(或修改)如下的元素:
<pages>
<tagMapping>
<add tagType="System.Web.UI.Web ......
MVC(即模型-视图-控制器)
MVC(Model-View-Controller)用于表示一种软件架构模式.它把软件系统分为三个基本部分:模型(Model),
视图(View)和控制器(Controller)
MVC的优点:
1多个视图对应一个模型的能力
......