您的位置:首页 > 编程语言 > C#

C#写的给图片加水印的实例代码

2009-11-07 10:39 573 查看
发现以前写的给图片加水印的实例代码,实际上是就是图片叠加的代码。

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace Enter_name
{
    class GenerateImage
    {
        public struct favoriteImage
        {
            private string _imagePath;
            private int _x;
            private int _y;
            public int x
            {
                get
                {
                    return _x;
                }
                set
                {
                    _x = value;
                }
            }
            public int y
            {
                get
                {
                    return _y;
                }
                set
                {
                    _y = value;
                }
            }
            public string imagePath
            {
                get
                {
                    return _imagePath;
                }
                set
                {
                    _imagePath = value;
                }
            }
        }
        [STAThread]
        static void Main(string[] args)
        {
            string CurrentDirectory = System.Environment.CurrentDirectory;
            string body_path = CurrentDirectory + "//4.jpg";
            favoriteImage[] FaImage = new favoriteImage[2];
            FaImage[0].x = -3;
            FaImage[0].y = 70;
            FaImage[0].imagePath = CurrentDirectory + "//1.jpg";
            FaImage[1].x = 20;//65;
            FaImage[1].y = -12;
            FaImage[1].imagePath = CurrentDirectory + "//2.jpg";
            generateWinterMark(CurrentDirectory, body_path, FaImage);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="savePath"></param>
        /// <param name="body_path"></param>
        /// <param name="favorite"></param>
        /// <returns></returns>        
        private static string generateWinterMark(string savePath, string body_path, favoriteImage[] favorite)
        {
            //create a image object containing the photograph to watermark
            Bitmap imgPhoto = new Bitmap(body_path);
            int phWidth = imgPhoto.Width;
            int phHeight = imgPhoto.Height;

            string nowTime = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
            nowTime += DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
            string saveImagePath = savePath + "//FA" + nowTime + ".jpg";
            //create a Bitmap the Size of the original photograph
            Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);
            //setResolution 
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
            //load the Bitmap into a Graphics object 
            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            //set the rendering quality for this Graphics object
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
            for (int i = 0; i < favorite.Length; i++)
            {
                //Draws the photo Image object at original size to the graphics object.
                grPhoto.DrawImage(
                    imgPhoto,                               // Photo Image object
                    new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure
                    0,                                      // x-coordinate of the portion of the source image to draw. 
                    0,                                      // y-coordinate of the portion of the source image to draw. 
                    phWidth,                                // Width of the portion of the source image to draw. 
                    phHeight,                               // Height of the portion of the source image to draw. 
                    GraphicsUnit.Pixel);                    // Units of measure 
                //------------------------------------------------------------
                //Step #2 - Insert Property image,For example:hair,skirt,shoes etc.
                //------------------------------------------------------------
                //create a image object containing the watermark
                Bitmap imgWatermark = new Bitmap(favorite[i].imagePath);
                int wmWidth = imgWatermark.Width;
                int wmHeight = imgWatermark.Height;
                imgWatermark.MakeTransparent(); //使默认的透明颜色对此 Bitmap 透明。

                //Create a Bitmap based on the previously modified photograph Bitmap
                Bitmap bmWatermark = new Bitmap(bmPhoto);
                //bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
                //Load this Bitmap into a new Graphic Object
                Graphics grWatermark = Graphics.FromImage(bmWatermark);

                int xPosOfWm = favorite[i].x;
                int yPosOfWm = favorite[i].y;
                //叠加
                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
                null);   //ImageAttributes Object

                //Replace the original photgraphs bitmap with the new Bitmap
                imgPhoto = bmWatermark;//age(FromHbitmap(bmWatermark;
                grWatermark.Dispose();
                imgWatermark.Dispose();
            }
            bmPhoto.Dispose();
            grPhoto.Dispose();
            //save new image to file system.
            imgPhoto.Save(saveImagePath, ImageFormat.Jpeg);
            imgPhoto.Dispose();
            return saveImagePath;
        }
    }
}





源码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: