TutorialASP.net adds a logo image watermark to the uploaded image. The quality of the generated image is very high. view the instance Code directly:
/// <Summary> /// Add a logo image watermark /// </Summary> /// <Param name = "FilePath"> source image address </param> /// <Param name = "SavePath"> image storage path </param> Public static void AddWaterMark (string FilePath, string SavePath) { Bitmap bitmap = new Bitmap (FilePath ); Graphics g = Graphics. FromImage (bitmap ); System. Drawing. Image logo = System. Drawing. Image. FromFile (HttpContext. Current. Server. MapPath ("~ /Images/top/"+ ConfigurationManager. AppSettings [" WaterMarkLogo "]. ToString (); // load the logo image If (bitmap. Width <logo. Width | bitmap. Height <= logo. Height) Return; // Define a rectangle area below, and then draw a transparent background and a white font in the rectangle. Float rectWidth = (float) logo. Width; Float rectHeight = (float) logo. Height; Float rectX = bitmap. Width-rectWidth; Float rectY = bitmap. Height-rectHeight; // Declare the rectangular Field RectangleF textArea = new RectangleF (rectX, rectY, rectWidth, rectHeight ); G. DrawImage (logo, textArea ); MemoryStream MS = new MemoryStream (); // Save as Jpg Bitmap. Save (MS, System. Drawing. Imaging. ImageFormat. Jpeg ); G. Dispose (); Bitmap. Dispose (); Logo. Dispose (); FileStream fs = new FileStream (SavePath, FileMode. OpenOrCreate ); Fs. Write (ms. ToArray (), 0, ms. ToArray (). Length ); Fs. Close (); }
|