-
asp.net生成缩略图(C#)
香水坏坏 发表于2007-7-7 [ASP.NET]这几天哈忙,好久有那么一二天木写BLOG,发段生成缩略图的代码吧
C#代码- private static XmlDocument _menudocument;
- private static object _menulock = new object();
- public static XmlDocument MenuXml
- {
- get
- {
- lock (_menulock)
- {
- if (_menudocument == null)
- {
- _menudocument = new XmlDocument();
- _menudocument.Load(HttpContext.Current.Server.MapPath("~/exp/sitemap.xml"));
- }
- }
- return _menudocument;
- }
- }
- public static Image DrawingBreviaryImage(string path, int width, int height)
- {
- Image orginImg = Image.FromFile(path);
- return DrawingBreviaryImage(orginImg, width, height);
- }
- public static Image DrawingBreviaryImage(Stream stream, int width, int height)
- {
- Image orginImg = Image.FromStream(stream);
- return DrawingBreviaryImage(orginImg, width, height);
- }
- ///
- /// get a breviary image of orginImg,which outter size will be deleted if orginimg size bigger than the rect specify
- ///
- /// orginImg /// rect width /// rect height ///
- public static Image DrawingBreviaryImage(Image orginImg, int width, int height)
- {
- Bitmap ret = new Bitmap(width, height);
- Graphics g = Graphics.FromImage(ret);
- g.CompositingQuality = CompositingQuality.HighSpeed;
- g.SmoothingMode = SmoothingMode.HighSpeed;
- g.Clear(Color.White);
- double scalePercentX = (double)orginImg.Width / (double)width;
- double scalePercentY = (double)orginImg.Height / (double)height;
- int x = 0, y = 0, orginwidth = orginImg.Width, orginHeight = orginImg.Height;
- if (scalePercentX > scalePercentY)
- {
- orginwidth = (int)Math.Floor(width * scalePercentY);
- x = (orginImg.Width - orginwidth) / 2;
- }
- else
- {
- orginHeight = (int)Math.Floor(height * scalePercentX);
- y = (orginImg.Height - orginHeight) / 2;
- }
- g.DrawImage(orginImg, new Rectangle(0, 0, width, height), new Rectangle(x, y, orginwidth, orginHeight), GraphicsUnit.Pixel);
- g.Flush();
- g.Dispose();
- orginImg.Dispose();
- return ret;
- }

