C # Picture Processing

Source: Internet
Author: User
Tags define count current time datetime file size tostring
<%@ Page Debug = "true" language= "C #"%>
<%@ Import namespace= "System.IO"%>
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "system.web"%>
<%@ Import namespace= "System.Data.SqlClient"%>
<!--This procedure is developed by Peng Yong of Changsha Railway Public Security Department, all rights reserved, and shall not be reproduced-->
<script language= "C #" runat= "Server" >

Page load Events
public void Page_Load (Object Sender,system.eventargs e)
{

Define picture Flags
String Pys_pic;
Pys_pic= "";

Define folder names
String Pys_dir;
Pys_dir=datetime.now.year.tostring ();

Define the previous part of the file name to prevent the upload file from being overwritten by file name duplication
String Pys_filename;

If the client has uploaded the file, save the
if (! IsPostBack)
{
liuxi.text= "Waiting to upload";
}
Else
{
Check for article IDs, if not, process
if (request["Pysid"]==null | request["Pysid"]== "")
{
strstatus.text= "<font color=red> error occurred, you did not enter the page from the proper way, please close this page!" </font> ";
}
Else
{
Traversing file Form elements
System.Web.HttpFileCollection Files=system.web.httpcontext.current.request.files;
Status information
System.Text.StringBuilder strmsg=new System.Text.StringBuilder ("<font color=red> uploaded file information for:</font> &LT;HR size=1 color=blue> ");
int filecount;
int Filecount=files. Count;
Store the client address of the uploaded image to detect the size of the uploaded image
String Imgpath;
Check whether the upload file is a picture
String Pys_type;
Save picture to generate thumbnail width
int pys_width;
Try
{
Database connection string
String connectionstring= "server= (local);d atabase=csc;uid=forcool;pwd=";

String Sql= "select * from Upfile";


Creating SqlConnection objects
SqlConnection thisconnection=new SqlConnection (ConnectionString);
Creates a Dataadaper object and initializes it.
SqlDataAdapter Adapter=new SqlDataAdapter (Sql,thisconnection);




Creating a DataSet Object
DataSet data=new DataSet ();


Create a SqlCommandBuilder object and associate it with SqlDataAdapter
SqlCommandBuilder builder=new SqlCommandBuilder (adapter);


Adapter. Fill (data, "upfile");


For (Filecount=0;filecount<files. count;filecount++)
{
Define the number of access client uploaded files
System.Web.HttpPostedFile Postedfile=files[filecount];
String filename,fileextension;
Get the file name of the upload
Filename=system.io.path.getfilename (Postedfile.filename);
if (filename!=string.empty)
{
Get file name extension
Fileextension=system.io.path.getextension (FileName);
Uploaded file information
Strmsg.append ("<font color=red><b>" + (filecount+1) + ".</b></font>");
Strmsg.append ("uploaded file type:" +postedfile.contenttype.tostring () + "<br>");
Strmsg.append ("Client file Address:" +postedfile.filename+ "<br>");
Strmsg.append ("The filename of the uploaded file:" +filename+ "<br>");
Strmsg.append ("Upload file extension:" +fileextension+ "<br>");
Strmsg.append ("Upload file Size:" +postedfile.contentlength+ "<br>");


Name the file at the current time (accurate to milliseconds) without the Chinese name and avoid coding errors that cannot be downloaded or displayed
Pys_filename=datetime.now.year.tostring () +datetime.now.month.tostring () +datetime.now.day.tostring () + DateTime.Now.Hour.ToString () +datetime.now.minute.tostring () +datetime.now.second.tostring () + DateTime.Now.Millisecond.ToString ();

Create a random Number object
Random rnd=new Random ();
Call next method to generate random number
Pys_filename+=rnd. Next (1,100000). ToString ();

pys_filename+= "-";
Pys_filename+=filecount;
pys_filename=request.querystring["Pysid"]+ "-" +pys_filename;

Pys_type=fileextension;
Pys_type=pys_type. ToLower ();

Assign an initial value to the thumbnail width
pys_width=0;
If it's a picture, check the size, and if the picture is too large, create thumbnails.
if (pys_type== ". jpg" |   pys_type== ". gif" |   pys_type== ". bmp" | pys_type== ". png")
{
Pys_pic= "yes";
Get the path to the uploaded picture
Imgpath=postedfile.filename;
Create an Image object for a selected picture
System.Drawing.Image Image=system.drawing.image.fromfile (Imgpath);
Get the height and width of a picture using the height and width properties of the Image object
Strmsg.append ("Upload picture height:" +image.) Height.tostring () + "<br>");
Strmsg.append ("Upload picture width:" +image.) Width.tostring () + "<br>");
If the uploaded image is over 100KB,

if (postedfile.contentlength>100000 | | Image. width>400)
{
Generate thumbnails
System.Drawing.Image.GetThumbnailImageAbort Callb=null;
Pys_width= (image. Height) *200/(image. Width);
System.Drawing.Image Newimage=image. Getthumbnailimage (200,pys_width,callb,new system.intptr ());
Save thumbnails
NewImage. Save (Server.MapPath ("" +pys_dir+ "\\small-") +pys_filename+fileextension);
Freeing resources occupied by NewImage objects

Compression Processing of graphics
Pys_width= (image. Height) *400/(image. Width);
Newimage=image. Getthumbnailimage (400,pys_width,callb,new system.intptr ());
Save a compressed diagram
NewImage. Save (Server.MapPath ("" +pys_dir+ "\") +pys_filename+fileextension);
Freeing resources occupied by NewImage objects
NewImage. Dispose ();
Strmsg.append ("The uploaded picture is larger than 100KB, and a thumbnail has been generated for this figure in order not to affect the browsing speed of the dial-up Internet unit." <br> ");
}
Release the resources occupied by image
Image. Dispose ();
}


Strmsg.append ("

Add uploaded file information to the dataset
DataRow Newrow=data. tables["Upfile"]. NewRow ();
newrow["Pysid"]=request["Pysid"];
newrow["file name"]=filename;
newrow["file type"]=fileextension;
newrow["File Size"]=postedfile.contentlength;
newrow["File location"]=pys_dir+ "/" +pys_filename+fileextension;
if (pys_width>0)
{
newrow["thumbnail position"]=pys_dir+ "/small-" +pys_filename+fileextension;
}
Data. tables["Upfile"]. Rows.Add (NewRow);


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.