你試過用.net來進行圖像處理嗎?公司最近進行的一個項目,數據庫中存入了很多的產品圖片,可是都沒有版權信息,當時客戶要求將所有的圖片打上公司名稱?這時候,你該怎么辦?
雖說我還是江湖嫩手,可是對這一問題的解決方案還是很多,歸結如下:
1.使用圖形處理軟件,如photoshop等,利用其批處理功能,可以實現這個功能,可是每次數據錄入員加進圖片的話,還得加工一下圖片,很麻煩的,看錄入員大姐平常對我很不錯,每日笑臉相迎,能忍心折磨她么?這個方案否決了。
2.使用.net行云流水般的圖像處理,當錄入員大姐上傳圖片的時候,就自動加上公司標記,這樣豈不更好,嗯,這想法不錯,可以進入2005最佳解決方案第萬強了,好說干就干。
using system.drawing;
using system.io;
using system.drawing.imaging;
private void addtexttoimg(string filename,string text)
{
if(!file.exists(mappath(filename)))
{
throw new filenotfoundexception("the file don't exist!");
}
if( text == string.empty )
{
return;
}
//還需要判斷文件類型是否為圖像類型,這里就不贅述了
system.drawing.image image = system.drawing.image.fromfile(mappath(filename));
bitmap bitmap = new bitmap(image,image.width,image.height);
graphics g = graphics.fromimage(bitmap);
float fontsize = 12.0f; //字體大小
float textwidth = text.length*fontsize; //文本的長度
//下面定義一個矩形區域,以后在這個矩形里畫上白底黑字
float rectx = 0;
float recty = 0;
float rectwidth = text.length*(fontsize+8);
float rectheight = fontsize+8;
//聲明矩形域
rectanglef textarea = new rectanglef(rectx,recty,rectwidth,rectheight);
font font = new font("宋體",fontsize); //定義字體
brush whitebrush = new solidbrush(color.white); //白筆刷,畫文字用
brush blackbrush = new solidbrush(color.black); //黑筆刷,畫背景用
g.fillrectangle(blackbrush,rectx,recty,rectwidth,rectheight);
g.drawstring(text,font,whitebrush,textarea);
memorystream ms = new memorystream( );
//保存為jpg類型
bitmap.save(ms,imageformat.jpeg);
//輸出處理后的圖像,這里為了演示方便,我將圖片顯示在頁面中了
response.clear();
response.contenttype = "image/jpeg";
response.binarywrite( ms.toarray() );
g.dispose();
bitmap.dispose();
image.dispose();
}
調用時很簡單,
addtexttoimg("me.jpg","小智");
一切ok了,感覺.net確實好強大,這些功能在asp中可是奢侈品了,而在.net環境中卻能輕而易舉的完成!
新聞熱點
疑難解答
圖片精選