图片服务器上图片的管理网站上传与前台显示

    在一些有图片的管理系统中,管理站点由于安全等因素,往往不和前台站点在一个服务器上.这时,要实现图片的管理站点上传,并在前台站点等多站点显示,我们一般会采用单独的图片服务器来实现.
    为了使用方便,我们可以使用自定义控件,如一个文件上传控件,通过指定Ftp站点的相关信息,在文件上传时,自动上传到Ftp服务器.
    而在前台显示时,也可以通过一个继承自Image的控件,从一个地址(图片服务器)来取得图片的缩略图.
    在图片服务器, 我们可以写一个HttpHandler来实现生成缩略图的过程.
    按照这个思路.上传文件时,代码举例如下:
HTML
None.gif   < asp:FtpFileUpload  style ="display:block;"  runat ="server"  ID ="fuPicture"   />
None.gif
编码
None.gif        string  GetGifImageUrl()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif
{
InBlock.gif            
//上传图片

InBlock.gif
            if (this.fuPicture.HasFile)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
{
InBlock.gif        
//返回上传到Ftp服务器后的文件名

InBlock.gif
                return this.fuPicture.Save();
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return "";
ExpandedBlockEnd.gif        }

None.gif
web.config配置
None.gif    <FtpFileUploadConfig Server="192.168.2.2" Port="21" UserName="scimg" Password="scimg@sina163.com" HomePath="" AllowExt=".jpe|.jpeg|.jpg|.png|.tif|.tiff|.bmp"/>
None.gif
在显示图片时,代码举例如下:
None.gif      < asp:RemoteImage  runat ="server"  ID ="imagePicture"  ImageUrl ='<%#  OperData.Picture % > ' />
None.gif
web.config配置如下:
None.gif      < RemoteImageConfig  RemoteHomeUrl ="http://img.xxxxxx.cn/getthumb.aspx"  EnableThumb ="true" />
None.gif
      我们已经看到了使用,下面我们来实现它:
首先是FtpFileUpload控件的实现方法

None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Web.UI.WebControls;
None.gif
using System.Configuration;
None.gif
using System.Collections;
None.gif
using System.Net;
None.gif
using System.IO;
None.gif
using System.Diagnostics;
None.gif
using System.Text.RegularExpressions;
None.gif
None.gif
namespace Iyond.Web.UI.WebControls
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
///     <section name="FtpFileUploadConfig" type="System.Configuration.SingleTagSectionHandler"/>
InBlock.gif    
///   <FtpFileUploadConfig Server="192.168.2.192" Port="21" UserName="happyfen_images" Password="happyfen_images" HomePath="" >
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class FtpFileUpload : FileUpload
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected FtpFileUploadConfig config = new FtpFileUploadConfig();
InBlock.gif
InBlock.gif        
private static Regex regexName = new Regex(@"[^\s]*$", RegexOptions.Compiled);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 检查文件是否存在
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="parentPath">父目录</param>
InBlock.gif        
/// <param name="name">文件名</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        protected bool CheckFileOrPath(string parentPath, string fileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//检查一下日期目录是否存在
InBlock.gif
            FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(config.GetFtpUri(parentPath));
InBlock.gif            req.Credentials 
= config.Credentials;
InBlock.gif            req.Method 
= WebRequestMethods.Ftp.ListDirectoryDetails;
InBlock.gif
InBlock.gif            Stream stream 
= req.GetResponse().GetResponseStream();
InBlock.gif
InBlock.gif            
using (StreamReader sr = new StreamReader(stream))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string line = sr.ReadLine();
InBlock.gif                
while (!string.IsNullOrEmpty(line))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    GroupCollection gc 
= regexName.Match(line).Groups;
InBlock.gif                    
if (gc.Count != 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
throw new ApplicationException("FTP 返回的字串格式不正确");
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
string path = gc[0].Value;
InBlock.gif                    
if (path == fileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return true;
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    line 
= sr.ReadLine();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return false;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected void CreatePath(string parentPath, string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            FtpWebRequest req 
= (FtpWebRequest)FtpWebRequest.Create(config.GetFtpUri(string.Format("{0}/{1}",parentPath,name)));
InBlock.gif            req.Credentials 
= config.Credentials;
InBlock.gif            req.Method 
= WebRequestMethods.Ftp.MakeDirectory;
InBlock.gif            req.GetResponse();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 在Ftp服务器上保存文件,并返回文件名
InBlock.gif        
/// </summary>
InBlock.gif        
/// <returns>保存的文件名以及路径</returns>
InBlock.gif        
/// <remarks>
InBlock.gif        
///     必须在 app.config 中配置
ExpandedSubBlockEnd.gif        
/// </remarks>

InBlock.gif        public string Save()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!this.HasFile)
InBlock.gif                
return string.Empty;
InBlock.gif
InBlock.gif            
if (config.AllowExt.IndexOf(Path.GetExtension(this.FileName)) == -1)
InBlock.gif                
throw new ApplicationException("不允许的文件类型" + Path.GetExtension(this.FileName));
InBlock.gif            
InBlock.gif            
//检查一下日期目录是否存在
InBlock.gif
            string dayPath = DateTime.Today.ToString("yyyyMMdd");
InBlock.gif            
if (!this.CheckFileOrPath("", dayPath))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.CreatePath("", dayPath);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
string fileName = string.Format("{0}_{1}{2}",Path.GetFileNameWithoutExtension(this.FileName),
InBlock.gif                DateTime.Now.TimeOfDay.TotalMilliseconds,
InBlock.gif                Path.GetExtension(
this.FileName));
InBlock.gif
InBlock.gif            
string filePath = string.Format("{0}/{1}",
InBlock.gif                dayPath, fileName
InBlock.gif                );
InBlock.gif
InBlock.gif            FtpWebRequest req 
= (FtpWebRequest)FtpWebRequest.Create(config.GetFtpUri(filePath));
InBlock.gif            req.Credentials 
= config.Credentials;
InBlock.gif            req.Method 
= WebRequestMethods.Ftp.UploadFile;
InBlock.gif            //这么效率应该高点了吧
InBlock.gif            BufferedStream upstream = new BufferedStream(req.GetRequestStream());
InBlock.gif
            for (int byteData = this.FileContent.ReadByte(); byteData != -1; byteData = this.FileContent.ReadByte())
InBlock.gif             {
InBlock.gif                 upstream.WriteByte((byte)byteData);
InBlock.gif             }
InBlock.gif            upstream.Flush();
InBlock.gif
InBlock.gif
            upstream.Close();
InBlock.gif            req.GetResponse();
InBlock.gif
InBlock.gif            
return filePath;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class FtpFileUploadConfig 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private IDictionary config = ConfigurationManager.GetSection("FtpFileUploadConfig"as IDictionary;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// FTP服务器IP
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string Server
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return config["Server"].ToString();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// FTP服务器端口
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string Port
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return config["Port"].ToString();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// FTP服务器登陆用户名
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string UserName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return config["UserName"].ToString();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Ftp服务器登陆密码
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string Password
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return config["Password"].ToString();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 上传的主目录,每个上传的文件建立日期(例:20070203)的目录
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string HomePath
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return config["HomePath"].ToString();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// AllowExt = ".jpe|.jpeg|.jpg|.png|.tif|.tiff|.bmp"
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string AllowExt
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return config["AllowExt"].ToString();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 依配置,生成FTP的URI
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="relationFilePath"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public Uri GetFtpUri(string relationFilePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
string uriString = string.Empty;
InBlock.gif            
if (HomePath != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                uriString 
= string.Format("ftp://{0}:{1}/%2f{2}/{3}", Server, Port, HomePath, relationFilePath);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                uriString 
= string.Format("ftp://{0}:{1}/%2f{2}", Server, Port, relationFilePath);
ExpandedSubBlockEnd.gif            }

InBlock.gif            Uri uri 
= new Uri(uriString);
InBlock.gif
InBlock.gif            
return uri;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 依配置,返回ICredentials的实例
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public ICredentials Credentials
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return new NetworkCredential(UserName, Password);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif

然后是RemoteImage控件的实现
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Collections;
None.gif
using System.Configuration;
None.gif
using System.Web;
None.gif
using System.IO;
None.gif
using System.Drawing;
None.gif
using System.Drawing.Imaging;
None.gif
using System.Web.UI.WebControls;
None.gif
None.gif
namespace Iyond.Web.UI.WebControls
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class RemoteImageHandler : IHttpHandler
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected HttpContext context = null;
InBlock.gif        
static Hashtable htmimes = new Hashtable();
InBlock.gif        
internal readonly string AllowExt = ".jpe|.jpeg|.jpg|.png|.tif|.tiff|.bmp";
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
IHttpHandler Members#region IHttpHandler Members
InBlock.gif
InBlock.gif        
public bool IsReusable
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn false; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void ProcessRequest(HttpContext context)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.context = context;
InBlock.gif            htmimes[
".jpeg"= "image/jpeg";
InBlock.gif            htmimes[
".jpg"= "image/jpeg";
InBlock.gif            htmimes[
".png"= "image/png";
InBlock.gif            htmimes[
".tif"= "image/tiff";
InBlock.gif            htmimes[
".tiff"= "image/tiff";
InBlock.gif            htmimes[
".bmp"= "image/bmp";
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                
string image = context.Request.QueryString["img"];
InBlock.gif                
int width = Convert.ToInt32(context.Request.QueryString["w"]);
InBlock.gif                
int height = Convert.ToInt32(context.Request.QueryString["h"]);
InBlock.gif
InBlock.gif                
string imagePath = context.Request.MapPath(image);
InBlock.gif                
if (!File.Exists(imagePath))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    context.Response.End();
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (width == 0 && height == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    context.Response.Redirect(image);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string imageUrl = GetPicPathUrl(image, width, height);
InBlock.gif                    context.Response.Redirect(imageUrl);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                context.Response.End();
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Helper#region Helper
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获取图像编码解码器的所有相关信息
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="mimeType">包含编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串</param>
ExpandedSubBlockEnd.gif        
/// <returns>返回图像编码解码器的所有相关信息</returns>

InBlock.gif        static ImageCodecInfo GetCodecInfo(string mimeType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ImageCodecInfo[] CodecInfo 
= ImageCodecInfo.GetImageEncoders();
InBlock.gif            
foreach (ImageCodecInfo ici in CodecInfo)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (ici.MimeType == mimeType) return ici;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return null;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 检测扩展名的有效性
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sExt">文件名扩展名</param>
ExpandedSubBlockEnd.gif        
/// <returns>如果扩展名有效,返回true,否则返回false.</returns>

InBlock.gif        bool CheckValidExt(string sExt)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
bool flag = false;
InBlock.gif            
string[] aExt = AllowExt.Split('|');
InBlock.gif            
foreach (string filetype in aExt)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (filetype.ToLower() == sExt)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    flag 
= true;
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return flag;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 保存图片
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="image">Image 对象</param>
InBlock.gif        
/// <param name="savePath">保存路径</param>
ExpandedSubBlockEnd.gif        
/// <param name="ici">指定格式的编解码参数</param>

InBlock.gif        void SaveImage(System.Drawing.Image image, string savePath, ImageCodecInfo ici)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string path = new FileInfo(savePath).DirectoryName;
InBlock.gif            
if (!Directory.Exists(path))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Directory.CreateDirectory(path);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//设置 原图片 对象的 EncoderParameters 对象
InBlock.gif
            EncoderParameters parameters = new EncoderParameters(1);
InBlock.gif            parameters.Param[
0= new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, ((long)90));
InBlock.gif            image.Save(savePath, ici, parameters);
InBlock.gif            parameters.Dispose();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Methods#region Methods
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 生成缩略图
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sourceImagePath">原图片路径(相对路径)</param>
InBlock.gif        
/// <param name="thumbnailImagePath">生成的缩略图路径,如果为空则保存为原图片路径(相对路径)</param>
ExpandedSubBlockEnd.gif        
/// <param name="thumbnailImageWidth">缩略图的宽度(高度与按源图片比例自动生成)</param>

InBlock.gif        public void ToThumbnailImages(string sourceImagePath, string thumbnailImagePath, int nWidth, int nHeight)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string sExt = sourceImagePath.Substring(sourceImagePath.LastIndexOf(".")).ToLower();
InBlock.gif            
if (sourceImagePath.ToString() == System.String.Empty) throw new NullReferenceException("sourceImagePath is null!");
InBlock.gif            
if (!CheckValidExt(sExt))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw new ArgumentException("原图片文件格式不正确,支持的格式有[ " + AllowExt + " ]""sourceImagePath");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//从 原图片 创建 Image 对象
InBlock.gif
            System.Drawing.Image image = System.Drawing.Image.FromFile(sourceImagePath);
InBlock.gif            
//int num = ((thumbnailImageWidth / 4) * 3);
InBlock.gif
            int width = image.Width;
InBlock.gif            
int height = image.Height;
InBlock.gif            
if (nWidth == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                nWidth 
= width;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (nHeight == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                nHeight 
= height;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//计算图片的比例
InBlock.gif
            double nblh = image.Width * 1.0 / nWidth;
InBlock.gif            
double nblv = image.Height * 1.0 / nHeight;
InBlock.gif            
double nBL = Math.Max(nblh, nblv);// nblh > nblv ? nblh:nblv;
InBlock.gif
            int thumbWidth, thumbHeight;
InBlock.gif            
if (nBL > 1.0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                thumbWidth 
= (int)(image.Width / nBL);
InBlock.gif                thumbHeight 
= (int)(image.Height / nBL);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                thumbWidth 
= nWidth;
InBlock.gif                thumbHeight 
= nHeight;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif           
//用指定的大小和格式初始化 Bitmap 类的新实例
InBlock.gif
            Bitmap bitmap = new Bitmap(thumbWidth, thumbHeight, PixelFormat.Format32bppArgb);
InBlock.gif            
//从指定的 Image 对象创建新 Graphics 对象
InBlock.gif
            Graphics graphics = Graphics.FromImage(bitmap);
InBlock.gif            
//清除整个绘图面并以透明背景色填充
InBlock.gif
            graphics.Clear(Color.Transparent);
InBlock.gif            
//在指定位置并且按指定大小绘制 原图片 对象
InBlock.gif
            graphics.DrawImage(image, new Rectangle(00, thumbWidth, thumbHeight));
InBlock.gif            image.Dispose();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//将此 原图片 以指定格式并用指定的编解码参数保存到指定文件 
InBlock.gif
                string savepath = (thumbnailImagePath == null ? sourceImagePath : thumbnailImagePath);
InBlock.gif                SaveImage(bitmap, savepath, GetCodecInfo((
string)htmimes[sExt]));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (System.Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw e;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                bitmap.Dispose();
InBlock.gif                graphics.Dispose();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif
InBlock.gif        
public string GetPicPathUrl(string image, int nWidth, int nHeight)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
const string pathEnd = "_";
InBlock.gif            
if (nWidth == 0 || nHeight == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return image;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
string imageSmallUrl = string.Format("{0}{1}/{2}_{3}{4}", image, pathEnd, nWidth, nHeight, Path.GetExtension(image));
InBlock.gif            
string imagePath = context.Request.MapPath(image);
InBlock.gif            
string imageSmallPath = context.Request.MapPath(imageSmallUrl);
InBlock.gif            
if (!File.Exists(imageSmallPath))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.ToThumbnailImages(imagePath, imageSmallPath, nWidth, nHeight);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return imageSmallUrl;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class RemoteImage : System.Web.UI.WebControls.Image
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected RemoteImageConfig config = new RemoteImageConfig();
InBlock.gif        
protected Unit width = Unit.Empty;
InBlock.gif        
protected Unit height = Unit.Empty;
InBlock.gif        
public override string ImageUrl
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (this.DesignMode)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return base.ImageUrl;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (config.EnableThumb &&
InBlock.gif                    
this.Width.Type == UnitType.Pixel && this.Height.Type == UnitType.Pixel)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return string.Format("{0}?img={1}&w={2}&h={3}", config.RemoteHomeUrl, System.Web.HttpUtility.UrlEncode(base.ImageUrl),
InBlock.gif                        
this.Width.IsEmpty ? 0 : this.Width.Value, this.Height.IsEmpty ? 0 : this.Height.Value);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return string.Format("{0}/{1}", config.RemoteHomeUrl, base.ImageUrl);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
base.ImageUrl = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 宽度,最好指定象素单位,Image服务器会生成相应宽度的缩略图
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public override Unit Width
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return width;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                width 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 高度,最好指定象素单位,Image服务器会生成相应高度的缩略图
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public override Unit Height
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return height;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                height 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public class RemoteImageConfig
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private IDictionary config = ConfigurationManager.GetSection("RemoteImageConfig"as IDictionary;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 图片服务器HttpHandler地址
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string RemoteHomeUrl
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return config["RemoteHomeUrl"].ToString().TrimEnd('\\','/');
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//是否启用缩略图
InBlock.gif
        public bool EnableThumb
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return Convert.ToBoolean(config["EnableThumb"]);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值