我用了FileUpload1来做上传,我现在想在上传图片的时候我能够把图片变小以后在上传,该如何实现,麻烦告知谢谢!我用的是C#语言,麻烦给个完整的例子谢谢!
解决方案 »
- 为什么.net发送邮件都不用填写用户密码的
- GridView模版列按钮写事件的时候怎么获得行号?
- 求一个可以拖动排序的控件
- 修改传说中的down.asp,跪求解答!
- 如何获取嵌套Repeater中的控件?在线等.
- 问一个读取"抽象类的非抽象属性"的问题,困惑我一早上,找不到答案.
- 如何在.net生成的新闻静态页面里添加"提交评论"与显示该新闻的所有评论?
- 能不能把从数据库里取出来的数据,行跟列置换。
- ASP.NET MVC基础布局问题
- 关于Request.querystring与Session的问题?困扰.......
- .NET连接SQL2005的时候发生的错误,大家帮忙看下!!!!!!!
- asp.net 的 fileupload 里面的内容被弹出窗口冲掉是怎么办啊?
http://blog.csdn.net/durongjian/archive/2009/05/14/4183109.aspx
// 保存图片,必要时还要缩小
if (this.File1.PostedFile.ContentType.IndexOf("image") == -1)
{ CCClient.AlertMessage("只能上传图片文件!");
return;
}
System.Drawing.Image img = System.Drawing.Image.FromStream(this.File1.PostedFile.InputStream);
int iMaxWidth = 240;
int iMaxHeight = 320; int iNewWidth = 0;
int iNewHeight = 0;
if (img.Width > iMaxWidth || img.Height > iMaxHeight)
{
iNewWidth = iMaxHeight * img.Width / img.Height;
iNewHeight = iMaxWidth * img.Height / img.Width;
if (iNewWidth > iMaxWidth) 还是宽了,以宽为标准计算
{
iNewWidth = iMaxWidth; }
else if (iNewHeight > iMaxHeight)
{
iNewHeight = iMaxHeight;
} img = img.GetThumbnailImage(iNewWidth, iNewHeight, null, IntPtr.Zero);
System.Drawing.Imaging.ImageCodecInfo e = null;
System.Drawing.Imaging.ImageCodecInfo[] es = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
// 扩展名;
string ext = this.File1.PostedFile.ContentType.ToLower();
if (ext.IndexOf("gif") > -1)
{
ext = "gif";
}
else
{
ext = "jpeg";
}
foreach (System.Drawing.Imaging.ImageCodecInfo ef in es)
{
if (ef.MimeType.ToLower().IndexOf(ext) > -1)
{
e = ef;
}
}
System.Drawing.Imaging.EncoderParameters ps = new System.Drawing.Imaging.EncoderParameters();
ps.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100); img.Save(NewPath, e, ps);
}
else
{
this.File1.PostedFile.SaveAs(NewPath);
}
Graphics objGraphics = Graphics.FromImage(objNewBitMap);
objGraphics.Clear(Color.Transparent);
objGraphics.DrawImage(img, new Rectangle(0, 0, iNewWidth, iNewHeight));
objNewBitMap.Save(NewPath, e, ps);