从流获取缩略图

 /// <summary>
        /// 从流获取缩略图
        /// </summary>
        /// <param name="filePathWithFileName"></param>
        private void CreateFavoriteThumb(string filePathWithFileName)
        {
            if (Request.InputStream == null || Request.InputStream.Length == 0)
            {
                throw new Exception("没有接收到缩略图文件流");
            }

            MemoryStream ms = null;
            try
            {
                using (ms = new MemoryStream())
                {
                    byte[] buffer = new byte[Request.InputStream.Length];
                    int read;
                    while ((read = Request.InputStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        ms.Write(buffer, 0, read);
                    }
                    Bitmap b = new Bitmap(ms);
                    b.Save(filePathWithFileName);
                }
            }
            catch
            {
                throw new Exception("缩略图保存失败");
            }
            finally
            {
                if (ms != null)
                {
                    ms.Flush();
                    ms.Close();
                    ms = null;
                }
            }
        }

posted @ 2015-05-26 15:39  mysuper  阅读(199)  评论(0)    收藏  举报