VS2003下用DataList做了相册,但是好象只能把图片的宽度设为定值,也就是说,如果图片宽高比是2:3显示出来的缩略图没有变形,但如果宽高比是3:2,显示出来的图片就会变形,有没有办法让显示出来的图片是正常的没有变形的缩略图呢?也就是说依据文件夹中相应图片的宽高比来显示。。 图片都是保存在文件中的,不是在数据库中。
解决方案 »
- VS2008(.NET/C#)中AJAX的使用问题
- 从CSDN上下了一个第三方控件,安装完成后,如何才能在visual developer 2008的toolbox上显示控件呢?
- 这个下载错误该怎么解决
- web自定义控件属性问题!
- 师傅们,快来帮帮小第吧!!
- VS2010 RC 版
- freetextbox 为什么不能获取从word复制过来的值
- 怎样卸掉vs.net.2003版中自带的水晶报表?我要装10,在线急等!
- 如何自己定义pagerstyle
- 怎么把数据库中的记录倒着取出来?或者把记录insert到数据库的开头?
- 用.net做了几个新闻,下一步做些什么?
- oracle和.NET 带参数sql问题
另外就是用javascript来控制,具体代码我也忘记了,你可以搜索一下,这样的话就可以做到不变形
public void pic_zero(string sourcepath,string aimpath,int scale)
{
string originalFilename =sourcepath;
//生成的高质量图片名称
string strGoodFile =aimpath; //从文件取得图片对象
System.Drawing.Image image = System.Drawing.Image.FromFile(originalFilename);
int iImgWidth = image.Width;
int iImgHeight = image.Height;
int iScale = (iImgWidth / scale)>(iImgHeight/scale) ? (iImgWidth / scale) : (iImgHeight / scale); //取得图片大小
System.Drawing.Size size = new Size(image.Width / iScale , image.Height / iScale);
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width,size.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.Blue);
//在指定位置画图
g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
new System.Drawing.Rectangle(0, 0, image.Width,image.Height),
System.Drawing.GraphicsUnit.Pixel);
//保存高清晰度的缩略图
bitmap.Save(strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg);
g.Dispose();
image.Dispose();
}
请高手指点
在前台html的<Img>里面写width=XXX height =XXX吗? 不行勒,这样就定死了呀高度和宽度呀,如果有400*200和200*400两种不同规格的图片要在一起显示出来,总有一种会变形呀。我就是不想让它不变形。
{
string photoGuid=System.Guid.NewGuid().ToString();
string path=System.Web.HttpContext.Current.Server.MapPath( System.Configuration.ConfigurationSettings.AppSettings["photoPath"]);
string sourcePic= path + photoGuid + ".jpg";
path=System.Web.HttpContext.Current.Server.MapPath( System.Configuration.ConfigurationSettings.AppSettings["thumbPath"]);
string thumbPic=path + photoGuid + ".jpg";
int thumbWidth=120;
int thumbHeight=160;
//生成图片id
Image image = Image.FromStream(source);
if(image.Height>600 || image.Width >600)
Compress(image,sourcePic);
else
{
FileStream srcfs = new FileStream(sourcePic,FileMode.Create);
image.Save(srcfs, ImageFormat.Jpeg);
srcfs.Close();
} //生成缩略图
int tw = thumbWidth;
int th = thumbHeight;
if(image.Height / th > image.Width / tw)
{
tw = image.Width * th / image.Height;
}
else
{
th = image.Height * tw / image.Width;
}
Image thumbImage = Image.FromHbitmap(new Bitmap(thumbWidth,thumbHeight,PixelFormat.Format32bppRgb).GetHbitmap());
Graphics g = Graphics.FromImage(thumbImage);
SolidBrush brush = new SolidBrush(Color.White);
g.FillRectangle(brush, 0, 0, thumbWidth, thumbHeight);
g.DrawImage(image, (thumbWidth - tw)/2, (thumbHeight -th)/2, tw, th);
g.Dispose();
FileStream thumbfs= new FileStream(thumbPic,FileMode.Create);
thumbImage.Save(thumbfs,ImageFormat.Jpeg);
thumbfs.Close();
image.Dispose();
return(photoGuid);
}