Using ASP.net to make the uploaded images smaller and stored in the database

Source: Internet
Author: User
Tags insert visual studio
asp.net| Upload | data | Database changimage.aspx code:
<%@ Page language= "vb" autoeventwireup= "false" codebehind= "Changimage.aspx.vb" inherits= "Uploadimage.changimage" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title></title>
<meta name= "generator" content= Microsoft Visual Studio.NET 7.0 >
<meta name= "Code_language" content= "Visual Basic 7.0" >
<meta name= "vs_defaultClientScript" content= "JavaScript" >
<meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" >
</HEAD>
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "post" enctype= "Multipart/form-data" runat= "Server" >
<font face= "Song Body" ><input id= "File1" style=; left:291px; width:180px; Position:absolute; top:119px; height:45px "type=" file "size=" Name= "File1" runat= "Server" > <asp:button id= "cmdupload" "style=" z-index:103; left:402px; Position:absolute; TOP:194PX "runat=" server "text=" Upload picture "width=" 81px "height=" 42px "></asp:Button>
</FONT>
</form>
</body>
</HTML>
The code in Changimage.aspx.vb is as follows:
Public Class Changimage
Inherits System.Web.UI.Page
Protected WithEvents Cmddemo as System.Web.UI.WebControls.Button
Protected WithEvents Cmdupload as System.Web.UI.WebControls.Button
Protected WithEvents Sqlconn as System.Data.SqlClient.SqlConnection
Protected WithEvents Sqlcomm as System.Data.SqlClient.SqlCommand
Protected WithEvents File1 as System.Web.UI.HtmlControls.HtmlInputFile #Region "Web Form Designer generated Code" ' This The call was required by the Web Form Designer.
<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()
Me.sqlconn = New System.Data.SqlClient.SqlConnection ()
Me.sqlcomm = New System.Data.SqlClient.SqlCommand () End Sub Private Sub Page_Init (ByVal sender as System.Object, ByVal e A S System.EventArgs) Handles Mybase.init
Form Designer
InitializeComponent ()
End Sub #End Region Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
End Sub Private Sub Cmdupload_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles cmdupload. Click
Dim image as System.Drawing.Image, newimage as System.Drawing.Image
Dim CALLB as System.Drawing.Image.GetThumbnailImageAbort
Dim f as System.IO.File, FS as System.IO.FileStream
Dim TempPath as String
Dim Bigdata as Byte (), Smalldata as Byte () ' Large picture data, small picture data
Dim pic as System.Data.SqlClient.SqlParameter, picsmall as System.Data.SqlClient.SqlParameter
' Check whether the upload file is up to standard, I need to write it according to the website
If Check (File1.PostedFile.FileName) <> "OK" Then
Response.Write (Check (File1.PostedFile.FileName))
Exit Sub
End If
' Set a temporary path, to prevent conflicts when multiuser access, set a Application object
IF application ("image") = "" Then
Application ("image") = 0
End If
Application.Lock ()
TempPath = Server.MapPath (CStr (Application ("image")) ' Temporary path
Application ("image") = Application ("image") + 1
Application.UnLock ()
' Read the picture's data
ReDim Bigdata (Me.File1.PostedFile.InputStream.Length)
Me.File1.PostedFile.InputStream.Read (bigdata, 0, UBound (bigdata)) ' reads the original picture data into the Bigdata
' Change the size of the picture
Image = System.Drawing.Image.FromStream (Me.File1.PostedFile.InputStream)
' The size in NewImage can also be set, I only used 80*60 and 60*80 two kinds
If image. Width > Image. Height Then
NewImage = image. Getthumbnailimage (CALLB, New system.intptr (0))
Else
NewImage = image. Getthumbnailimage (CALLB, New system.intptr (0))
End If
Image. Dispose ()
' Make new pictures and pictures smaller and save them in a temporary path
NewImage. Save (TempPath, System.Drawing.Imaging.ImageFormat.Jpeg)
NewImage. Dispose ()
' Read temporary file data to Smalldata
FS = New System.IO.FileStream (TempPath, IO. FileMode.Open, IO. FileAccess.Read)
ReDim Smalldata (fs. Length)
Fs. Read (smalldata, 0, UBound (smalldata))
Fs. Close ()
' The way I used to get the little picture I wanted to use System.IO.MemoryStream, but it didn't work: The code is as follows:
' Dim m as System.IO.MemoryStream
' M=new System.IO.MemoryStream ()
' Newimage.save (m,system.drawing.imaging.imageformat.jpeg)
' ReDim Smalldata (M.length)
' M.read (smalldata,0,m.length)
But the above method reads the Smalldata all is empty, does not know the reason, please advise
' Delete temporary files
If f.exists (TempPath) Then
F.delete (TempPath)
End If
' Adding data to the database
' Because there is an image field in the database, I can't plug it in with SQL and use a stored procedure
' Ask you, prawn. Insert a table with an image field with an SQL statement how to write
' INSERT into Talbe (Pic,picsmall) VALUES ("&bigdata&", "&smalldata&") so no, with ' &bigdata& ' Not even!
Sqlconn = New System.Data.SqlClient.SqlConnection (connstr) ' can set up the connstr to connect to the database server
Sqlcomm = New System.Data.SqlClient.SqlCommand ()
Sqlcomm.commandtype = CommandType.StoredProcedure
Sqlcomm.commandtext = "Dbo.image"
Pic = New System.Data.SqlClient.SqlParameter ("@pic", Sqldbtype.image)
Pic. Value = Bigdata
Picsmall = New System.Data.SqlClient.SqlParameter ("@picsmall", Sqldbtype.image)
Picsmall. Value = Smalldata
SqlComm.Parameters.Add (pic)
SQLCOMM.PARAMETERS.ADD (Picsmall)
Sqlcomm.connection = sqlconn
SqlComm.Connection.Open ()
Sqlcomm.executenonquery ()
SqlComm.Connection.Close ()
Sqlcomm.dispose ()
Sqlconn.dispose ()
End Sub
End Class Dbo.image stored procedures are as follows:
Create proc Dbo.image
@pic image,
@picsmall image
As
Insert into table (Pic,picsmall) VALUES (@pic, @picsmall)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.