graphics的drawimage方法,定义了多种原型,可以在制定位置绘制指定image对象。利用此方法可以在图片对象上再绘制一个水印图片。结合freetextbox方便的图片上传功能,可以实现一个适合图片新闻较多的新闻系统。以下watermark方法所带参数为文件流,原始图片名称,水印图片名称,图片保存路径等,对应注释理解代码应该没有多大问题。
—————————————————————–
public void watermark(stream inputstream, string filename, string
markname, string picpath)
{
string workingdirectory =
httpcontext.current.request.physicalapplicationpath + "\\" + picpath;
stream photostream = inputstream;
string watermarkname = markname;
string photofinalname = filename;
//create a image object containing the photograph to watermark
system.drawing.image imgphoto = system.drawing.image.fromstream(photostream);
int phwidth = imgphoto.width;
int phheight = imgphoto.height;
//create a image object containing the watermark
system.drawing.image imgwatermark = new bitmap(workingdirectory + "\\" + watermarkname);
int wmwidth = imgwatermark.width;
int wmheight = imgwatermark.height;
//create a bitmap
bitmap bmwatermark = new bitmap(photostream);
bmwatermark.setresolution(imgphoto.horizontalresolution, imgphoto.verticalresolution);
//load this bitmap into a new graphic object
graphics grwatermark = graphics.fromimage(bmwatermark);
imageattributes imageattributes = new imageattributes();
//this color manipulation is used to change the opacity of the
//watermark. this is done by applying a 5×5 matrix that contains the
//coordinates for the rgba space. by setting the 3rd row and 3rd column
//to 0.3f we achive a level of opacity
float[][] colormatrixelements = {
new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f},
new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}};
colormatrix wmcolormatrix = new colormatrix(colormatrixelements);
imageattributes.setcolormatrix(wmcolormatrix, colormatrixflag.default,
coloradjusttype.bitmap);
//for this example we will place the watermark in the upper right
//hand corner of the photograph. offset down 10 pixels and to the
//left 10 pixles
int xposofwm = ((phwidth – wmwidth)-10);
int yposofwm = 10;
grwatermark.drawimage(imgwatermark,
new rectangle(xposofwm,yposofwm,wmwidth,wmheight), //set the detination position
0, // x-coordinate of the portion of the source image to draw.
0, // y-coordinate of the portion of the source image to draw.
wmwidth, // watermark width
wmheight, // watermark height
graphicsunit.pixel, // unit of measurment
imageattributes); //imageattributes object
//replace the original photgraphs bitmap with the new bitmap
imgphoto = bmwatermark;
grwatermark.dispose();
//save new image to file system.
imgphoto.save(workingdirectory + "\\" + photofinalname, imageformat.jpeg);
imgphoto.dispose();
imgwatermark.dispose();
photostream.close();
}
——————————————————————–
ftb的图片上传主要利用htmlinputfile控件,对应htmlinputfile类的属性postedfile,它含有saveas方法可以来保存图片。当然我们不希望在图片保存完之后再专门读它建graphics对象来再次处理,因此查了msdn,发现postedfile属性返回的是httppostedfile 类的一个实例,而httppostedfile 有inputstream对象,通过htmlinputfile控件上传的文件可以通过该stream对象获得上传文件流,作为watermake的参数实现最终功能。
所以最后只要在ftb中把ftb.imagegallery.aspx文件第77行uploadfile.postedfile.saveas那句注释,并替换为对watermake方法的调用就行:watermark(uploadfile.postedfile.inputstream, uploadfilename, "watermark.bmp", "uploadpics");当然还要把watermake方法放到代码中。
