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 Photography 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 buy to change the opacity of the
// Watermark. this is done by applying a 5x5 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 UPER RIGHT
// Hand Corner of the photography. 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 debinament 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 Measurement
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 ();
}
-------------------------------------------------- ------------------
The image upload of FTB mainly uses the HTMLINPUTFILE control, corresponding to the properties of the HTMLInputFile class, which contains the SaveAs method to save the image. Of course, we don't want to read it again after the picture is saved. If you check the MSDN, you find that the postedfile property returns an instance of the HTTPPostedFile class, and httppostedfile has an InputStream object, and the file uploaded by the HTMLINPUTFILE control The upload file stream is obtained by the Stream object, and the final function is implemented as the parameters of Watermake.