2015/11/09

C#.Net 實現簡單浮水印功能

要實現浮水印大致上最基礎的方法是去對ARGB做處理
但個人只是純粹好玩而已,並沒有針對浮水印做太多思考

Code:
using System.Drawing;
using System.Windows.Forms;

namespace WaterMarkImage
{
    public partial class Form1 : Form
    {


        /// <summary>
        /// 背景圖
        /// </summary>
        private Bitmap bitmapBack;

        /// <summary>
        /// 用於浮水印圖
        /// </summary>
        private Bitmap bitmapWaterMark;


        public Form1()
        {
            InitializeComponent();
        }

        private void buttonMark_MouseClick(object sender, MouseEventArgs e)
        {

            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "JPG File|*.jpg";

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                Graphics g = Graphics.FromImage(bitmapBack);
                g.DrawImage(bitmapWaterMark, new Point(0, 0));
                bitmapBack.Save(dialog.FileName);
                MessageBox.Show("製作成功");
            }
        }

        private void buttonWaterMark_MouseClick(object sender, MouseEventArgs e)
        {

            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "JPG File|*.jpg";

            if (dialog.ShowDialog() == DialogResult.OK)
            {

                bitmapWaterMark = new Bitmap(dialog.FileName);
                for (int x = 0; x < bitmapWaterMark.Width; x++)
                {
                    for (int y = 0; y < bitmapWaterMark.Height; y++)
                    {
                        Color pixel = bitmapWaterMark.GetPixel(x, y);
                        int red = pixel.R / 2;
                        int green = pixel.G / 2;
                        int blue = pixel.B / 2;
                        int alpha = pixel.A / 10;
                        pixel = Color.FromArgb(alpha, red, green, blue);
                        bitmapWaterMark.SetPixel(x, y, pixel);
                    }
                }
            }
        }

        private void buttonImage_MouseClick(object sender, MouseEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "JPG File|*.jpg";

            if (dialog.ShowDialog() == DialogResult.OK)
                bitmapBack = new Bitmap(dialog.FileName);
        }


    }
}


執行結果: