导航

  1. 常用CHM
  2. 订阅
  • 本站大部分内容从网上收集,收集目的仅供研究、学习。涉及版权或不希望收录您的文章请您及时与我联系。
  • 本站IM群,请自行选择。请各位朋友按照自己喜好加入。加入群后请及时发言,防止被清理。谢谢您的合作!!!
  • QQ群:Y①WEB开发(ASP.NET)号码:7351660 QQ群:Y②WEB开发(ASP+.NET)号码:11864905
  • QQ群:Y③WEB开发(DIV+CSS)号码:16610506 QQ群:Y④WEB开发(JS+AJAX)号码:16143998
  • QQ群:Y⑤WEB开发(新手)号码:12777715 MSN群:yaosansi[at]126.com
« C#中使用快速排序按文件创建时间将文件排序web标准常见问题集合 »

Js缩放WEB页面图片大小及C#无失真缩小图片

分类: C#|DOTNET 发布: yaosansi 浏览: 日期: 2007年2月22日

JS缩放图片大小

<script language="JavaScript" >
var flag=false;
function changeImg(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;
  }
 
  }
}  
</script>



C#无失真缩小图片


public static byte[] ResizeImageFile(byte[] imageFile, int targetSizeW, int targetSizeH)
{
   System.Drawing.Image original = System.Drawing.Image.FromStream(new MemoryStream(imageFile));
   int targetH, targetW;
   targetW = targetSizeW;
   targetH = (int)(original.Height * ((float)targetSizeW / (float)original.Width));
   if (targetH > targetSizeH)
   {
      targetH = targetSizeH;
      targetW = (int)(original.Width * ((float)targetSizeH / (float)original.Height));
   }
   if (targetSizeW < (int)original.Width || targetSizeH < (int)original.Height)
   {
      System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(new MemoryStream(imageFile));
      // Create a new blank canvas.  The resized image will be drawn on this canvas.
      Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
      bmPhoto.SetResolution(72, 72);
      Graphics grPhoto = Graphics.FromImage(bmPhoto);
      grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
      grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
      grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
      grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);
      // Save out to memory and then to a file.  We dispose of all objects to make sure the files don't stay locked.
      MemoryStream mm = new MemoryStream();
      bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
      original.Dispose();
      imgPhoto.Dispose();
      bmPhoto.Dispose();
      grPhoto.Dispose();
      return mm.GetBuffer();
   }
   else
   {
      return imageFile;
   }
}

相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-Blog 1.8 Walle Build 100427

Copyright 2005-2010 yaosansi'site All Rights Reserved.
感谢系统大玩家为本站提供FTP空间
辽ICP备05021434号