コード例 #1
0
        /// <summary>
        /// 添加水印(分图片水印与文字水印两种)
        /// </summary>
        /// <param name="oldpath">原图片绝对地址</param>
        /// <param name="newpath">新图片放置的绝对地址</param>
        /// <param name="wmpPosition">要添加的水印的位置</param>
        /// <param name="wmtType">要添加的水印的类型</param>
        /// <param name="sWaterMarkContent">水印内容,若添加文字水印,此即为要添加的文字;
        /// 若要添加图片水印,此为图片的路径</param>
        public void addWaterMark(string oldpath, string newpath, WaterMarkPosition wmpPosition,
                                 WaterMarkType wmtType, string sWaterMarkContent)
        {
            try
            {
                Image image = Image.FromFile(oldpath);

                Bitmap b = new Bitmap(image.Width, image.Height,
                                      PixelFormat.Format24bppRgb);

                Graphics g = Graphics.FromImage(b);
                g.Clear(Color.White);
                g.SmoothingMode     = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.High;

                g.DrawImage(image, 0, 0, image.Width, image.Height);

                switch (wmtType)
                {
                case WaterMarkType.TextMark:
                    //文字水印
                    this.addWatermarkText(g, sWaterMarkContent, wmpPosition.ToString(),
                                          image.Width, image.Height);
                    break;

                case WaterMarkType.ImageMark:
                    //图片水印
                    Image imageWatermark = Image.FromFile(sWaterMarkContent);
                    this.addWatermarkImage(g, imageWatermark, wmpPosition.ToString(), image.Width, image.Height);
                    break;
                }

                b.Save(newpath);
                b.Dispose();
                image.Dispose();
            }
            catch
            {
                if (File.Exists(oldpath))
                {
                    //File.Delete(oldpath);
                }
            }
            finally
            {
                if (File.Exists(oldpath))
                {
                    //File.Delete(oldpath);
                }
            }
        }