ASP.NET图像上传与调整大小

前端之家收集整理的这篇文章主要介绍了ASP.NET图像上传与调整大小前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个aspx页面,将上传图像到服务器硬盘从客户端pc

但现在我需要改变我的程序,这将允许我在上传时调整图像的大小。

有人有任何想法吗?我不能找到这些属性/方法与输入文件服务器控制

任何一个有指导我?

解决方法

一旦文件已保存到服务器,您可以使用这样的代码来调整大小。这个代码将在调整大小时处理长宽比。
  1. public static Bitmap CreateThumbnail(string lcFilename,int lnWidth,int lnHeight)
  2. {
  3.  
  4. System.Drawing.Bitmap bmpOut = null;
  5.  
  6. try
  7. {
  8. Bitmap loBMP = new Bitmap(lcFilename);
  9. ImageFormat loFormat = loBMP.RawFormat;
  10.  
  11. decimal lnRatio;
  12. int lnNewWidth = 0;
  13. int lnNewHeight = 0;
  14.  
  15. if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
  16. return loBMP;
  17.  
  18. if (loBMP.Width > loBMP.Height)
  19. {
  20. lnRatio = (decimal)lnWidth / loBMP.Width;
  21. lnNewWidth = lnWidth;
  22. decimal lnTemp = loBMP.Height * lnRatio;
  23. lnNewHeight = (int)lnTemp;
  24. }
  25. else
  26. {
  27. lnRatio = (decimal)lnHeight / loBMP.Height;
  28. lnNewHeight = lnHeight;
  29. decimal lnTemp = loBMP.Width * lnRatio;
  30. lnNewWidth = (int)lnTemp;
  31. }
  32.  
  33.  
  34. bmpOut = new Bitmap(lnNewWidth,lnNewHeight);
  35. Graphics g = Graphics.FromImage(bmpOut);
  36. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  37. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  38. g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  39. g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
  40. g.FillRectangle(Brushes.White,lnNewWidth,lnNewHeight);
  41. g.DrawImage(loBMP,lnNewHeight);
  42.  
  43. loBMP.Dispose();
  44. }
  45. catch
  46. {
  47. return null;
  48. }
  49. return bmpOut;
  50. }

猜你在找的asp.Net相关文章