Asp.net code for uploading images and generating thumbnails at the same time

Source: Internet
Author: User

Copy codeThe Code is as follows:
<% @ Page Language = "C #" ResponseEncoding = "gb2312" %>
<% @ Import Namespace = "System" %>
<% @ Import Namespace = "System. IO" %>
<% @ Import Namespace = "System. Drawing" %>
<% @ Import Namespace = "System. Drawing. Imaging" %>
<Script runat = "server">
 
Void Page_Load (Object sender, EventArgs e)
{
If (! Page. IsPostBack)
{
ImgPreview. Visible = false;
}
}
Void GetThumbnailImage (int width, int height, string strInfo, int left, int right)
{
String file = "Uploads/" + uploadFile. PostedFile. FileName. Substring (uploadFile. PostedFile. FileName. LastIndexOf ('\') + 1 );
String newfile = "Uploads/" + uploadFile. PostedFile. FileName. Substring (uploadFile. PostedFile. FileName. LastIndexOf ('\') + 1) + ". jpg ";
String strAdd = strInfo;
System. Drawing. Image oldimage = System. Drawing. Image. FromFile (Server. MapPath (file ));
System. Drawing. Image thumbnailImage =
Oldimage. GetThumbnailImage (width, height, new System. Drawing. Image. GetThumbnailImageAbort (ThumbnailCallback), IntPtr. Zero );
Response. Clear ();
Bitmap output = new Bitmap (thumbnailImage );
Graphics g = Graphics. FromImage (output );
G. DrawString (strAdd, new Font ("Courier New", 14), new SolidBrush (Color. Red), left, right );
Output. Save (Server. MapPath (newfile), System. Drawing. Imaging. ImageFormat. Jpeg );
Response. ContentType = "image/gif ";
ImgPreview. Visible = true;
ImgPreview. ImageUrl = newfile;
}
Bool ThumbnailCallback ()
{
Return true;
}
Void Button_Click (object sender, EventArgs e)
{
Int width, height, left, right;
String strAddInfo = txtAddInfo. Text;
Width = Int32.Parse (txtWidth. Text );
Height = Int32.Parse (txtHeight. Text );
Left = Int32.Parse (txtLeft. Text );
Right = Int32.Parse (txtRight. Text );
If (! (UploadFile. PostedFile. ContentLength> 0 ))
{
LblErrInfo. Text = "no file selected ";
}
Else
{
String path = Server. MapPath ("./Uploads/" + uploadFile. PostedFile. FileName. Substring (uploadFile. PostedFile. FileName. LastIndexOf ('\') + 1 ));
If (File. Exists (path ))
{
LblErrInfo. Text = "a file with the same name already exists ";
}
Else
{
UploadFile. PostedFile. SaveAs (path );
GetThumbnailImage (width, height, strAddInfo, left, right );
}
}
}
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> upload an image and generate a thumbnail </title>
</Head>
<Body>
<Form id = "Form1" method = "post" enctype = "multipart/form-data" runat = "server">
<P>
<Input id = "uploadFile" type = "file" runat = "server"/>
<Asp: Label ID = "lblErrInfo" runat = "server" ForeColor = "Red"> </asp: Label>
</P>
<P>
Width: <asp: TextBox ID = "txtWidth" runat = "server" Width = "40px"> 100 </asp: TextBox>
Height: <asp: TextBox ID = "txtHeight" runat = "server" Width = "40px"> 150 </asp: TextBox>
</P>
<P>
Add information: <asp: TextBox ID = "txtAddInfo" runat = "server"> AspxBoy. Com </asp: TextBox>
</P>
<P>
Information location: left: <asp: TextBox ID = "txtLeft" runat = "server" Width = "40px"> 10 </asp: TextBox>
Right: <asp: TextBox ID = "txtRight" runat = "server" Width = "40px"> 135 </asp: TextBox>
</P>
<P>
<Input id = "button" type = "button" value = "upload to generate a thumbnail" onserverclick = "Button_Click" runat = "server"/>
</P>
<P>
<Asp: Image ID = "ImgPreview" runat = "server"> </asp: Image>
</P>
<! -- Insert content here -->
</Form>
</Body>
</Html>

Asp.net (vbscript) uploads images and generates thumbnails at the same time:
Copy codeThe Code is as follows:
<Script language = "VB" runat = "server">
Sub UploadFile (sender As Object, e As EventArgs)
If FileUp. PostedFile. ContentLength = 0 Then
FileInfo. Visible = False
Exit Sub
Else
FileInfo. Visible = True
FDisplay1.Visible = True
End If
FSize. Text = "Size of the uploaded file" + CStr (FileUp. PostedFile. ContentLength/1024) + "KB"
FName. Text = "uploaded file name:" + FileUp. PostedFile. FileName + "<br>" + FName. Text
"Write to database
On error resume next
Dim myconn as sqlconnection
Dim mycomm as sqlcommand
Dim SQL as string
Dim id as integer
Dim image, anewimage As System. Drawing. Image
Dim width, height, newwidth, newheight as integer
Dim callb As System. Drawing. Image. GetThumbnailImageAbort
MyConn = New sqlconnection (ConfigurationSettings. deleettings ("Database "))
Myconn. open ()
SQL = "insert into picture (name, class, Introduction, attribute) values (" "& request. cookies ("dgxyl "). values ("dgxylname") & "", "" & request. cookies ("dgxyl "). values ("dgxylbj") & "", "" & trim (request ("TextBox1") & "", "" & request ("r1 ")&"")"
Mycomm = New sqlcommand (SQL, myconn)
Mycomm.exe cutenonquery ()
Myconn. close ()
Dim myCommand As New SqlCommand ("select top 1 id from picture order by id desc", myConn)
MyCommand. Connection. Open ()
Dim myReader As SqlDataReader=mycommand.exe cutereader ()
If myReader. Read () then
Id = myReader ("id ")
End if
Myconn. close ()
"Save image
FileUp. PostedFile. SaveAs (Server. MapPath ("\ classpic \") & cstr (id) & ". jpg ")
"Generating thumbnails
Image = System. Drawing. Image. FromFile (Server. MapPath ("/classpic/" + cstr (id) + ". jpg "))
Width = image. Width
Height = image. height
If width> height then
Newwidth = 250
Newheight = image. height/image. Width * newwidth
Else
Newheight = 250
Newwidth = image. Width/image. height * newheight
End if
Response. write ("id =" + cstr (id) + "width =" + cstr (Width) + "; height =" + cstr (height) + "")
Response. write ("newwidth =" + cstr (newwidth) + "; newheight =" + cstr (newheight) + "<br> ")
ANewImage = image. GetThumbnailImage (newwidth, newheight, callb, new System. IntPtr ())
ANewImage. Save (Server. MapPath ("/smallpic/" + cstr (id) + ". jpg "))
Image. Dispose ()
Dim FileSplit () As String = Split (FileUp. PostedFile. FileName ,"\")
Dim FileName As String = FileSplit (FileSplit. Length-1)
Dim Exts () As String = Split (FileName ,".")
Dim Ext As String = LCase (Exts. Length-1 ))
FDisplay. text = "<A Target =" _ blank "HREF ="/classpic/"& cstr (id )&". jpg "&"> View uploaded files </A>"
FDisplay1.text = "<a href ="/picture/default. asp? Bj = "& cstr (request. cookies (" dgxyl "). values (" dgxylbj ") &" "> return </a>"
End Sub
</Script>
<SCRIPT language = JavaScript>
<! --
Var requestsubmitted = false;
Function guestbook_Validator (theForm)
{
// Check whether the request is submitted again
If (requestsubmitted = true ){
Alert ("You have submitted a message. Please wait for the server to respond! ");
Return (false );
}
Requestsubmitted = true;
Return (true );
}
// -->
</SCRIPT>
<Html>
<Body BgColor = White>
<H3 align = "center"> enter the following items correctly <Hr> </H3>
<Div id = "FileInfo" Visible = "False" runat = "server">
<Asp: Label id = "FSize" runat = "server"/> <br>
<Asp: Label id = "FName" runat = "server"/> <br>
<Asp: Label id = "FDisplay" runat = "server"/>
<Asp: Label id = "FDisplay1" runat = "server"/>
</Div>
<Form Enctype = "multipart/form-data" onsubmit = "return guestbook_Validator (this)" runat = "server">
Upload files
<Input Type = "File" id = "FileUp" runat = "server" size = "20"> <br>
Image attribute: <input type = "radio" value = "<% = request. cookies ("dgxyl "). values ("dgxylbj ") %> "name =" R1 "checked> This class <input type =" radio "value =" all "name =" R1 "> all schools (this class is only displayed in this class, the whole school is displayed in the whole school.) <P>
Image Description: <br>
<Asp: TextBox id = "TextBox1" runat = "server" Width = "233px" Height = "141px">
</Asp: TextBox>
<Asp: RequiredFieldValidator id = "RequiredFieldValidator1" runat = "server" ControlToValidate = "TextBox1" ErrorMessage = "Enter image description">
</Asp: RequiredFieldValidator> <br>
<Asp: button id = "Upload" OnClick = "UploadFile" Text = "Upload image" runat = "server"/>
</Form>
<Hr>
<P> Note: </p>
<Ol>
<Li> <B> It is strictly prohibited to upload photos that pollute the environment. Otherwise, the account will be deleted! </B> </li>
<Li> <font color = "#808000"> <B> Fill in the photo description in detail. If the picture description is not detailed, it will be regarded as a false picture and will be deleted! </B> </font> </li>
</Ol>
</Body>
</Html>

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.