首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#批量给图片平添水印

2013-10-14 
c#批量给图片添加水印,想做一个小工具!批量给图片加上自己的透明图片水印和透明文字,求教.已经创建了一个

c#批量给图片添加水印,
想做一个小工具!批量给图片加上自己的透明图片水印和透明文字,求教.
已经创建了一个带透明的png图片作为水印.如何合并到背景图片上.?
求给代码和思路.谢谢 c# 图片 bitmap graphics
[解决办法]
原来写的wpf的打水印的 楼主可以借鉴下


public class WatermarkHelper
    {
        string _waterImgPath;
        BitmapSource _waterPic;
        string _prefix = null;

        // constructors
        public WatermarkHelper(string waterImgFilePath, string prefix = null)
        {
            this._waterImgPath = waterImgFilePath;
            this._waterPic = new BitmapImage(new Uri(this._waterImgPath));
            if (prefix == null)
                prefix = "";
            this._prefix = prefix;
            // this._result = false;
        }


        // public members
        public void AddWatermark_SaveToDir(string inputFilePath, string outputDir)
        {
            if (!Directory.Exists(outputDir))
                Directory.CreateDirectory(outputDir);
            string output_filepath = Path.Combine(
                outputDir,
                string.Format("{0}{1}", this._prefix, Path.GetFileName(inputFilePath))
                );  // <-- use Path.Combine to prevent potential problems

            //  process
            Process(inputFilePath, output_filepath);
        }


        public void AddWatermark_SaveToFile(string inputFilePath, string output_filepath) // <-- you see what i mean when i say 'flexibility'
        {
            // create directory if it doesn't exist yet
            string output_dir = Path.GetDirectoryName(output_filepath);
            if (!Directory.Exists(output_dir))
                Directory.CreateDirectory(output_dir);

            //  process
            Process(inputFilePath, output_filepath);
        }


        // internal logic
        bool Process(string input_filepath, string output_filepath) // <-- let upper logic determine the output filename, these will provide flexibility
        {
            // load source image
            // BitmapSource bitmap = new BitmapImage(new Uri(input_filepath));
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource = new Uri(input_filepath);
            bitmap.CacheOption = BitmapCacheOption.OnLoad;


            bitmap.EndInit();

            // create render bitmap
            var rtbitmap = new RenderTargetBitmap(bitmap.PixelWidth,
                bitmap.PixelHeight,
                bitmap.DpiX,
                bitmap.DpiY,
                PixelFormats.Default);
            // draw watermark
            var drawingVisual = new DrawingVisual();
            using (var dc = drawingVisual.RenderOpen())
            {
                // draw source image
                dc.DrawImage(bitmap, new Rect(0, 0, bitmap.Width, bitmap.Height));
                // determine the render size of the watermark
                double hs = bitmap.Height * 0.3 / this._waterPic.Height;
                double ws = bitmap.Width * 0.3 / this._waterPic.Width;
                double scale = hs > ws ? ws : hs;
                if (bitmap.Height / bitmap.Width > 5)
                {
                    scale = bitmap.Width * 0.8 / this._waterPic.Width;
                }
                else if (bitmap.Width / bitmap.Height > 5)
                {
                    scale = bitmap.Height * 0.8 / this._waterPic.Height;
                }
                double Wstart = bitmap.Width - this._waterPic.Width * scale - 20 < 0 ? 0 : bitmap.Width - this._waterPic.Width * scale - 20;
                double Hstart = bitmap.Height - this._waterPic.Height * scale;
                // draw water make
                dc.DrawImage(this._waterPic, new Rect(Wstart, Hstart, this._waterPic.Width * scale, this._waterPic.Height * scale));
            }
            rtbitmap.Render(drawingVisual);
            var bitmapEncoder = new PngBitmapEncoder();
            bitmapEncoder.Frames.Add(BitmapFrame.Create(rtbitmap));

            // save image
            if (File.Exists(output_filepath))
                File.Delete(output_filepath);

            using (FileStream file = new FileStream(output_filepath, FileMode.Create))
                bitmapEncoder.Save(file);

            // return
            return true;
        }
    }


[解决办法]

 #region 图片加上水印的操作

        /// <summary>


        /// 向图片上写文字再输出
        /// </summary>
        /// <param name="img"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static Image WriteString(Image img, string text)
        {
            Bitmap bmap = new Bitmap(img);
            Graphics g = Graphics.FromImage(bmap);
            Font font = new Font("楷体", 15);
            PointF pf = new PointF();//((float)(img.Width/2-2.5),img.Height/2-2.5);
            pf.X = 0;
            pf.Y = 0;

            g.DrawString(text, font, Brushes.Gold, pf);
            g.Dispose();

            return bmap;
        }
        /// <summary>
        /// 将图片加载水印图片
        /// </summary>
        /// <param name="markImg"></param>
        /// <param name="fromImg"></param>
        /// <returns></returns>
        public static Image WriteImg(Image markImg, Image fromImg)
        {
            Bitmap b = new Bitmap(fromImg);
            Graphics g = Graphics.FromImage(b);
            g.DrawImage(markImg, 0, 0, markImg.Width, markImg.Height);
            g.Dispose();
            return b;
        }

        #endregion 

热点排行