如何实现将用户上传的文件生成缩略图!

时间:2021-12-05 20:31:52

具体给个例子就清楚啦。。
如下:

System.Web.HttpPostedFile UpFile = UpMhtFile.PostedFile;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

              System.IO.Stream StreamObject = UpFile.InputStream;

              System.IO.FileStream myStream = new System.IO.FileStream(UpFile.FileName,System.IO.FileMode.Open,System.IO.FileAccess.Read);

             

              System.Drawing.Image.GetThumbnailImageAbort myCallback =

                   new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

 

              System.Drawing.Image img1 = System.Drawing.Image.FromStream(StreamObject);

          

              System.Drawing.Image timg = img1.GetThumbnailImage(100,100,myCallback,System.IntPtr.Zero);

              timg.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);


上例中UpMhtFile是一个upfile WebControl,上述情况将上传的图片转换成流,然后生成缩略图后直接保存在输出流中,一般用于从数据库中读出流后生成图片浏览。