`

上传文件

阅读更多

using System;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web;
using System.Web.UI;

namespace My532ad.Func
{
/// <summary>
/// myUpimg 的摘要说明。
/// </summary>
public class myUpimg:Page
{
public string[] myupimgfile()
{

string[] myfilename = new string[3];
HttpFileCollection _files = HttpContext.Current.Request.Files;
for (int mynum = 0; mynum < _files.Count; mynum++)
{
string newfilename = "";
HttpPostedFile upimg = _files[mynum];

string imgPath, imgType;
string mPath;
imgPath = upimg.FileName;
imgType = imgPath.Substring(imgPath.LastIndexOf(".") + 1);
if (upimg.FileName != "")
{
imgPath = upimg.FileName;
imgType = imgPath.Substring(imgPath.LastIndexOf(".") + 1);
if (imgType == "jpg" || imgType == "jpeg")
{
string now = 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();
string newname = now + upimg.ContentLength.ToString() + "." + imgType;
newfilename = newname;
//建立虚拟路径
mPath = Server.MapPath("../infoimg");
//保存文件
upimg.SaveAs(mPath + "\\" + newname);
//原始图片名称
string originalFilename = mPath + "\\" + newname;
//生成的高质量图片名称
string strGoodFile = mPath + "\\" + newname;
//生成的低质量图片名称
//string strBadFile = mPath + "\\" + newname;
//缩小的倍数
int iScale = 1;
//从文件取得图片对象
Image image = Image.FromFile(originalFilename);
//取得图片大小
Size size = new Size(image.Width/iScale, image.Height/iScale);
//新建一个bmp图片
Image bitmap = new Bitmap(size.Width, size.Height);
//新建一个画板
Graphics g = Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.Blue);
//在指定位置画图
g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
//保存高清晰度的缩略图
//水印字 14:131,37; 10:90,34; 8:74,12
int fontsize = 10;
int mygx = 10;
int mygy = 20;
if (image.Width > 160)
{
fontsize = 14;
mygx = (image.Width - 160)/2;
}
if (image.Width > 121 && image.Width < 161)
{
fontsize = 10;
mygx = (image.Width - 121)/2;
}
if (image.Width < 122)
{
fontsize = 8;
if (image.Width > 74)
{
mygx = (image.Width - 74)/2;
}
else
{
mygx = 1;
}

}
if (image.Height > 37)
{
mygy = image.Height - 60;
}
else
{
mygy = 10;
}

Font f = new Font("楷体", fontsize);
Brush b = new SolidBrush(Color.FromArgb(150, 1, 1, 1));
Brush b1 = new SolidBrush(Color.FromArgb(150, 250, 250, 250));

g.DrawString("www.532ad.com", f, b, mygx, mygy + 20);
g.DrawString("www.532ad.com", f, b1, mygx, mygy + 21);


//Font f = new Font("宋体", fontsize);
//Brush b = new SolidBrush(Color.FromArgb( 150, 50, 50, 50 ));
//string addText ="青岛分类广告网 http://www.532ad.com";
//Font f1 = new Font("Impact", 14);
//Brush b1 = new SolidBrush(Color.FromArgb( 250, 50, 50, 50 ));
//string addText1 ="http://www.532ad.com";
image.Dispose();
g.Dispose();
//g.DrawString(addText, f, b, 10, 10);
if (imgType == "jpg" || imgType == "jpeg")
bitmap.Save(strGoodFile, ImageFormat.Jpeg);
if (imgType == "gif")
bitmap.Save(strGoodFile, ImageFormat.Gif);
//取得原图像的普通缩略图
//System.Drawing.Image img = image.GetThumbnailImage(image.Width/iScale, image.Height/iScale, null, IntPtr.Zero);
//保存普通缩略图
//if (imgType == "jpg" || imgType == "jpeg")
//img.Save(strBadFile, System.Drawing.Imaging.ImageFormat.Jpeg);
//if (imgType == "gif")
//img.Save(strBadFile, System.Drawing.Imaging.ImageFormat.Gif);
//img.Dispose();
}
if (imgType == "gif")
{
string now = 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();
string newname = now + upimg.ContentLength.ToString() + "." + imgType;
newfilename = newname;
//建立虚拟路径
mPath = Server.MapPath("../infoimg");
//保存文件
upimg.SaveAs(mPath + "\\" + newname);
}
}
if (mynum == 0)
{
myfilename[mynum] = newfilename;
}
else
{
myfilename[mynum] = newfilename;

}

}
return myfilename;

}


public string[] myupimgfile1()
{

string[] myfilename = new string[3];
HttpFileCollection _files = HttpContext.Current.Request.Files;
for (int mynum = 0; mynum < _files.Count; mynum++)
{
string newfilename = "";
HttpPostedFile upimg = _files[mynum];

string imgPath, imgType;
string mPath;
imgPath = upimg.FileName;
imgType = imgPath.Substring(imgPath.LastIndexOf(".") + 1);
if (upimg.FileName != "")
{
imgPath = upimg.FileName;
imgType = imgPath.Substring(imgPath.LastIndexOf(".") + 1);
if (imgType == "jpg" || imgType == "jpeg" || imgType == "gif")
{
string now = 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();
string newname = now + upimg.ContentLength.ToString() + "." + imgType;
newfilename = newname;
//建立虚拟路径
mPath = Server.MapPath("../infoimg");
//保存文件
upimg.SaveAs(mPath + "\\" + newname);
//原始图片名称
}
}
myfilename[mynum] = newfilename;

}
return myfilename;

}


public string[] myupimgfile2()
{

string[] myfilename = new string[3];
HttpFileCollection _files = HttpContext.Current.Request.Files;
for (int mynum = 0; mynum < _files.Count; mynum++)
{
string newfilename = "";
HttpPostedFile upimg = _files[mynum];

string imgPath, imgType;
string mPath;
if (upimg.FileName != "")
{
imgPath = upimg.FileName;
imgType = imgPath.Substring(imgPath.LastIndexOf(".") + 1);
if (imgType == "jpg" || imgType == "jpeg" || imgType == "gif")
{
string now = 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();
string newname = now + upimg.ContentLength.ToString() + "." + imgType;
newfilename = newname;
//建立虚拟路径
mPath = Server.MapPath("clubimg");
//保存文件
upimg.SaveAs(mPath + "\\" + newname);
//原始图片名称
#region 存图
string originalFilename = mPath + "\\" + newname;
//生成的高质量图片名称
string strGoodFile = mPath + "\\" + newname;
//生成的低质量图片名称
//string strBadFile = mPath + "\\" + newname;
//缩小的倍数
int iScale = 1;
//从文件取得图片对象
Image image = Image.FromFile(originalFilename);
//取得图片大小
//Size size = new Size(image.Width/iScale, image.Height/iScale);
int hi =0;
int wi =0;
if(image.Width>300)
{
wi=300;
}
else
{

}
if(image.Height>300)
{
hi=300;
}
else
{

}
Size size = new Size(wi,hi);
//新建一个bmp图片
Image bitmap = new Bitmap(size.Width, size.Height);
//新建一个画板
Graphics g = Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = SmoothingMode.HighQuality;
//清空一下画布
g.Clear(Color.Blue);
//在指定位置画图
g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
//保存高清晰度的缩略图
//水印字 14:131,37; 10:90,34; 8:74,12
int fontsize = 10;
int mygx = 10;
int mygy = 20;
if (image.Width > 160)
{
fontsize = 14;
mygx = (image.Width - 160)/2;
}
if (image.Width > 121 && image.Width < 161)
{
fontsize = 10;
mygx = (image.Width - 121)/2;
}
if (image.Width < 122)
{
fontsize = 8;
if (image.Width > 74)
{
mygx = (image.Width - 74)/2;
}
else
{
mygx = 1;
}

}
if (image.Height > 37)
{
mygy = image.Height - 60;
}
else
{
mygy = 10;
}

Font f = new Font("楷体", fontsize);
Brush b = new SolidBrush(Color.FromArgb(150, 1, 1, 1));
Brush b1 = new SolidBrush(Color.FromArgb(150, 250, 250, 250));

g.DrawString("www.532ad.com", f, b, mygx, mygy + 20);
g.DrawString("www.532ad.com", f, b1, mygx, mygy + 21);


//Font f = new Font("宋体", fontsize);
//Brush b = new SolidBrush(Color.FromArgb( 150, 50, 50, 50 ));
//string addText ="青岛分类广告网 http://www.532ad.com";
//Font f1 = new Font("Impact", 14);
//Brush b1 = new SolidBrush(Color.FromArgb( 250, 50, 50, 50 ));
//string addText1 ="http://www.532ad.com";
image.Dispose();
g.Dispose();
//g.DrawString(addText, f, b, 10, 10);
if (imgType == "jpg" || imgType == "jpeg")
bitmap.Save(strGoodFile, ImageFormat.Jpeg);
if (imgType == "gif")
bitmap.Save(strGoodFile, ImageFormat.Gif);


newfilename = newname;
//取得原图像的普通缩略图
//System.Drawing.Image img = image.GetThumbnailImage(image.Width/iScale, image.Height/iScale, null, IntPtr.Zero);
//保存普通缩略图
//if (imgType == "jpg" || imgType == "jpeg")
//img.Save(strBadFile, System.Drawing.Imaging.ImageFormat.Jpeg);
//if (imgType == "gif")
//img.Save(strBadFile, System.Drawing.Imaging.ImageFormat.Gif);
//img.Dispose();
//}
/*if (imgType == "gif")
{
/*string now = 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();
string newname = now + upimg.ContentLength.ToString() + "." + imgType;*/
/*newfilename = newname;
//建立虚拟路径
mPath = Server.MapPath("../infoimg");
//保存文件
upimg.SaveAs(mPath + "\\" + newname);
}*/
#endregion
}
}
myfilename[mynum] = newfilename;

}
return myfilename;

}


public string[] myupimgfile3()
{

string[] myfilename = new string[3];
HttpFileCollection _files = HttpContext.Current.Request.Files;
for (int mynum = 0; mynum < _files.Count; mynum++)
{
string newfilename = "";
HttpPostedFile upimg = _files[mynum];

string imgPath, imgType;
string mPath;
if (upimg.FileName != "")
{
imgPath = upimg.FileName;
imgType = imgPath.Substring(imgPath.LastIndexOf(".") + 1);
if (imgType == "jpg" || imgType == "jpeg" || imgType == "gif")
{
string now = 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();
string newname = now + upimg.ContentLength.ToString() + "." + imgType;
newfilename = newname;
//建立虚拟路径
mPath = Server.MapPath("../adimg");
//保存文件
upimg.SaveAs(mPath + "\\" + newname);
//原始图片名称
}
}
myfilename[mynum] = newfilename;

}
return myfilename;

}

public bool deleteimg(int empid, string senderid)
{
bool deletebool = true;
try
{
DataBase db = new DataBase();
string[] myimgname = new string[3];
db.Open();
db.comm = new SqlCommand("select id,senderid,imgname,imgname1,imgname2 from info where senderid = '" + senderid + "'and id = " + empid, db.conn);
db.dr = db.comm.ExecuteReader();
if (db.dr.Read())
{
myimgname[0] = db.dr["imgname"].ToString();
myimgname[1] = db.dr["imgname1"].ToString();
myimgname[2] = db.dr["imgname2"].ToString();
}
db.dr.Close();
db.comm.Dispose();
db.conn.Close();


for (int i = 0; i < 3; i++)
{
if (myimgname[i] != "" && File.Exists(Server.MapPath("../infoimg/" + myimgname[i])) == true)
{
File.Delete(Server.MapPath("../infoimg/" + myimgname[i]));
}
}
}
catch
{
deletebool = false;
}

return deletebool;
}


public bool deleteimg(int empid)
{
bool deletebool = true;
try
{
DataBase db = new DataBase();
string[] myimgname = new string[1];
db.Open();
db.comm = new SqlCommand("select * from movement where id = " + empid, db.conn);
db.dr = db.comm.ExecuteReader();
if (db.dr.Read())
{
myimgname[0] = db.dr["imgname"].ToString();
}
db.dr.Close();
db.comm.Dispose();
db.conn.Close();


for (int i = 0; i < 1; i++)
{
if (myimgname[i] != "" && File.Exists(Server.MapPath("../infoimg/" + myimgname[i])) == true)
{
File.Delete(Server.MapPath("../infoimg/" + myimgname[i]));
}
}
}
catch
{

deletebool = false;
}

return deletebool;
}

}

}

风格的

string extension = Path.GetExtension(UploadFile.PostedFile.FileName).ToUpper();
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
string path = Server.MapPath(".") + "/upfile/" + fileName + extension;
UploadFile.PostedFile.SaveAs(path);

// UploadFile.Dispose();

//加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
Graphics g = Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
Font f = new Font("Verdana", 32);
Brush b = new SolidBrush(Color.Red);
string addText = "ty158.com";

g.DrawString(addText, f, b, 10, 10);

image.Save("c://gugug.jpg");
image.Dispose();
g.Dispose();

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics