📄 addnew.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class addnew : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = db.createconn();
conn.Open();
string nTitle = txttitle.Text;
string nAuthor = txtauthor.Text;
string nContent = txtcontent.Text;
if (nTitle == "")
{
Response.Write("<script>alert('新闻标题不能为空!');history.go(-1);</script>");
}
else
{
string extension = Path.GetExtension(File1.PostedFile.FileName).ToLower();
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
string path = Server.MapPath(".") + "\\up\\" + fileName + extension;
string fullFileName = this.File1.PostedFile.FileName;
//string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);
string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1);
if (fileName == "")
{
SqlCommand cmd = new SqlCommand("insert into news(newstitle,newsauthor,newscontent)" + "values('" + nTitle + "','" + nAuthor + "','" + nContent + "')", conn);
cmd.ExecuteNonQuery();
Response.Redirect("default.aspx");
}
else
{
if (type == "jpg" || type == "bmp" || type == "gif")
{
File1.PostedFile.SaveAs(path);
//加文字水印
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("Arial Black", 26);
Brush b = new SolidBrush(Color.Brown);
string addText = "www.nt-nt.cn";
g.DrawString(addText, f, b, 10, 10);
g.Dispose();
//保存加水印过后的图片,删除原始图片
string newname = fileName + "_new" + extension;
string newPath = Server.MapPath(".") + "\\up\\" + fileName + "_new" + extension;
//string newPath = Server.MapPath(".") + "/upload/" + fileName + "_new" + extension;
image.Save(newPath);
image.Dispose();
if (File.Exists(path))
{
File.Delete(path);
}
SqlCommand cmd = new SqlCommand("insert into news(newstitle,newspic,newsauthor,newscontent)" + "values('" + nTitle + "','" + newname + "','" + nAuthor + "','" + nContent + "')", conn);
cmd.ExecuteNonQuery();
Response.Redirect("default.aspx");
}
else
{
Response.Write("<script language='javascript'>alert('图片格式不对');</script>");
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -