c#批量给图片添加水印,

何必tom 2013-10-11 07:56:04
想做一个小工具!批量给图片加上自己的透明图片水印和透明文字,求教.
已经创建了一个带透明的png图片作为水印.如何合并到背景图片上.?
求给代码和思路.谢谢
...全文
310 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
拥抱开源 2013-10-12
  • 打赏
  • 举报
回复
标记一下 便于学习使用
何必tom 2013-10-12
  • 打赏
  • 举报
回复
引用 3 楼 xtdhb 的回复:

......
      ...
谢谢 !!
lwwcomeon 2013-10-12
  • 打赏
  • 举报
回复
留名学习学习
老张一笑 2013-10-11
  • 打赏
  • 举报
回复

 #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 

何必tom 2013-10-11
  • 打赏
  • 举报
回复
引用 1 楼 lhx527099095 的回复:
原来写的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;
        }
    }
谢谢楼上的!先看看.
lhx527099095 2013-10-11
  • 打赏
  • 举报
回复
原来写的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;
        }
    }

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧