C#:如何在目录图片的左下角添加图像水印? [英] C#: how can I add image watermark at left bottom of a directory's pictures?

查看:179
本文介绍了C#:如何在目录图片的左下角添加图像水印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了C#代码,用于通过

I found C# code for add a text watermark to photos of a directory by

FileStream

为目录的照片添加文本水印。

但我有问题。 ..



- 如何从左下角协调图片(用于添加图像水印(不是文本水印)到左下角所有照片的一角)?



我的尝试:



.
But I have problem with that...

- How can I coordinate the picture from bottom left(for adding an "Image watermark"(not text watermark) to the "bottom left" corner of all pictures)?

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
 
namespace WatermarkImages
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.folderBrowserDialog1.Description =
            "Select the images directory";
 
            // Disallow creation of new files using the FolderBrowserDialog.
            this.folderBrowserDialog1.ShowNewFolderButton = false;
        }
 
       
        private void btnWatermark_Click(object sender, EventArgs e)
        {
            string path = String.Empty;
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                path = folderBrowserDialog1.SelectedPath;
            }
 
            if (path == String.Empty)
            {
                lblStatus.Text = "Invalid directory..";
                return;
            }
 
 
            lblStatus.Text = String.Empty;                       
            Image img = null;
            string fullPath = String.Empty;
           
            try
            {
                string[] imgExtension = { "*.jpg", "*.jpeg", ".gif", "*.bmp" };
                List<FileInfo> files = new List<FileInfo>();               
               
                DirectoryInfo dir = new DirectoryInfo(path);
               
                foreach (string ext in imgExtension)
                {
                    FileInfo[] folder = dir.GetFiles(ext, SearchOption.AllDirectories);
                    foreach (FileInfo file in folder)
                    {
                        FileStream fs = file.OpenRead();
                        fullPath = path + @"\" + file.Name ;
           
                        Stream outputStream = new MemoryStream();
                        AddWatermark(fs, "www.dotnetcurry.com", outputStream);
                        fs.Close();
                        file.Delete();
                        img = Image.FromStream(outputStream);
 
                        using (Bitmap savingImage = new Bitmap(img.Width, img.Height, img.PixelFormat))
                        {
                            using (Graphics g = Graphics.FromImage(savingImage))
                                g.DrawImage(img, new Point(0, 0));
                            savingImage.Save(fullPath, ImageFormat.Jpeg);            
                           
                        }
                      
                        img.Dispose();
                       
                    }
                }
                lblStatus.Text = "Processing Completed";
            }
            catch (Exception ex)
           {
               lblStatus.Text = "There was an error during processing..";           
            }
            finally
            {
                if (img != null)
                    img.Dispose();
            }
      }
 
 
        public void AddWatermark(FileStream fs, string watermarkText, Stream outputStream)
        {
            Image img = Image.FromStream(fs);               
            Font font = new Font("Verdana", 16, FontStyle.Bold, GraphicsUnit.Pixel);
            //Adds a transparent watermark with an 100 alpha value.
            Color color = Color.FromArgb(100, 0, 0, 0);
            //The position where to draw the watermark on the image
            Point pt = new Point(10, 30);
            SolidBrush sbrush = new SolidBrush(color);
 
            Graphics gr = null;
            try
            {
                gr = Graphics.FromImage(img);
            }
            catch
            {
                // http://support.microsoft.com/Default.aspx?id=814675
                Image img1 = img;
                img = new Bitmap(img.Width, img.Height);
                gr = Graphics.FromImage(img);
                gr.DrawImage(img1, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
                img1.Dispose();
            }
                       
            gr.DrawString(watermarkText, font, sbrush, pt);
            gr.Dispose();
           
            img.Save(outputStream, ImageFormat.Jpeg);
        }
    
    }
}

推荐答案

两件事:你不能在目录中添加水印,你必须分别将它添加到每个单独的图片中,并且你有代码。



但其他的东西是那个你正在阅读和编写JPEG文件,JPEG是一种有损压缩格式 - 这意味着每次你编写文件时,你都会丢弃信息并且图像质量下降 - 它不需要很多读写周期确实得到了一个非常糟糕的形象。



当您最初保存文件时使用水印并使用非损耗格式(如GIF或PNG而不是JPEG。
Two things: you can't "add a watermark to a directory", you would have to add it to each individual picture separately, and you have code for that.

But the other things is that you are reading and writing JPEG files, and JPEG is a "lossy compression" format - which means that each time you write the file, you throw away information and the image quality is degraded - it doesn't take many read-write cycles to get a very poor image indeed.

You would be much, much better off applying your watermark when you originally save the file, and using a non-lossy format such as GIF or PNG instead of JPEG.


这篇关于C#:如何在目录图片的左下角添加图像水印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆