问题描述
asp.net(VB)上传和下载并生成缩略图(初始代码)<%@PageLanguage="VB"AutoEventWireup="false"CodeFile="1.aspx.vb"Inherits="Thumbnail"%><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN"><HTML><HEAD><title>在ASP.NET里轻松实现缩略图</title></HEAD><bodyMS_POSITIONING="GridLayout"><asp:Labelid="Label1"runat="server"></asp:Label><formid="Form1"method="post"runat="server"enctype="multipart/form-data"><INPUTtype="file"name="file"width="600"><br><br><asp:Buttonid="Button1"runat="server"></asp:Button><asp:ButtonID="Button2"runat="server"Text="Button"/></form></body></HTML>ImportsSystemImportsSystem.WebImportsSystem.DrawingImportsSystem.IOImportsSystem.Drawing.ImagingPublicClassThumbnailInheritsSystem.Web.UI.Page'ProtectedWithEventsLabel1AsSystem.Web.UI.WebControls.Label'ProtectedWithEventsButton1AsSystem.Web.UI.WebControls.Button#Region"WebFormDesignerGeneratedCode"'ThiscallisrequiredbytheWebFormDesigner.<System.Diagnostics.DebuggerStepThrough()>PrivateSubInitializeComponent()EndSubPrivateSubPage_Init(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Init'CODEGEN:ThismethodcallisrequiredbytheWebFormDesigner'Donotmodifyitusingthecodeeditor.InitializeComponent()EndSub#EndRegionPrivateSubPage_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.LoadLabel1.Text="<h3>在ASP.NET里轻松实现缩略图</h3>"Button1.Text="上载并显示缩略图"EndSubPrivateSubButton1_Click(ByValsenderAsObject,ByValeAsSystem.EventArgs)HandlesButton1.ClickDimMyFileCollAsHttpFileCollection=HttpContext.Current.Request.FilesDimMyPostedFileAsHttpPostedFile=MyFileColl.Item(0)IfLCase(MyPostedFile.ContentType.ToString()).IndexOf("image")<0ThenResponse.Write("无效的图形格式。")ExitSubEndIfGetThumbNail(MyPostedFile.FileName,200,100,MyPostedFile.ContentType.ToString(),True,MyPostedFile.InputStream)EndSubPrivateFunctionGetImageType(ByValstrContentType)AsSystem.Drawing.Imaging.ImageFormatSelectCase(strContentType.ToString().ToLower())Case"image/jpeg"GetImageType=System.Drawing.Imaging.ImageFormat.JpegCase"image/gif"GetImageType=System.Drawing.Imaging.ImageFormat.GifCase"image/bmp"GetImageType=System.Drawing.Imaging.ImageFormat.BmpCase"image/tiff"GetImageType=System.Drawing.Imaging.ImageFormat.TiffCase"image/x-icon"GetImageType=System.Drawing.Imaging.ImageFormat.IconCase"image/x-png"GetImageType=System.Drawing.Imaging.ImageFormat.PngCase"image/x-emf"GetImageType=System.Drawing.Imaging.ImageFormat.EmfCase"image/x-exif"GetImageType=System.Drawing.Imaging.ImageFormat.ExifCase"image/x-wmf"GetImageType=System.Drawing.Imaging.ImageFormat.WmfCaseElseGetImageType=System.Drawing.Imaging.ImageFormat.MemoryBmpEndSelectEndFunctionPrivateSubGetThumbNail(ByValstrFileNameAsString,ByValiWidthAsInt16,ByValiheightAsInt16,ByValstrContentTypeAsString,_ByValblnGetFromFileAsBoolean,ByValImgStreamAsStream)DimoImgAsImageIfblnGetFromFileThenoImg=Image.FromFile(strFileName)ElseoImg=Image.FromStream(ImgStream)EndIf'Response.Write(oImg.Width)oImg=oImg.GetThumbnailImage(iWidth,iheight,Nothing,IntPtr.Zero)'DimstrGuidAsString=Guid.NewGuid().ToString().ToUpper()DimstrFileExtAsString=strFileName.Substring(strFileName.LastIndexOf("."))'保存到本地oImg.Save("E:网站sooying1"&strFileExt,GetImageType(strContentType))''直接输出url文件''Response.Redirect("images/"+strGuid+strFileExt)''以下显示在屏幕上Response.ContentType=strContentTypeDimMemStreamAsNewMemoryStream()'注意:这里如果直接用oImg.Save(Response.OutputStream,GetImageType(strContentType))'对不同的格式可能会出错,比如Png格式。oImg.Save(MemStream,System.Drawing.Imaging.ImageFormat.Jpeg)MemStream.WriteTo(Response.OutputStream)EndSubProtectedSubButton2_Click(ByValsenderAsObject,ByValeAsSystem.EventArgs)HandlesButton2.ClickGetThumbNail("http://www.kuulee.com/files/beyondpic/2007-2/23/0722321152951189.jpg",200,200,"jpg",False,ypic())EndSubFunctionypic()AsIO.StreamDimwReqAsNet.WebRequest=Net.WebRequest.Create("http://www.kuulee.com/files/beyondpic/2007-2/23/0722321152951189.jpg")DimwRespAsNet.WebResponse=wReq.GetResponse()DimrespStreamAsIO.Stream=wResp.GetResponseStream()ReturnrespStreamEndFunctionEndClass我这里不能用。说是GDI+中发生一般性错误。请高手指点。