想做一个小工具!批量给图片加上自己的透明图片水印和透明文字,求教.
已经创建了一个带透明的png图片作为水印.如何合并到背景图片上.?
求给代码和思路.谢谢c#图片bitmapgraphics

解决方案 »

  1.   

    原来写的wpf的打水印的 楼主可以借鉴下
    public class WaterHelper
        {
            string _waterImgPath;
            BitmapSource _waterPic;
            string _prefix = null;        // constructors
            public WaterHelper(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 AddWater_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 AddWater_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 water
                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 water
                    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;
            }
        }
      

  2.   


     #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="Img"></param>
            /// <param name="fromImg"></param>
            /// <returns></returns>
            public static Image WriteImg(Image Img, Image fromImg)
            {
                Bitmap b = new Bitmap(fromImg);
                Graphics g = Graphics.FromImage(b);
                g.DrawImage(Img, 0, 0, Img.Width, Img.Height);
                g.Dispose();
                return b;
            }        #endregion