c#多图片上传并生成缩略图的实例代码(3)



   #region 将图片生成缩略图
     /// <summary>
     /// 生成缩略图
     /// </summary>
     private void SmallImg(Stream oStream, string FileName)
     {
         using (System.Drawing.Image img = System.Drawing.Image.FromStream(oStream))
         {
             int newWidth = 100;
             int newHeight = 80;
             int oldWidth = img.Width;
             int oldHeight = img.Height;
             if (oldWidth > oldHeight)
             {
                 newHeight = (int)Math.Floor((double)oldHeight * (double)newWidth / (double)oldWidth);
             }
             else
             {
                 newWidth = (int)Math.Floor((double)oldWidth * (double)newHeight / (double)oldHeight);
             }
             using (Bitmap bmp = new Bitmap(newWidth, newHeight))
             {
                 using (Graphics g = Graphics.FromImage(bmp))
                 {
                     g.Clear(Color.Transparent);
                     g.InterpolationMode = InterpolationMode.High;
                     g.CompositingQuality = CompositingQuality.HighQuality;
                     g.SmoothingMode = SmoothingMode.HighQuality;
                     g.DrawImage(img, new Rectangle(0, 0, newWidth, newHeight), new Rectangle(0, 0, oldWidth, oldHeight), GraphicsUnit.Pixel);
                     string newFileName = Path.GetFileNameWithoutExtension(FileName) + "_small" + Path.GetExtension(FileName);   //缩略图名称
                     string filePath = Server.MapPath("/uploadImg/") + newFileName;
                     bmp.Save(filePath);
                 }
             }

         }
     }
     #endregion

代码有很多需要改进的地方,希望大家多多指点。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjgsgw.html