ASP.NET(vb.net) & Upload and Resize Image - The in this tutorial, you’ll learn and example scripts how to Upload and resize image using by ASP.NET scripts.
ShotDev Focus:
- ASP.NET(vb.net) & Upload and Resize Image
Example
AspNetUploadResizeImages.aspx
<%@ Import Namespace="System.Drawing" %> <%@ Import Namespace="System.Drawing.Imaging" %> <%@ Page Language="VB" %> <script runat="server"> Sub btnUpload_OnClick(sender As Object, e As EventArgs) If Me.fiUpload.HasFile Then Dim intWidth,intHeight As Integer Dim UlFileName,NewFileName As String intWidth = 100 '*** Fix Width ***' 'intHeight = 0 '*** If = 0 Auto Re-Cal Size ***' intHeight = 120 UlFileName = "MyImages/" & fiUpload.FileName '*** Save Images ***' Me.fiUpload.SaveAs(Server.MapPath(UlFileName)) NewFileName = "MyImages/Thumbnail_" & fiUpload.FileName Dim objGraphic As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath(UlFileName)) Dim objBitmap As Bitmap '*** Calculate Height ***' If intHeight > 0 Then objBitmap = New Bitmap(objGraphic, intWidth, intHeight) Else If objGraphic.Width > intWidth Then Dim ratio As Double = objGraphic.Height / objGraphic.Width intHeight = ratio * intWidth objBitmap = New Bitmap(objGraphic, intWidth, intHeight) Else objBitmap = New Bitmap(objGraphic) End If End If '*** Save As ***' objBitmap.Save(Server.MapPath(NewFileName), objGraphic.RawFormat) '*** Close ***' objGraphic.Dispose() '*** View Images ***' Me.imgPicture.Visible = True Me.imgPicture.ImageURL = NewFileName End IF End Sub </script> <html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <form id="form1" runat="server"> <asp:FileUpload id="fiUpload" runat="server"></asp:FileUpload> <input id="btnUpload" type="button" OnServerClick="btnUpload_OnClick" value="Upload" runat="server" /> <hr /> <asp:Image id="imgPicture" Visible="false" runat="server" /><br /><br /> </form> </body> </html>
Screenshot
2enquiry…
…