当前位置:编程学习 > asp >>

缩略图多路径多格式保存

using System;
using System.Drawing;

namespace PubLib
{
 /// <summary>
 /// PicShow 的摘要说明。
 /// </summary>
 public class PicShow
 {
  public PicShow()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }

  //查找图片文件是否存在
  public static string ViewPIC(string PicPath, string PicName)
  {
   
   string BigPic = Checks.HM_PHYSICSROOT + "MoviePIC/"+Checks.HM_PICROOTPATH+"/"+PicName;
   string SmlPic = Checks.HM_PHYSICSROOT + "MoviePIC/"+PicPath+"/"+PicName;

   if (null==PicName || false==System.IO.File.Exists(BigPic)) //大图名称为空或文件不存在
    PicName = "nopic.jpg";

   string OutPic = "MoviePIC/"+PicPath+"/"+PicName;

   if (!System.IO.File.Exists(SmlPic)) //查找小图不存在
   {
    CreatePIC(PicPath, PicName);
    return OutPic;
   }
   return OutPic;
  }

  //创建缩略图
  public static void CreatePIC(string PicPath, string PicName)
  {
   int iWidth,iHeight;
   if (null!=PicPath && PicPath.IndexOf("X")>1)
   {
    char[] spliter = {X};
    string[] aPicPath = PicPath.Split(spliter,2);
    iWidth  = Int32.Parse(aPicPath[0]);
    iHeight = Int32.Parse(aPicPath[1]);

    string BigPic = Checks.HM_PHYSICSROOT + "MoviePIC/"+Checks.HM_PICROOTPATH+"/"+PicName;
    string SmlPic = Checks.HM_PHYSICSROOT + "MoviePIC/"+PicPath+"/"+PicName;
    Image BigImage = Image.FromFile(BigPic);
    Image SmlImage = BigImage.GetThumbnailImage(iWidth,iHeight,null,new System.IntPtr());
    SmlImage.Save(SmlPic,System.Drawing.Imaging.ImageFormat.Jpeg);
    BigImage.Dispose();
    SmlImage.Dispose();
   }
  }
 }
}

Checks.HM_PHYSICSROOT 是一个静态变量是系统的根路径,你问怎么获得?其实很简单你可以直接写个路径给它。虽然不灵活但也还算简单。我是这么搞的

public static string HM_PHYSICSROOT   = AppDomain.CurrentDomain.BaseDirectory;

(其实也简单顾弄玄虚:~)

多路径,也就是说的有点吓人而已,你在生成缩略图的时候指定一个路径就可以了。就是这个参数PicPath是要你手工指定的,多格式说白了就是大小可以调整。我的方法是路径就是大小,比如200*300的图片路径就叫200X300嘿嘿,当然要指定一个图片原始路径,不然从那里得到图片来生成缩略啊。这个Checks.HM_PICROOTPATH就只能手写了,没什么办法。反正一次写了以后也不改动了。


补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,