asp.net upload picture (good)

Source: Internet
Author: User
Tags datetime tostring
asp.net| Upload | upload pictures

///
By the time the pictures were uploaded
///
///
///
private void Button1_Click (object sender, System.EventArgs e)
{
int towidth = 120;
int toheight = 90;
int t=0;
Page.validate ();
if (! Page.IsValid)
{
Return
}

Istop= ""; S_filename= "";
foreach (ListItem each in stristop. Items) {if (each. Selected) {Istop+=each. Value;}}

Loop to get uploaded files
System.Web.HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;
Try
{

for (System.Int32 _ifile = 0; _ifile < _files. Count; _ifile + +)
{

Check if the upload file is gif or JPG
System.Web.HttpPostedFile _postedfile = _files[_ifile];
System.String _filename, _fileextension;

_filename = System.IO.Path.GetFileName (_postedfile.filename);

_fileextension = System.IO.Path.GetExtension (_filename);

If ((_fileextension== ". gif") | | (_fileextension== ". jpg"))
{
if (_postedfile.contentlength<=256000)//If the size of the picture is within the specified range
{
if (t>0) {istop= "";}
Classnews p=new classnews ();
Filename1=p.maketid ("") + "-" + _ifile.tostring () +_fileextension;
S_filename = "S_" + filename1;
_postedfile.saveas (Server.MapPath (@). /.. /upload/03 ") +" \ "+filename1);
String Newfilename=p.drawword ("Ez2cn.com", Server.MapPath (@). /.. /upload/03 "), filename1,0,0,0,0);p =null;

String File1=server.mappath (@). /.. /upload/03 ") +" \ "+newfilename;
String File2=server.mappath (@). /.. /upload/03 ") +" \ "+s_filename;

Try
{
P.makethumbnail (file1,file2,200,200, "W");
System.Drawing.Image originalimage = System.Drawing.Image.FromFile (file1);
int x = 0;
int y = 0;
int ow = Originalimage.width;
int oh = originalimage.height;

Below gets the width and height of the thumbnail image
Towidth = 120;
Toheight = 90;

if (OW>=OH)//If the picture is horizontally greater than the vertical
{
towidth=120;
Toheight=120*oh/ow;
if (toheight>90)
{
Toheight = 90;
Towidth = 90*towidth/toheight;
}
}
else if (ow {
toheight=90;
Towidth = 90*ow/oh;
if (towidth>120)
{
Towidth = 120;
Toheight=120*toheight/towidth;
}
}

End Get width high

Create a new BMP picture
System.Drawing.Image bitmap = new System.Drawing.Bitmap (towidth,toheight);

Create a new artboard
Graphics g = System.Drawing.Graphics.FromImage (bitmap);

Set high quality interpolation method
G.interpolationmode = System.Drawing.Drawing2D.InterpolationMode.High;

Set high quality, low speed rendering smooth degree
G.smoothingmode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

Empty the canvas and fill with a transparent background color
G.clear (color.transparent);

Draws the specified portion of the original picture at the specified location and at the specified size
G.drawimage (Originalimage, New Rectangle (0, 0, Towidth, toheight), New Rectangle (x, Y, Ow,oh), GraphicsUnit.Pixel);

Try
{
Save thumbnails in JPG format
Bitmap. Save (File2, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception ex)
{
Throw ex;
}
Finally
{
Originalimage.dispose ();
Bitmap. Dispose ();
G.dispose ();
}

}
catch (System.Exception ex)
{
Response.Write (ex. ToString ());
}

filename1= "03/" +newfilename;
S_filename= "03/" +s_filename;

Add files to the database//

Add_photo ();
t=t+1;
}
Else
{
}
}
else//If the picture is not in the correct format
{

}
}

}
Catch
{

}

}


Using System;
Using System.Drawing;
Using System.Drawing.Imaging;
Using System.IO;
Using System.Text;
Namespace System.inc.cs
{
///


///
public class Classnews
{int Xp;
int Yp;
int wsize;

public string Maketid (string stri)
{string stryear=system.datetime.now.year.tostring ();
String strmonth=system.datetime.now.month.tostring ();
String strday=system.datetime.now.day.tostring ();
String strhour=system.datetime.now.hour.tostring ();
String strminute=system.datetime.now.minute.tostring ();
String strsecond=system.datetime.now.second.tostring ();
return (Stri+stryear+strmonth+strday+strhour+strminute+strsecond);
}

Text watermark
private bool Drawposition (int imgwidth,int imgheight,int typecount)//imgwidth picture wide imgheight picture high font size
{
Wsize=convert.toint16 (IMGWIDTH/40);
if (wsize+6<11) {return (false);}
Yp=imgheight-25-wsize;
Xp=imgwidth-convert.toint16 (WSIZE*TYPECOUNT+20);
return (true);

}

///


Mark the watermark.
///
/// Watermark Content
///
///
///
///
///
///
///
public string Drawword (string word,string filedir,string filename,int width,int height,int x,int Y)
{
if ((word==null) | | (Filename==null)) {return (filename);}

if (x!=0) {xp=x;} if (y!=0) {yp=y;}
//
String Path=filedir + "\" +filename;
//
Image image=system.drawing.image.fromfile (path);

if (width==0) {width=image. Width;}

if (height==0) {height=image. Height;}

if (!) ( Drawposition (width,height,13))) {image. Dispose (); return (filename);};
//
Bitmap bitmap=new Bitmap (image);
Graphics graphic=system.drawing.graphics.fromimage (bitmap);
Font f=new font ("Comic Sans Ms", wsize);
Brush b=new SolidBrush (Color.FromArgb (244,255,232));
Graphic. DrawString (WORD,F,B,XP,YP);
Graphic. Dispose ();
//
if (filename. ToLower (). IndexOf ("gif") >-1)
{
Bitmap. Save (filedir+ "\\windowscar.com_" +filename,system.drawing.imaging.imageformat.gif);
}
Else
{bitmap. Save (filedir+ "\\windowscar.com_" +filename,system.drawing.imaging.imageformat.jpeg);}

F.dispose ();
B.dispose ();
Image. Dispose ();
System.IO.File.Delete (path);
Return ("Windowscar.com_" +filename);
}
//Picture watermark
public string drawpicture (string pathsource,string path)
{
if (pathsource==null) | | (Path==null)) {return (null);}

Image image=system.drawing.image.fromfile (path);
Image Copyimage=system.drawing.image.fromfile (pathsource);

Graphics graphic=graphics.fromimage (image);
Graphic. DrawImage (copyimage,new Rectangle) (image. Width-copyimage.width,image. Height-copyimage.height,copyimage. Width,copyimage. Height), 0,0,copyimage. Width,copyimage. Height,graphicsunit.pixel);
Graphic. Dispose ();
Image. Save (path);
Image. Dispose ();

return (path);
}

 

Public Classnews ()
{xp=0;
Yp=0;


//
TODO: Add constructor logic here
//
}
}
}



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.