急问如何将一个图片缩小尺寸? 在做web程序时候,客户上传了一张图片到服务器,我需要同时为这张图片制作一个缩略图,请问这个缩略图如何制作?谢谢大侠.急 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是我以前用过的生成立体版权,与缩略图给几篇文章你参考http://dev.csdn.net/develop/article/26/26706.shtmhttp://dev.csdn.net/develop/article/21/21341.shtmpublic string UploadImage(System.Web.HttpPostedFile postFile) { try { if(postFile.ContentLength==0) { throw(new Exception("NotExistedPostFile")); } else if(postFile.ContentLength>Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["MaxImageSize"])) { throw(new Exception("TooLargePostFile")); } String strExt=System.IO.Path.GetExtension(postFile.FileName); if(System.Configuration.ConfigurationSettings.AppSettings["ImageExtension"].IndexOf(";"+strExt+";")>-1) { throw(new Exception("ErrorImageExtension")); } String strFileName=@"/uploads/images/"+DateTime.Now.ToString("yyyyMMddHHmmssffff")+".jpg"; String strAbsoluteFileName=System.Web.HttpContext.Current.Server.MapPath(strFileName); String Copyright=System.Configuration.ConfigurationSettings.AppSettings["CopyRight"]; System.Drawing.Image imgPhoto=System.Drawing.Image.FromStream(postFile.InputStream,true); //取高和宽 int phWidth = imgPhoto.Width; int phHeight =imgPhoto.Height; //建新图,指定格式为每像素 24 位;红色、绿色和蓝色分量各使用 8 位。 Bitmap bmPhoto = new Bitmap(phWidth, phHeight,PixelFormat.Format24bppRgb); //设置分辨率 bmPhoto.SetResolution(imgPhoto.HorizontalResolution,imgPhoto.VerticalResolution); //准备Graphics Graphics grPhoto = Graphics.FromImage(bmPhoto); try { //指定消除锯齿的呈现。 grPhoto.SmoothingMode = SmoothingMode.AntiAlias; //拷贝原图到做图区 grPhoto.DrawImage( imgPhoto, new Rectangle(0, 0, phWidth, phHeight), 0, 0, phWidth, phHeight, GraphicsUnit.Pixel); //用指定Sizes绘制图片时,测量用指定的字串宽度 //取可能的最大宽度 int[] sizes = new int[]{64,48,32,16,8,6,4}; Font crFont = null; SizeF crSize = new SizeF(); for (int i=0 ;i<7; i++) { crFont = new Font("Verdana", sizes[i], FontStyle.Bold); crSize = grPhoto.MeasureString(Copyright, crFont); if((ushort)crSize.Width < (ushort)phWidth) break; } //指定做图点 int yPixlesFromBottom = (int)(phHeight *.05); float yPosFromBottom = ((phHeight - yPixlesFromBottom)-(crSize.Height/2)); float xCenterOfImg = (phWidth/2); StringFormat StrFormat = new StringFormat(); StrFormat.Alignment = StringAlignment.Center; //绘制copyright SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(100, 0, 0,0)); grPhoto.DrawString(Copyright, crFont, semiTransBrush2, new PointF(xCenterOfImg+1,yPosFromBottom+1), StrFormat); SolidBrush semiTransBrush = new SolidBrush( Color.FromArgb(100, 255, 255, 255)); grPhoto.DrawString(Copyright, crFont, semiTransBrush, new PointF(xCenterOfImg,yPosFromBottom), StrFormat); bmPhoto.Save(strAbsoluteFileName,ImageFormat.Jpeg); } finally { grPhoto.Dispose(); bmPhoto.Dispose(); imgPhoto.Dispose(); } return strFileName; } catch(Exception excep) { throw(excep); } } 我是以前遇到这样的问题过,我用的是visual studio,当时没有生成缩略图,直接在页面上拉了个IMAGE控件,然后把控件的固定成一个合适的大小,然后用户上传上图片以后在IMAGE控件里显示就行了,和缩略图效果一样的,就是把图片另存下来可以看到实际上图片没有变小,还是原图,但是显示是小了。 公司有个NB的人用JS写个程序,在页面上显示大图,在右下角显示小图,小图就是大图的缩略图(只有大图的1/10),还可以拖来拖去!只不过我看不到代码!UP 谢谢 LaoDai_Net(老代.Net『学无止境』) ,果然是高人.根据你提供的文章搞定了 新手求助 编辑代码的时候 要怎么样才能换成插入啊 split 分割中文字的问题 基于ASP.Net的B/S结构系统开发 散分贴 大数据量下数据库(MSSQL)的性能管理及优化?? 简单问题: 那里有智能客户端的例子下载 用C#代码写一个数据库导入系统,只是将excel表导入到SQL2008数据库中 求解:winform下下载文件的进度条如何做?up者有分! 在数据库的temp字段中中放入:Response.Write("您好!");现在C#中读出,如何显示并运行这一行程序? 急!!!哪可下载InstallShield v8.02 在c#如何调用vb脚本 数组的Sort方法的用法
给几篇文章你参考
http://dev.csdn.net/develop/article/26/26706.shtm
http://dev.csdn.net/develop/article/21/21341.shtm
public string UploadImage(System.Web.HttpPostedFile postFile)
{
try
{
if(postFile.ContentLength==0)
{
throw(new Exception("NotExistedPostFile"));
}
else
if(postFile.ContentLength>Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["MaxImageSize"]))
{
throw(new Exception("TooLargePostFile"));
}
String strExt=System.IO.Path.GetExtension(postFile.FileName);
if(System.Configuration.ConfigurationSettings.AppSettings["ImageExtension"].IndexOf(";"+strExt+";")>-1)
{
throw(new Exception("ErrorImageExtension"));
} String strFileName=@"/uploads/images/"+DateTime.Now.ToString("yyyyMMddHHmmssffff")+".jpg";
String strAbsoluteFileName=System.Web.HttpContext.Current.Server.MapPath(strFileName);
String Copyright=System.Configuration.ConfigurationSettings.AppSettings["CopyRight"]; System.Drawing.Image imgPhoto=System.Drawing.Image.FromStream(postFile.InputStream,true);
//取高和宽
int phWidth = imgPhoto.Width;
int phHeight =imgPhoto.Height;
//建新图,指定格式为每像素 24 位;红色、绿色和蓝色分量各使用 8 位。
Bitmap bmPhoto = new Bitmap(phWidth, phHeight,PixelFormat.Format24bppRgb);
//设置分辨率
bmPhoto.SetResolution(imgPhoto.HorizontalResolution,imgPhoto.VerticalResolution);
//准备Graphics
Graphics grPhoto = Graphics.FromImage(bmPhoto);
try
{
//指定消除锯齿的呈现。
grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
//拷贝原图到做图区
grPhoto.DrawImage(
imgPhoto,
new Rectangle(0, 0, phWidth, phHeight),
0,
0,
phWidth,
phHeight,
GraphicsUnit.Pixel);
//用指定Sizes绘制图片时,测量用指定的字串宽度
//取可能的最大宽度
int[] sizes = new int[]{64,48,32,16,8,6,4};
Font crFont = null;
SizeF crSize = new SizeF();
for (int i=0 ;i<7; i++)
{
crFont = new Font("Verdana", sizes[i],
FontStyle.Bold);
crSize = grPhoto.MeasureString(Copyright,
crFont);
if((ushort)crSize.Width < (ushort)phWidth)
break;
}
//指定做图点
int yPixlesFromBottom = (int)(phHeight *.05);
float yPosFromBottom = ((phHeight -
yPixlesFromBottom)-(crSize.Height/2));
float xCenterOfImg = (phWidth/2);
StringFormat StrFormat = new StringFormat();
StrFormat.Alignment = StringAlignment.Center;
//绘制copyright
SolidBrush semiTransBrush2 =
new SolidBrush(Color.FromArgb(100, 0, 0,0));
grPhoto.DrawString(Copyright,
crFont,
semiTransBrush2,
new PointF(xCenterOfImg+1,yPosFromBottom+1),
StrFormat);
SolidBrush semiTransBrush = new SolidBrush(
Color.FromArgb(100, 255, 255, 255));
grPhoto.DrawString(Copyright,
crFont,
semiTransBrush,
new PointF(xCenterOfImg,yPosFromBottom),
StrFormat);
bmPhoto.Save(strAbsoluteFileName,ImageFormat.Jpeg);
}
finally
{
grPhoto.Dispose();
bmPhoto.Dispose();
imgPhoto.Dispose();
}
return strFileName;
}
catch(Exception excep)
{
throw(excep);
}
}