Use asp.net to reduce the size of uploaded images and store them in the database.

Source: Internet
Author: User
With the image upload function, I save image data to the database. I need to reduce the image size and save it to the database with the original image. In this way, a lot of small images can be displayed first, and then a large image will be displayed! This is done in Asp.net:
Code in changimage. aspx:
<% @ 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 = ""> <INPUT id = "File1" style = "Z-INDEX: 101; LEFT: 291px; WIDTH: 180px; POSITION: absolute; TOP: 119px; HEIGHT: 45px "type =" file "size =" 10 "name =" File1 "runat =" server "> <asp: Button id =" cmdupload "style =" Z-INDEX: 103; LEFT: 402px; POSITION: absolute; TOP: 194px "runat =" server "Text =" Upload image "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 release demo 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 call is 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 As 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 () 'big image data, small image data
Dim pic As System. Data. SqlClient. SqlParameter, picsmall As System. Data. SqlClient. SqlParameter
'The checking whether the file to be uploaded meets the standards. I wrote the check function based on website requirements.
If check (File1.PostedFile. FileName) <> "OK" Then
Response. Write (check (File1.PostedFile. FileName ))
Exit Sub
End If
'Set the temporary path. To prevent conflicts during multi-user access, an application object is set.
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 ()
'Reading image data
ReDim bigdata (Me. File1.PostedFile. InputStream. Length)
Me. File1.PostedFile. InputStream. Read (bigdata, 0, UBound (bigdata) 'Read the original image data to bigdata
'Change the image size
Image = System. Drawing. Image. FromStream (Me. File1.PostedFile. InputStream)
'Newimage size can also be set. I only use 80*60 and 60*80.
If image. Width> image. Height Then
Newimage = image. GetThumbnailImage (80, 60, callb, New System. IntPtr (0 ))
Else
Newimage = image. GetThumbnailImage (60, 80, callb, New System. IntPtr (0 ))
End If
Image. Dispose ()
'Save the new image and image to the 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 above method for obtaining small images I originally 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)
'However, the smalldata read by the above method is empty. Please advise if you do not know the reason.
'Delete temporary files
If f. Exists (temppath) Then
F. Delete (temppath)
End If
'Add data to the database
'Because the database has an image field, I Can't insert it in SQL, so I use a stored procedure.
'The question is how to write a table with an image field inserted in SQL statements
'Use insert into talbe (pic, picsmall) values ("& bigdata &", "& smalldata &"). You can't use '"& bigdata!
SqlConn = New System. Data. SqlClient. SqlConnection (connstr) 'You can set 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

The stored procedure of dbo. image is as follows:
Create proc dbo. image
@ Pic image,
@ Picsmall image
As
Insert into table (pic, picsmall) values (@ pic, @ picsmall)

Lead http://www.mikecat.net//trackback.asp? TbID = 1, 419

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.