ASP.NET技术

z511273571 2010-05-10 08:46:23
上传的图片,将宽度大于640px的图片保存成宽为640px的等比例缩小的图片,怎么做?
...全文
99 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovehooho 2010-05-11
  • 打赏
  • 举报
回复
收藏了
hao2629 2010-05-11
  • 打赏
  • 举报
回复
学习学习 UP
njlywy 2010-05-11
  • 打赏
  • 举报
回复
学习…
ljb07976513524 2010-05-11
  • 打赏
  • 举报
回复
缩率图
duxj007 2010-05-10
  • 打赏
  • 举报
回复
学习~```````
wuyq11 2010-05-10
  • 打赏
  • 举报
回复
通过比例生成缩略图
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
Image originalImage = Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW":
break;
case "W":
toheight = originalImage.Height * width/originalImage.Width;
break;
case "H":
towidth = originalImage.Width * height/originalImage.Height;
break;
case "Cut":
if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height*towidth/toheight;
y = 0;
x = (originalImage.Width - ow)/2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width*height/towidth;
x = 0;
y = (originalImage.Height - oh)/2;
}
break;
default :
break;
}
Image bitmap = new System.Drawing.Bitmap(towidth,toheight);
Graphics g = System.Drawing.Graphics.FromImage(bitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
new Rectangle(x, y, ow,oh),
GraphicsUnit.Pixel);

try
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch(System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}

<script language="JavaScript">
var flag=false;
function DrawImage(ImgD,iwidth,iheight){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}

}
}


qq497525725 2010-05-10
  • 打赏
  • 举报
回复

哥们 这是一个图片缩略方法,里面的高和宽: 如果图片实际宽度小于设置的宽度不做处理,高也是这样.
还有就是这个是等比例缩放.
16:9 的原始图片 ,缩放后也是16:9

这是原始方法,肯定是没有问题,我一直用这个.

/// <summary>
/// 缩放图片不带水印
/// </summary>
/// <param name="hifile"></param>
/// <param name="imgName"></param>
/// <param name="aWidth"></param>
/// <param name="aHeight"></param>
/// <returns></returns>
public string UpImages(FileUpload hifile, string imgName, Unit aWidth, Unit aHeight)
{
string strOldFilePath = hifile.PostedFile.FileName;
//取得上传文件的扩展名
string strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
//生成原图
Byte[] oFileByte = new byte[hifile.PostedFile.ContentLength];
System.IO.Stream oStream = hifile.PostedFile.InputStream;
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);

int oWidth = oImage.Width; //原图宽度
int oHeight = oImage.Height; //原图高度
int tWidth = Convert.ToInt32(aWidth.Value); //设置缩略图初始宽度
int tHeight = Convert.ToInt32(aHeight.Value); //设置缩略图初始高度
if (imgName == "")
{
//图片名
imgName = DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString() + strExtension;
//图片存储相对路径
}
string path = WebPath.PhysicalApplicationPath + "/UpImage/" + imgName;
if (oWidth <= tWidth && oHeight <= tHeight)
{
try
{
hifile.PostedFile.SaveAs(path);
}
catch
{
imgName = "no";
}
}
else
{
//按比例计算出缩略图的宽度和高度
if (oWidth >= oHeight)
{
tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
}
else
{
tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
}

//生成缩略原图
Bitmap tImage = new Bitmap(tWidth, tHeight);
Graphics g = Graphics.FromImage(tImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
g.Clear(Color.Transparent); //清空画布并以透明背景色填充


g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);

//存储图片所需的绝对路径
try
{
//以JPG格式保存图片
tImage.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch
{
imgName = "no";
}
finally
{
//释放资源
oImage.Dispose();
g.Dispose();
tImage.Dispose();
}
}
return imgName;
}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧