asp 上传图片加水印后添加到数据库
发布网友
发布时间:2022-05-19 16:58
我来回答
共5个回答
懂视网
时间:2022-05-19 21:20
代码如下:
// 涉及命名空间
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Web;
using System.Configuration;
// 方法
public void AddUser(string PersonName, string PersonEmail, string PersonSex, string PersonDOB, string PersonImage, string PersonImageType)
{
string strImageType = userImage.PostedFile.ContentType;
Stream ImageStream = userImage.PostedFile.InputStream;
// 加水印----------------->
string wImageFile = Server.MapPath("/bkwww/image/HomeSign.gif"); // 要加的水印图
Image sImage = Image.FromStream(ImageStream); // 从 Http 输入流创建 image
Image wImage = Image.FromFile(wImageFile);
// 绘图
Graphics g = Graphics.FromImage(sImage);
g.DrawImage(wImage, new Rectangle(0, 0, wImage.Width, wImage.Height), 0, 0, wImage.Width, wImage.Height, GraphicsUnit.Pixel);
// 保存,并将 image 转化为 byte[]
MemoryStream ms=new MemoryStream();
byte[] myImage=null;
sImage.Save(ms, ImageFormat.Gif);
myImage = ms.GetBuffer();
//------------------------>
// 写入数据库
string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection myConnection = new SqlConnection(strConn);
SqlCommand myCommand = new SqlCommand("sp_person_isp", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add("@PersonEmail", SqlDbType.VarChar, 255).Value = PersonEmail;
myCommand.Parameters.Add("@PersonName", SqlDbType.VarChar, 255).Value = PersonName;
myCommand.Parameters.Add("@PersonSex", SqlDbType.Char, 1);
if(sexMale.Checked)
myCommand.Parameters["@PersonSex"].Value = "M";
else
myCommand.Parameters["@PersonSex"].Value = "F";
myCommand.Parameters.Add("@PersonDOB", SqlDbType.DateTime).Value = PersonDOB;
myCommand.Parameters.Add("@PersonImage", SqlDbType.Image).Value = myImage;
myCommand.Parameters.Add("@PersonImageType", SqlDbType.VarChar, 255).Value = ImageType;
try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
Response.Write("添加成功!");
}
catch(System.Exception SqlEx)
{
Response.Write("添加失败!"+SqlEx.ToString());
}
}
热心网友
时间:2022-05-19 18:28
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.IO;
public partial class BookImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string id = Request.QueryString["id"];
string path = "images/" + id + ".jpg";
string bookPath = Request.MapPath(path);
System.Drawing.Image bookImg = null;
if (File.Exists(bookPath))
{
bookImg = System.Drawing.Image.FromFile(bookPath);
string waterPath = Request.MapPath("images/WaterMark.jpg");
System.Drawing.Image waterImg = System.Drawing.Image.FromFile(waterPath);
Graphics g = Graphics.FromImage(bookImg);
g.DrawImage(waterImg, bookImg.Width - waterImg.Width, bookImg.Height - waterImg.Height, waterImg.Width, waterImg.Height);
// g.DrawImage(waterImg,new Rectangle(bookImg.Width - waterImg.Width, bookImg.Height - waterImg.Height, waterImg.Width, waterImg.Height),0,0,waterImg.Width,waterImg.Height,GraphicsUnit.Pixel);
g.Dispose();
waterImg.Dispose();
}
else
{
bookPath = Request.MapPath("images/default.jpg");
bookImg = System.Drawing.Image.FromFile(bookPath);
}
Response.ContentType = "image/jpeg";
bookImg.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
bookImg.Dispose();
Response.End();
}
}
热心网友
时间:2022-05-19 19:46
啊 那样的话,数据库会变的非常巨大的。
一般来说,都是上传图片到指定路径,然后把图片路径存在数据库中。
热心网友
时间:2022-05-19 21:20
这个解决很繁琐,你最好不要这样弄,放数据库会使数据库变得很大,速度变慢的。
参考资料:http://www.tjanju.cn
热心网友
时间:2022-05-19 23:12
保存到数据库不太现实吧,就算是公司网站也不应该这么做,没有必要