求一个能实现上传图片实比例缩放

kuuga_8086 2009-09-27 12:04:40
求一个能实现上传图片的方法,图片长宽能按比例缩放。
比如说我要一张width:300 heigh:200 的图片.
能按比例缩放为130*130的一张小图,比例空出来的部分自动用白色背景来填允。
...全文
220 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2009-09-27
  • 打赏
  • 举报
回复
<script language="JavaScript">
var flag=false;
function DrawImage(ImgD){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= 140/120){
if(image.width>140){
ImgD.width=140;
ImgD.height=(image.height*140)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
else{
if(image.height>120){
ImgD.height=120;
ImgD.width=(image.width*120)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
}
}
</script>
onload="javascript:DrawImage1(this)">
上传图片生成缩略图
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage = System.Drawing.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;
}
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
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(System.Drawing.Color.Transparent);
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);

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


liaoyukun111 2009-09-27
  • 打赏
  • 举报
回复
关注
随风落梦 2009-09-27
  • 打赏
  • 举报
回复
学习学习
lovexilove 2009-09-27
  • 打赏
  • 举报
回复
网上一大堆
Rock870210 2009-09-27
  • 打赏
  • 举报
回复
/// <summary>
/// 缩放图片
/// </summary>
/// <param name="img">原图片</param>
/// <param name="iWidth">新的宽度</param>
/// <param name="iHeight">新的高度</param>
/// <returns></returns>
public static Image ZoomBitmap(Image img, int iWidth, int iHeight)
{
if (img == null) return new Bitmap(iWidth, iHeight);//返回空图像

Bitmap small = new Bitmap(iWidth, iHeight);
Graphics g = Graphics.FromImage(small);
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.Clear(Color.Transparent);
g.DrawImage(img, 0, 0, iWidth, iHeight);
return small;
}
闪亮的人生 2009-09-27
  • 打赏
  • 举报
回复
学习了
jenny0810 2009-09-27
  • 打赏
  • 举报
回复
学习
kuuga_8086 2009-09-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
<script  language="JavaScript"> 
var  flag=false; 
function  DrawImage(ImgD){ 
    var  image=new  Image(); 
    image.src=ImgD.src; 
    if(image.width>0  &&  image.height>0){ 
      flag=true; 
      if(image.width/image.height>=  140/120){ 
        if(image.width>140){     
        ImgD.width=140; 
        ImgD.height=(image.height*140)/image.width; 
        }else{ 
        ImgD.width=image.width;     
        ImgD.height=image.height; 
        } 
        } 
      else{ 
        if(image.height>120){     
        ImgD.height=120; 
        ImgD.width=(image.width*120)/image.height;           
        }else{ 
        ImgD.width=image.width;     
        ImgD.height=image.height; 
        } 
        } 
      } 
}     
</script> 
onload="javascript:DrawImage1(this)">
上传图片生成缩略图
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage = System.Drawing.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;
}
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
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(System.Drawing.Color.Transparent);
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);

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



[/Quote]

是页面跟后台结合一起使用?
liaoyukun111 2009-09-27
  • 打赏
  • 举报
回复
public void tothumbimage(string imagefrom, string imageto, int width , int height)
{
try
{
Bitmap bmp = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bmp);
Bitmap bmpfile = new Bitmap(imagefrom);

Rectangle sourceRectangle = new Rectangle(0, 0, bmpfile.Width, bmpfile.Height);
Rectangle destRectangle = new Rectangle(0, 0, width, height);
g.DrawImage(bmpfile, destRectangle, sourceRectangle, GraphicsUnit.Pixel);

g.DrawImage(bmp, new Point(0, 0));
bmp.Save(imageto);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}

}
chenyunkun2008 2009-09-27
  • 打赏
  • 举报
回复
顶楼上的,楼主试试

62,050

社区成员

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

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

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

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