生成高清晰度的缩略图

以下是我在网上找到的,经验证失真很小,高质量  
  //调用示例  
  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();  
  }  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你需要性能生成略图的Java例子,可以尝试使用Java图像处理库`imgscalr`。`imgscalr`是一个基于Java的性能图像放库,它可以在生成略图时提供较好的性能和质量。 要使用`imgscalr`库,你需要添加以下依赖到你的项目中: ```xml <dependency> <groupId>org.imgscalr</groupId> <artifactId>imgscalr-lib</artifactId> <version>4.2</version> </dependency> ``` 下面是一个使用`imgscalr`生成略图的例子: ```java import org.imgscalr.Scalr; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class ThumbnailGenerator { public static void generateThumbnail(String imagePath, String outputDirectory, int thumbnailWidth, int thumbnailHeight) throws IOException { BufferedImage originalImage = ImageIO.read(new File(imagePath)); BufferedImage thumbnail = Scalr.resize(originalImage, thumbnailWidth, thumbnailHeight); String outputFilePath = outputDirectory + File.separator + getFileName(imagePath); ImageIO.write(thumbnail, "jpg", new File(outputFilePath)); } private static String getFileName(String filePath) { File file = new File(filePath); return file.getName(); } } ``` 在这个例子中,我们使用`ImageIO.read`读取原始图片,并使用`Scalr.resize`方法来生成指定宽度和度的略图。然后,我们将略图保存到指定的输出目录。 你可以根据需要调整略图的宽度和度,并使用`Scalr.Method`来指定放的算法(例如,`Scalr.Method.ULTRA_QUALITY`提供了更的质量但速度较慢)。 使用`imgscalr`库,你可以获得更好的性能和质量,同时也能避免内存溢出问题。记得在项目中添加相应的依赖,并根据需要调整生成略图的逻辑。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值