第一、图片上传,代码如下:
xxx.aspx
复制代码 代码如下:
xxx.aspx.cs
复制代码 代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile file = Request.Files[i];
if (file.ContentLength > 0)
{
if (file.ContentType.Contains("image/"))
{
using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream))
{
string FileName = System.IO.Path.GetFileName(file.FileName);
string[] SplitFileName = FileName.Split('.');
string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss")+"." + SplitFileName[1];
img.Save(Server.MapPath("/upload/" + AtterFileName));
this.Image1.ImageUrl = "upload/" + AtterFileName;
}
}
else
{
Response.Write("");
}
}
else
{
Response.Write("");
}
}
}
第二、添加文字水印的图片上传,代码如下:
xxx.aspx
复制代码 代码如下:
xxx.aspx.cs
复制代码 代码如下:
protected void Button2_Click(object sender, EventArgs e)
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile file = Request.Files[i];
if (file.ContentLength > 0)
{
if (file.ContentType.Contains("image/"))
{
using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream))
{
using (Graphics g = Graphics.FromImage(img))
{
g.DrawString("我的图片", new Font("宋体", 14), Brushes.Red, 0, 0);
}
string FileName = System.IO.Path.GetFileName(file.FileName);
string[] SplitFileName = FileName.Split('.');
string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss") + "." + SplitFileName[1];
img.Save(Server.MapPath("/upload/" + AtterFileName));
this.Image2.ImageUrl = "upload/" + AtterFileName;
}
}
else
{
Response.Write("");
}
}
else
{
Response.Write("");
}
}
}
第三、添加图片水印的图片上传,代码如下:
xxx.aspx
复制代码 代码如下:
xxx.aspx.cs
复制代码 代码如下:
protected void Button3_Click(object sender, EventArgs e)
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile file = Request.Files[i];
if (file.ContentLength > 0)
{
if (file.ContentType.Contains("image/"))
{
string fileName = file.FileName;
using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream))
{
using (System.Drawing.Image imgWater = System.Drawing.Image.FromFile(Server.MapPath("/img/czlogo.jpg")))
{
using (Graphics g = Graphics.FromImage(img))
{
g.DrawImage(imgWater, 0, 0);
}
string[] SplitFileName = fileName.Split('.');
string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss") + "." + SplitFileName[1];
img.Save(Server.MapPath("/upload/" + AtterFileName));
this.Image3.ImageUrl = "upload/" + AtterFileName;
}
}
}
else
{
Response.Write("");
}
}
else
{
Response.Write("");
}
}
}
第四、上传图片浓缩图,代码如下:
xxx.aspx
复制代码 代码如下:
xxx.aspx.cs
复制代码 代码如下:
protected void Button4_Click(object sender, EventArgs e)
{
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile file = Request.Files[i];
if (file.ContentLength > 0)
{
if (file.ContentType.Contains("image/"))
{
using (System.Drawing.Image img = System.Drawing.Image.FromStream(file.InputStream))
{
using (System.Drawing.Image imgThumb = new Bitmap(200, 100))
{
using (Graphics g = Graphics.FromImage(imgThumb))
{
g.DrawImage(img, new Rectangle(0, 0, imgThumb.Width, imgThumb.Height), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
}
string fileName = file.FileName;
string[] SplitFileName = fileName.Split('.');
string AtterFileName = DateTime.Now.ToString("yyyMMddHHmmss") + "." + SplitFileName[1];
img.Save(Server.MapPath("/upload/" + AtterFileName));
this.Image4.ImageUrl = "upload/" + AtterFileName;
}
}
}
else
{
Response.Write("");
}
}
else
{
Response.Write("");
}
}
}