• asp.net生成缩略图(C#)

    香水坏坏 发表于2007-7-7 [ASP.NET]

    这几天哈忙,好久有那么一二天木写BLOG,发段生成缩略图的代码吧

     

    C#代码
    1. private static XmlDocument _menudocument;   
    2. private static object _menulock = new object();   
    3.   
    4. public static XmlDocument MenuXml   
    5. {   
    6. get  
    7. {   
    8. lock (_menulock)   
    9. {   
    10. if (_menudocument == null)   
    11. {   
    12. _menudocument = new XmlDocument();   
    13. _menudocument.Load(HttpContext.Current.Server.MapPath("~/exp/sitemap.xml"));   
    14.   
    15. }   
    16. }   
    17. return _menudocument;   
    18. }   
    19. }   
    20.   
    21. public static Image DrawingBreviaryImage(string path, int width, int height)   
    22. {   
    23. Image orginImg = Image.FromFile(path);   
    24. return DrawingBreviaryImage(orginImg, width, height);   
    25. }   
    26. public static Image DrawingBreviaryImage(Stream stream, int width, int height)   
    27. {   
    28. Image orginImg = Image.FromStream(stream);   
    29. return DrawingBreviaryImage(orginImg, width, height);   
    30. }   
    31. ///    
    32. /// get a breviary image of orginImg,which outter size will be deleted if orginimg size bigger than the rect specify   
    33. ///    
    34. /// orginImg /// rect width /// rect height ///    
    35. public static Image DrawingBreviaryImage(Image orginImg, int width, int height)   
    36. {   
    37. Bitmap ret = new Bitmap(width, height);   
    38. Graphics g = Graphics.FromImage(ret);   
    39.   
    40. g.CompositingQuality = CompositingQuality.HighSpeed;   
    41. g.SmoothingMode = SmoothingMode.HighSpeed;   
    42. g.Clear(Color.White);   
    43.   
    44. double scalePercentX = (double)orginImg.Width / (double)width;   
    45. double scalePercentY = (double)orginImg.Height / (double)height;   
    46.   
    47. int x = 0, y = 0, orginwidth = orginImg.Width, orginHeight = orginImg.Height;   
    48. if (scalePercentX > scalePercentY)   
    49. {   
    50. orginwidth = (int)Math.Floor(width * scalePercentY);   
    51. x = (orginImg.Width - orginwidth) / 2;   
    52. }   
    53. else  
    54. {   
    55. orginHeight = (int)Math.Floor(height * scalePercentX);   
    56. y = (orginImg.Height - orginHeight) / 2;   
    57. }   
    58. g.DrawImage(orginImg, new Rectangle(0, 0, width, height), new Rectangle(x, y, orginwidth, orginHeight), GraphicsUnit.Pixel);   
    59. g.Flush();   
    60. g.Dispose();   
    61. orginImg.Dispose();   
    62. return ret;   
    63. }   
    64.   

     

    Hits: 685  Feedback: 1  标签:缩略图  Trackback  Permalink: http://www.aspstat.com/37