private void DrawIamge(string str)
{
System.Drawing.Image image,aNewImage,callb;
int width,height,newwidth=10,newheight=10;
image=System.Drawing.Image.FromFile(Server.MapPath("/image/"+str));
width=image.Width;
height=image.Height;
if(width>height)
{
newwidth=250;
newheight=image.Height * newwidth/image.Width;
}
else
{
aNewImage=image.GetThumbnailImage(newwidth,newheight,callb,new System.IntPtr());
aNewImage.Save(Server.MapPath("/image/"+"min_"+str));
}
image.Dispose();
}
教教我,哪里错了
{
System.Drawing.Image image,aNewImage,callb;
int width,height,newwidth=10,newheight=10;
image=System.Drawing.Image.FromFile(Server.MapPath("/image/"+str));
width=image.Width;
height=image.Height;
if(width>height)
{
newwidth=250;
newheight=image.Height * newwidth/image.Width;
}
else
{
aNewImage=image.GetThumbnailImage(newwidth,newheight,callb,new System.IntPtr());
aNewImage.Save(Server.MapPath("/image/"+"min_"+str));
}
image.Dispose();
}
教教我,哪里错了
IF语句的TRUE部分就没有执行缩略了.
{
newwidth=250;
newheight=image.Height * newwidth/image.Width;
}
else // 这个else应该去掉把
{
aNewImage=image.GetThumbnailImage(newwidth,newheight,callb,new System.IntPtr());
aNewImage.Save(Server.MapPath("/image/"+"min_"+str));
}
提示是这句有错误
aNewImage=image.GetThumbnailImage(newwidth,newheight,callb,new System.IntPtr());
{
System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
aNewImage=image.GetThumbnailImage(newwidth,newheight,callb,new System.IntPtr());
aNewImage.Save(Server.MapPath("/image/"+"min_"+str));
}
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/10/16/504620.aspx
下载:http://szlawbook.com/csdnv2/csdnv2.rar
public static bool ThumbnailCallback()
{
return false;
}
//调用示例
this.MakeSmallImg(f.PostedFile,this.Server.MapPath("1.jpg"),600,500);
//------------------------------------------------------------public void MakeSmallImg(System.Web.HttpPostedFile postFile,string saveImg,System.Double Width,System.Double Height)
{
//原始图片名称
string originalFilename = postFile.FileName;
//生成的高质量图片名称
string strGoodFile = saveImg;
//从文件取得图片对象
System.Drawing.Image image = System.Drawing.Image.FromStream(postFile.InputStream,true); System.Double NewWidth,NewHeight;
if(image.Width>image.Height)
{
NewWidth=Width;
NewHeight=image.Height*(NewWidth/image.Width);
}
else
{
NewHeight=Height;
NewWidth=(NewHeight/image.Height)*image.Width;
} if (NewWidth>Width)
{
NewWidth=Width;
}
if (NewHeight>Height)
{
NewHeight=Height;
} //取得图片大小
System.Drawing.Size size = new Size((int)NewWidth,(int)NewHeight);
//新建一个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.White);
//在指定位置画图
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);
///文字水印
//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(strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg); g.Dispose();
image.Dispose();
bitmap.Dispose();
}