The following. NET Operation class is a picture operation class, his principle is to upload the picture to the remote server, and then according to user settings to generate small map or other operations, the need for friends can refer to.
public class Imghelp
{
public static string gif = "Image/gif";
public static string jpg = "image/pjpeg";
public static string png = "Image/x-png";
Page pag = new page ();
String[] Imgtype = {"Image/gif", "Image/pjpeg", "Image/x-png"};
public static string thisyear = Datetime.now.year.tostring ();
public static string thismonth = Datetime.now.month.tostring ();
public static string thisday = Datetime.now.day.tostring ();
String strdate = thisyear + thismonth + thisday + datetime.now.hour.tostring () + datetime.now.second.tostring () + datetime . now.minute.tostring () + datetime.now.millisecond.tostring ();
<summary>
Save uploaded file return True to save success massage to file path, return false to save failure massage error message
</summary>
<param name= "Producfile" ></param>
<param name= "size" > Maximum allowed upload value (in bytes) </param>
<param name= "Addurl" > Storage path </param>
<param name= "Massage" > Storage information </param>
<returns></returns>
public bool Upimg (fileupload producfile, int size, string addurl, out string massage)
{
String mag;
bool Su = FALSE;
int i = throwimg (producfile, size, out mag);
if (i = = 1)
{
New file name
String newfilename = Strdate + producfile.filename.replace ("~", ""). Replace ("|", ""). Replace ("&", "");
string url = addurl;
String pddurl = Pag.server.mappath (URL);
Try
{
if (!directory.exists (Pddurl))
{
Directory.CreateDirectory (Pddurl);
}
if (file.exists (pag.server.mappath (url + newfilename)) = = False)
{
Producfile.saveas (Pag.server.mappath (url + newfilename));
Massage = URL + newfilename;
Su = true;
}
Else
{
massage = "Upload filename already exists, please upload it again!";
}
}
Catch
{
massage = "File upload when the program appears abnormal, please upload!";
}
}
Else
{
massage = mag;
}
return su;
}
<summary>
Upload picture off to generate thumbnails
</summary>
<param name= "Producfile" > Controls </param>
<param name= "size" > Maximum allowed upload value (in bytes) </param>
<param name= "Addurl" > Storage path </param>
<param name= "Frontname" > Save file Prefix name collection </param>
<param name= "Imgwid", Width set </param>
<param name= "Imghid" > Height set </param>
<param name= "Massage" > Return upload information </param>
<returns></returns>
public bool Upimgandbreviary (fileupload producfile, int size, string addurl,string[] frontname,int[] imgwid,int[] Imghid , out string massage)
{
String mag;
BOOL key = FALSE;
int i = throwimg (producfile, size, out mag);
if (I!= 1)
{
massage = mag;
Key = false;
}
else if (frontname.length <= 0 | | frontname.length!= imgwid.length | | frontname.length!= imghid.length)
{
Massage = "Thumbnail information does not match";
Key = false;
}
Else
{
New file name
String newfilename = "Primary_" + strdate + producfile.filename.replace ("Primary_", "");
Storage path
String url = addurl + thisyear + "/" + Thismonth + "/" + ThisDay;
Absolute path to Server
String pddurl = Pag.server.mappath (URL);
Try
{
To determine if a path directory exists
if (!directory.exists (Pddurl))
{
Create a table of contents
Directory.CreateDirectory (Pddurl);
if (file.exists (URL + pag.server.mappath/+ newfilename)) = = False)
{
Save File
Producfile.saveas (Pag.server.mappath (url + "/" + NewFileName));
Return file Address
Massage = URL + "/" + NewFileName;
Generate thumbnails
for (int j = 0; J < Frontname.length; J + +)
{
Breviaryimg (Massage, Massage.replace ("Primary_", Frontname[j)), Imgwid[j], imghid[j]);
}
Key = true;
Generate two thumbnails, save name is in front of the original name added a Middle_,small_.
Newwidth1 = 200;
NEWHEIGHT1 = 200;
Newwidth2 = 60;
Newheight2 = 60;
Breviaryimg (Massage, Massage.replace ("Primary_", "Middle_"), Newwidth1, newheight1);
Breviaryimg (Massage, Massage.replace ("Primary_", "Small_"), Newwidth2, newheight2);
}
Else
{
massage = "Upload file duplicate name, please upload again!";
Key = false;
}
}
Else
{
if (file.exists (URL + pag.server.mappath/+ newfilename)) = = False)
{
Save File
Producfile.saveas (Pag.server.mappath (url + "/" + NewFileName));
Return to save Address
Massage = URL + "/" + NewFileName;
Generate thumbnails.
for (int j = 0; J < Frontname.length; J + +)
{
Breviaryimg (Massage, Massage.replace ("Primary_", Frontname[j)), Imgwid[j], imghid[j]);
}
Key = true;
}
Else
{
massage = "Upload filename already exists, please upload it again!";
Key = false;
}
}
}
Catch
{
massage = "File upload when there is a program error, please upload!";
Key = false;
}
}
Return key;
}
<summary>
detecting uploaded files
</summary>
<param name= "Producfile" ></param>
<param name= "Size" > Allow maximum upload </param>
<param name= "Massage" > Detection information </param>
<returns></returns>
private int throwimg (fileupload producfile, int size, out string massage)
{
int i = 0;
Boolean Imgok = false;
if (producfile.hasfile)
{
Picture Type set
String[] imgextension = {". gif", ". jpg", ". jpeg", ". png"};
File type
string filetype = Producfile.postedfile.contenttype;
File name extension
String fileextype = Path.getextension (producfile.filename). ToLower ();
Match type
for (int k = 0; k < imgextension.length; k++)
{
if (Fileextype = = Imgextension[k])
{
foreach (String type in Imgtype)
{
if (filetype = = type)
{
Imgok = true;
Break
}
}
}
}
if (Imgok = = True)
{
Get File size
int filesize = Producfile.postedfile.contentlength;
if (FileSize < size)
{
i = 1;
massage = "";
}
Else
{
massage = "Upload file too large!";
i = 0;
}
}
Else
{
massage = "The format of the uploaded file is not valid!";
i = 0;
}
}
Else
{
Massage = "Please choose the correct file path!";
i = 0;
}
return i;
}
<summary>
To generate thumbnail thumbnails
</summary>
<param name= "ImagePath" > Original path </param>
<param name= "Savepath" > Thumbnail storage path </param>
<param name= "wid" > Thumbnail width </param>
<param name= "hid" > Thumbnail height </param>
private void Breviaryimg (String ImagePath, string savepath, int wid, int hid)
{
String originalimagepath = Pag.server.mappath (ImagePath);
String thumbnailpath = Pag.server.mappath (Savepath);
System.Drawing.Image originalimage = System.drawing.image.fromfile (Originalimagepath);
int towidth = WID;
int toheight = HID;
int x = 0;
int y = 0;
int ow = Originalimage.width;
int oh = originalimage.height;
Create a new BMP picture
System.Drawing.Image bitmap = new System.Drawing.Bitmap (WID, HID);
Create a new artboard
System.Drawing.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 (system.drawing.color.transparent);
Draws the specified portion of the original picture at the specified location and at the specified size
G.drawimage (Originalimage, New System.Drawing.Rectangle (0, 0, wid, HID), new System.Drawing.Rectangle (x, Y, Ow, OH), Syst Em.drawing.graphicsunit.pixel);
Try
{
Save thumbnails in original format
Bitmap.save (Thumbnailpath, Originalimage.rawformat);
}
catch (System.Exception e)
{
Throw e;
}
Finally
{
Originalimage.dispose ();
Bitmap.dispose ();
G.dispose ();
}
}
}