asp.net中上传图片文件实现防伪图片水印并写入数据库

时间:2024-01-17 01:36:49 


// 涉及命名空间
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());
}
}
标签:上传图片,防伪,图片水印
0
投稿

猜你喜欢

  • 提升Go语言开发效率的小技巧实例(GO语言语法糖)汇总

    2023-07-08 10:01:26
  • keras中epoch,batch,loss,val_loss用法说明

    2021-08-11 10:56:30
  • 微信小程序获取当前位置的详细步骤

    2024-04-08 10:52:09
  • js实现微信聊天效果

    2024-04-16 09:14:33
  • Python写安全小工具之TCP全连接端口扫描器

    2023-12-30 13:34:52
  • javascript打印html内容功能的方法示例

    2024-04-25 13:13:23
  • Linux中Oracle数据库备份

    2024-01-18 16:03:57
  • MySQL详解如何优化查询条件

    2024-01-26 06:52:05
  • Python的数据类型与标识符和判断语句详解

    2021-04-11 13:18:15
  • javascript记住用户名和登录密码(两种方式)

    2024-05-02 16:16:44
  • MySQL和Oracle的元数据抽取实例分析

    2024-01-20 18:52:00
  • python opencv肤色检测的实现示例

    2023-06-13 20:31:58
  • python实现web方式logview的方法

    2023-12-23 17:07:54
  • Flask教程之重定向与错误处理实例分析

    2021-02-04 08:00:49
  • 详解在Azure上部署Asp.NET Core Web App

    2023-07-20 00:55:18
  • 使用python脚本自动创建pip.ini配置文件代码实例

    2022-03-03 15:05:58
  • Python 网络编程之UDP发送接收数据功能示例【基于socket套接字】

    2023-09-23 14:33:28
  • MySQL 查询速度慢的原因

    2024-01-25 01:10:27
  • 详解Python中的进程和线程

    2021-10-06 21:49:03
  • 教你快速掌握数据库查询优化的实用技巧

    2008-11-28 15:10:00
  • asp之家 网络编程 m.aspxhome.com