Add watermark to freetextbox1.6.5 upload image

Source: Internet
Author: User
Upload | Upload image graphics DrawImage method, define a variety of prototypes, you can draw the specified image object in place. Using this method, you can draw a watermark picture on a picture object. Combined with freetextbox convenient picture upload function, can realize a picture news system suitable for more pictures. The following watermark method takes the parameter as the file stream, the original picture name, the watermark picture name, the picture saves the path and so on, corresponding annotation understanding code should not have the big problem.
-----------------------------------------------------------------

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 was used to change the opacity of the

Watermark. This is doing by applying a 5x5 matrix that contains the

Coordinates for the RGBA. 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'll place the watermark on the upper right

Hand corner of the photograph. Offset down pixels and to the

Left Ten 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 ();





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.