asp.net中上傳圖片檔案執行個體(給上傳圖片加浮水印)

來源:互聯網
上載者:User

本款asp教程.net教程是一款asp.net教程中上傳圖片檔案執行個體(給上傳圖片加浮水印)哦,他先是把圖片上傳伺服器,然後增加圖片浮水印,再把圖片儲存到資料庫教程。

// 涉及命名空間
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.apps教程ettings["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());
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.