asp.Netで画像ファイルをアップロードして偽造防止画像の透かしを実現し、データベースに書き込む

2796 ワード

 
  
//
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());
}
}