Add a function of the watermark renderings to the picture (you can add your own copyright and the watermark of the logo picture

xiaoxiao2021-04-04  275

The front of the front, found an article adding a watermark, saved, did not look at it, now it is idle, sorted into a function logo picture: original picture: renderings:

/ ** /

/ * Function to set a picture watermark renderings * You can add your own LOGO picture and copyright watermark on the image * * author: Joel Neubeck * * I conducted on the basis of the original author of a partially modified and changed to a single Function * You can modify this code as needed, but please retain the original author information. * * Stading .NET (ZhuHee) * Modified to 2006-6-14 * * /

Using

System;

Using

System.drawing;

Using

System.drawing.Image;

Using

System.drawing.drawing2d;

Namespace

Webwatermark.components

{/ ** ////

/// Watermark's summary description.

/// public class watermark {public water, {// // // // // //} / ** ////

/// for processing pictures / // Add watermark to the picture /// /// Copyright information you need to write /// If you want to leave logo pictures /// Pictures of images that need to be processed /// Saved Image Path Public Void Makewatermark (String Copyright , string MarkBmpPath, string photoPath, string savePhotoPath) {// create an image object that is to be processed images Image imgPhoto = Image.FromFile (photoPath); int phWidth = imgPhoto.Width; int phHeight = imgPhoto.Height; // create the original image size bitmap bitmap bmPhoto = new bitmap (phWidth, phHeight, PixelFormat.Format24bppRgb); bmPhoto.SetResolution (imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); // bitmap bmPhoto loaded into the Graphics object Graphics grPhoto = Graphics.FromImage (bmPhoto); // create a need for mercury filling Image object Image imgWatermark = new Bitmap (MarkBmpPath); int wmWidth = imgWatermark.Width; int wmHeight = imgWatermark.Height; // ----- -------------------------------------------------- ----- // First step, insert copyright information / / ------------------------------------------------------------------------------------------------------------------ -------------------------- // Set the image quality Grphoto.smoothingmode = smoothingmode.antialias;

/ / Draw the original image to Grphoto.drawimage (imagphoto, // to draw the image object new Rectangle (0, 0, phwidth, phheight), // Draw the position of the image and size 0, // To draw The original image part of the X coordinate 0, // to draw the original image of the original image of the left upper left corner pHWIDTH, // The original image of the original image to be drawn Phheight, // To draw the original image Width graphicsunit.pixel); // Source rectangular metrics / / ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------- // The font size is placed in an array, the maximum font is 32. // The feeling is not large, you can modify itself here ///// -------------------------------------------------- ----- int [] Sizes = new int}; font crfont = null; sizef crfont = null; sizef crfont = new sizef (); // The font size defined in the loop test array Whether it is suitable for copyright information, if appropriate, use this font size for (int i = 0; i <5; i ) {// set font type, can be separately proposed, as parameters CrFont = New Font ("Song Body", Sizes [I], FontStyle.Bold); // Measure this font size Crsize = Grphoto.measureString (Copyright, crFont); if ((USHORT) CRSIZE.WIDTH <(Ushort) phwidth ) Break;} // Give the bottom of the% 3 space int ypixlesfrombottom = (int) (phheight * .03); // Set the position of the font in the picture float yposfrombottom = ((Phheight - YpixlesFromBottom) - (Crsize.Height / 2)); // float xcenterofimg = (phwidth / 2); float xcenterofimg = (phwidth- (crsize.width) / 2); // Setting the font hin Strformation = new stringFormat (); strformat.alignment =

StringAlignment.center; // Sets the color and texture of drawing text (alpha = 153) Solidbrush Semitransbrush2 = New Solidbrush (Color.Fromargb (153, 0)); // Draw copyright information to the image Grphoto. DrawString (Copyright, // copyright information crFont, // font semitransbrush2, // Draw text color and texture new pointf (xcenterofimg 1, yposfrombottom 1), // Draw a location Strformat of text); // Format // Setting Drawing the color and texture of the text (alpha = 153) Solidbrush Semitransbrush = New Solidbrush (Color.Fromargb (153, 255, 255)); // Reopromes copyright information to make it have a shadow effect // put text Move a pixel Grphoto.drawstring (Copyright, // copyright information crFont, // font semitransbrush, // Draw text color and texture new pointf (xcenterofimg, yposfrombottom), // Draw text Strfo RMAT); // format // ------------------------------------------ ------------------ // Step 2, inserted into the picture water printing // --------------------- --------------------------------------- // Create a one in the original modified BMPHOTO mercury bitmap bitmap bmWatermark = new bitmap (bmPhoto); bmWatermark.SetResolution (imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); // bitmap bmWatermark loaded Graphics object Graphics grWatermark = Graphics.FromImage (bmWatermark);

// To achieve a transulcent watermark we will apply (2) color // manipulations by defineing a ImageAttributes object and // seting (2) of its properties ImageAttributes imageAttributes = new ImageAttributes ();. // The first step in manipulating the watermark Image is to replace /// The Background Color with One That Is Trarative (alpha = 0, r = 0, g = 0, b = 0) // to do this we will use a colorMap and use this to define a Remaptable ColorMap ColorMap = New colorMap (); // my watermark Was defined with a background of 100% Green this will // be the color we search for and replace with transparency color.oldcolor = color.fromargb (255, 0, 255, 0); colorMap.NewColor = Color.FromArgb (0, 0, 0, 0); ColorMap [] remapTable = {colorMap}; imageAttributes.SetRemapTable (remapTable, ColorAdjustType.Bitmap); // The second color manipulation is used to change th e.comermark. 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 a Level of Opacity float [] [] [ ] ColorMatrixElements =

{New float [] {1.0F, 0.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.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.0 f, 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.drawima GE (Imgwatermark, New Rectangle (Xposofwm, Yposofwm, Wmwidth, Wmheight), // set the points 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; grPhoto .Dispose (); grwatermark.dispose ();

转载请注明原文地址:https://www.9cbs.com/read-131839.html

New Post(0)