asp.net upload pictures and generate thumbnails at the same time code _ practical Tips

Source: Internet
Author: User
Copy Code code 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"), 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 files selected";
}
Else
{
String path = Server.MapPath ("./uploads/" + uploadFile.PostedFile.FileName.Substring ( UploadFile.PostedFile.FileName.LastIndexOf (' \ \ ') + 1));
if (file.exists (path))
{
Lblerrinfo.text = "already has a file of the same name";
}
Else
{
UploadFile.PostedFile.SaveAs (path);
Getthumbnailimage (width, height, straddinfo, left, right);
}
}
}
</script>
<title> upload pictures and generate thumbnails </title>
<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 info: <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= "A sketch of the upload Build" onserverclick= "Button_Click" runat= "Server"/>
</p>
<p>
<asp:image id= "Imgpreview" runat= "Server" ></asp:Image>
</p>
<!--Insert content here-->
</form>
</body>

asp.net (VBScript) uploads pictures and generates thumbnails at the same time:
Copy Code code 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 = "Upload file size" + CSTR (fileup.postedfile.contentlength/1024) + "KB"
Fname.text = "uploaded file name:" +fileup.postedfile.filename+ "<br>" +fname.text
The Write 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.AppSettings ("Database"))
MyConn.Open ()
Sql= "INSERT into the picture (name, class, Introduction, Attributes) VALUES (" "&request.cookies (" Dgxyl "). VALUES (" Dgxylname ") &" "," & Request.Cookies ("Dgxyl"). VALUES ("DGXYLBJ") & "," "&trim (Request (" TextBox1 ")) &", "" &request ("R1") & "") "
Mycomm=new SqlCommand (Sql,myconn)
Mycomm.executenonquery ()
Myconn.close ()
Dim mycommand as New SqlCommand ("Select top 1 IDs from picture order by id Desc", myconn)
MyCommand.Connection.Open ()
Dim myreader as Sqldatareader=mycommand.executereader ()
If Myreader.read () then
Id=myreader ("id")
End If
Myconn.close ()
The Save picture
FileUp.PostedFile.SaveAs (Server.MapPath ("\classpic\") &cstr (ID) & ". jpg")
The build thumbnail
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 (exts.length-1))
Fdisplay.text = "<a target=" _blank "href="/classpic/"&cstr (ID) &". jpg "&" "> View upload file </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 to see if you are submitting a new
if (requestsubmitted==true) {
Alert ("You have submitted a message, please wait for the server to reply!") ");
return (false);
}
Requestsubmitted=true;
return (true);
}
-->
</SCRIPT>
<Html>
<body bgcolor=white>

<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" >
Uploading files
<input type= "File" id= "Fileup" runat= "Server" size= "><br>"
Picture properties: <input type= "Radio" value= <%=request.cookies ("Dgxyl"). VALUES ("DGXYLBJ")%> "name=" R1 "checked>" <input type= "Radio" value= "The whole School" name= "R1" > School (This class is only shown in this class, the whole school is shown in the whole school) <P>
Picture Description:<br>
<asp:textbox id= "TextBox1" runat= "Server" width= "233px" height= "141px" >
</asp:TextBox>
<asp:requiredfieldvalidator id= "RequiredFieldValidator1" runat= "Server" controltovalidate= "TextBox1" Errormessage= "Please enter picture description" >
</asp:RequiredFieldValidator><br>
<asp:button id= "Upload" onclick= "UploadFile" text= "Upload Picture" runat= "Server"/>
</form>
<Hr>
<p> Note:</p>
<ol>
<li><b> do not upload photos of contaminated environment, otherwise the account will be deleted! </b></li>
<li><font color= "#808000" ><b> please fill in the photo description, the picture description is not detailed will be considered as a random image, 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.