如何使用位图向多个图像添加水印。

huangapple go评论105阅读模式
英文:

How do I add a watermark to multiple images using bitmap

问题

我正在尝试将文本水印添加到从目录中加载的多个图像,并将其保存到另一个目录。以下是我目前的代码:

using System.Web.Mvc;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace something.Controllers
{
    public class something : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(string text, string text1)
        {
            DirectoryInfo dir = new DirectoryInfo(@"C:\Users\name\Desktop\Images");
            FileInfo[] files = dir.GetFiles();
            string value = text;
            string value1 = text1;
            foreach (FileInfo file in files)
            {
                using (Bitmap bitmap = new Bitmap(file.FullName))
                {
                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
                        Brush brush = new SolidBrush(Color.Red);
                        Font font = new Font("Arial", 10, FontStyle.Italic, GraphicsUnit.Pixel);
                        SizeF textSize = new SizeF();
                        textSize = graphics.MeasureString(value, font);
                        Point position = new Point(bitmap.Width - ((int)textSize.Width + 30), bitmap.Height - ((int)textSize.Height + 10));
                        graphics.DrawString((value + value1), font, brush, position);
                        using (MemoryStream mStream = new MemoryStream())
                        {
                            mStream.Position = 0;
                            bitmap.Save(mStream, ImageFormat.Png);
    
                            string _path = Path.Combine(Server.MapPath("~/UploadedFolders"), file.Name);
                            bitmap.Save(_path);
                        }
                    }
                }
            }
            return View();
        }
    }
}

这段代码接受用户输入并将其附加到图像,这部分正常工作。

问题是:使用这段代码,它只会对文件夹中的第一个图像添加水印并保存。而不是对所有图像添加水印。

谢谢!

英文:

I am trying to add text watermark to multiple images from a directory and save it to a directory. Here is what I have so far:

using System.Web.Mvc;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace something.Controllers
{

    public class something: Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(string text, string text1)
        {
            DirectoryInfo dir = new DirectoryInfo(@"C:\Users\name\Desktop\Images");
            FileInfo[] files = dir.GetFiles();
            string value = text;
            string value1 = text1;
            foreach (FileInfo file in files)
            {
          

                using (Bitmap bitmap = new Bitmap( file.FullName))
                {
                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
                        Brush brush = new SolidBrush(Color.Red);
                        Font font = new Font("Arial", 10, FontStyle.Italic, 
                        GraphicsUnit.Pixel);
                        SizeF textSize = new SizeF();
                        textSize = graphics.MeasureString(value, font);
                        Point position = new Point(bitmap.Width - ((int)textSize.Width + 
                        30), bitmap.Height - ((int)textSize.Height + 10));
                        graphics.DrawString((value + value1), font, brush, position);
                        using (MemoryStream mStream = new MemoryStream())
                        {
                            mStream.Position = 0;
                            bitmap.Save(mStream, ImageFormat.Png);
                           


                            string _path = 
                            Path.Combine(Server.MapPath("~/UploadedFolders"), 
                            file.Name);
                            bitmap.Save(_path);
                            return File(mStream.ToArray(), "image/png", file.Name);
                        }
                    }
                }



            }
            return View();
        }

    }

}

This code take user input and append it to the image this part works fine.
The issue:
With this code it goes to the path and only add a water mark to the first image in the folder and save it. Not all images.
Thank you

答案1

得分: 1

以下代码可行:

public void watermarkImage(string sourceImage, string text, string targetImage, ImageFormat fmt) {
    try
    {
        // 打开源图像为流并创建输出的内存流
        FileStream source = new FileStream(sourceImage, FileMode.Open);
        Stream output = new MemoryStream();
        Image img = Image.FromStream(source);

        // 选择文本字体
        Font font = new Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Pixel);

        // 选择颜色和透明度
        Color color = Color.FromArgb(100, 255, 0, 0);

        // 水印文本在父图像中的位置
        Point pt = new Point(10, 5);
        SolidBrush brush = new SolidBrush(color);

        // 在图像上绘制文本
        Graphics graphics = Graphics.FromImage(img);
        graphics.DrawString(text, font, brush, pt);
        graphics.Dispose();

        // 更新图像内存流
        img.Save(output, fmt);
        Image imgFinal = Image.FromStream(output);

        // 将修改后的图像写入文件
        Bitmap bmp = new System.Drawing.Bitmap(img.Width, img.Height, img.PixelFormat);
        Graphics graphics2 = Graphics.FromImage(bmp);
        graphics2.DrawImage(imgFinal, new Point(0, 0));
        bmp.Save(targetImage, fmt);

        imgFinal.Dispose();
        img.Dispose();
    }
    catch (Exception ex) {
        MessageBox.Show(ex.Message);
    }
}
英文:

Below Code works :

public void watermarkImage(string sourceImage, string text, string targetImage, ImageFormat fmt) {

        try
        {
            // open source image as stream and create a memorystream for output
            FileStream source = new FileStream(sourceImage, FileMode.Open);
            Stream output = new MemoryStream();
            Image img = Image.FromStream(source);

            // choose font for text
            Font font = new Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Pixel);

            //choose color and transparency
            Color color = Color.FromArgb(100, 255, 0, 0);

            //location of the watermark text in the parent image
            Point pt = new Point(10, 5);
            SolidBrush brush = new SolidBrush(color);

            //draw text on image
            Graphics graphics = Graphics.FromImage(img);
            graphics.DrawString(text, font, brush, pt);
            graphics.Dispose();

            //update image memorystream
            img.Save(output, fmt);
            Image imgFinal = Image.FromStream(output);
            
            //write modified image to file
            Bitmap bmp = new System.Drawing.Bitmap(img.Width, img.Height, img.PixelFormat);
            Graphics graphics2 = Graphics.FromImage(bmp);
            graphics2.DrawImage(imgFinal, new Point(0, 0));
            bmp.Save(targetImage, fmt);

            imgFinal.Dispose();
            img.Dispose();



        }
        catch (Exception ex) {
            MessageBox.Show(ex.Message);
        }
    }

答案2

得分: 0

只需要移除这一行:

return File(mStream.ToArray(), "image/png", file.Name);
代码运行良好,如果有人需要,这是另一个参考。

英文:

simply needed to remove this line:

return File(mStream.ToArray(), "image/png", file.Name);

Code works good and is another reference if someone needs it.

huangapple
  • 本文由 发表于 2020年1月4日 01:41:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/59583002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定