第一、图片上传,代码如下:
xxx.aspx
复制代码 代码如下:
<td class="style1">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="上传一般图片" onclick="Button1_Click" />
</td>
<td class="style3">
<asp:Image ID="Image1" runat="server" Height="200px" Width="200px" />
</td>
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("<script>alert('该文件不是图片格式!');</script>");
}
}
else
{
Response.Write("<script>alert('请选择要上传的图片');</script>");
}
}
}
第二、添加文字水印的图片上传,代码如下:
xxx.aspx
复制代码 代码如下:
<td class="style1">
<asp:FileUpload ID="FileUpload2" runat="server" />
<asp:Button ID="Button2" runat="server" Text="上传文字图片" onclick="Button2_Click" />
</td>
<td>
<asp:Image ID="Image2" runat="server" Height="200px" Width="200px" />
</td>
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("<script>alert('该文件不是图片格式!');</script>");
}
}
else
{
Response.Write("<script>alert('请选择要上传的图片');</script>");
}
}
}
第三、添加图片水印的图片上传,代码如下:
xxx.aspx
复制代码 代码如下:
<td class="style1">
<asp:FileUpload ID="FileUpload3" runat="server" />
<asp:Button ID="Button3" runat="server" Text="上传水印图片" onclick="Button3_Click" />
</td>
<td>
<asp:Image ID="Image3" runat="server" Height="200px" Width="200px" />
</td>
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("<script>alert('该文件不是图片格式!');</script>");
}
}
else
{
Response.Write("<script>alert('请选择要上传的图片');</script>");
}
}
}
第四、上传图片浓缩图,代码如下:
xxx.aspx
复制代码 代码如下:
<td class="style1">
<asp:FileUpload ID="FileUpload4" runat="server" />
<asp:Button ID="Button4" runat="server" Text="上传浓缩图片" onclick="Button4_Click" />
</td>
<td>
<asp:Image ID="Image4" runat="server" Height="200px" Width="200px" />
</td>
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("<script>alert('该文件不是图片格式!');</script>");
}
}
else
{
Response.Write("<script>alert('请选择要上传的图片');</script>");
}
}
}
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 炉石传说月初最强卡组有哪些 2024月初最强上分卡组推荐
- 狼人杀亮相原生鸿蒙之夜 假面科技强势登陆华为生态
- 12小时光线挑战!AI画质专家才是大平层首选
- 2024游戏IP报告:1~9月规模1960亿 68%用户愿为之付费
- 群星.2024-今夜一起为爱鼓掌电视剧原声带【相信音乐】【FLAC分轨】
- BIGFOUR.2013-大家利事【寰亚】【WAV+CUE】
- 李美凤.1992-情深透全情歌集【EMI百代】【WAV+CUE】
- 田震2024-《时光音乐会》[金峰][WAV+CUE]
- 群星《监听天碟3》[LECD]限量版[WAV+CUE]
- 心妤《声如夏花HQ》头版限量编号[WAV+CUE]
- 群星《摇滚五杰》[低速原抓WAV+CUE][1.1G]
- 群星 《2024好听新歌30》十倍音质 U盘音乐 [WAV+分轨]
- 群星《试音草原·女声篇》经典蒙古民歌[WAV+CUE][1G]
- 陈慧娴《永远是你的朋友》头版限量编号MQA-UHQCD2024[低速原抓WAV+CUE]
- 曼丽·女人三十《如果·爱》限量1:1母盘直刻[低速原抓WAV+CUE]