using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace VisibleWatermarking
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }
        Bitmap mainPic;
        private void btnPic_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "Images (*.jpg,*.png) | *.jpg; *.png;";
            if(op.ShowDialog()==DialogResult.OK)
            {
                pictureBox1.Image = ScaleImage(Image.FromFile(op.FileName), pictureBox1.Width, pictureBox1.Height);
                mainPic = new Bitmap(pictureBox1.Image);
            }
           
        }
        public Image ScaleImage(Image img,int maxW,int maxH)
        {
            var ratioX =(double)maxW / img.Width;
            var ratioY= (double)maxH / img.Height;

            var newW =(int)(img.Width * ratioX);
            var newH = (int)(img.Height * ratioY);
            Bitmap b = new Bitmap(newW, newH);
            using (var g = Graphics.FromImage(b))
            {
                g.DrawImage(img, 0, 0, newW, newH);
            }
            return b;
        }

        private void btnWatermark_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            op.Filter = "Images (*.jpg,*.png) | *.jpg; *.png;";
            if (op.ShowDialog() == DialogResult.OK)
            {
                pictureBox2.Image = ScaleImage(Image.FromFile(op.FileName), pictureBox2.Width, pictureBox2.Height);
            }


            pictureBox2.Visible = false;
            Bitmap result = new Bitmap(pictureBox1.Image);
            Bitmap watermark = new Bitmap(pictureBox2.Image);
            DrawWatermark(watermark, result, 0, 0);
            pictureBox1.Image = result;

        }
        private void DrawWatermark(Bitmap watermark,Bitmap result,int x,int y)
        {
            ColorMatrix cm = new ColorMatrix();
            cm.Matrix33 = 0.7f;
            if (chkTransparent.Checked)
                watermark.MakeTransparent(watermark.GetPixel(0, 0));
            ImageAttributes imgAtr = new ImageAttributes();
            imgAtr.SetColorMatrices(cm, null);
            using (var g = Graphics.FromImage(result))
            {
                Rectangle rect = new Rectangle(x, y, watermark.Width, watermark.Height);
                g.DrawImage(watermark, rect, 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imgAtr);
            }

        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            Bitmap result = new Bitmap(mainPic);
            Bitmap watermark = new Bitmap(pictureBox2.Image);
            DrawWatermark(watermark, result, trackBar1.Value, trackBar2.Value);
            pictureBox1.Image = result;
        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {

            Bitmap result = new Bitmap(mainPic);
            Bitmap watermark = new Bitmap(pictureBox2.Image);
            DrawWatermark(watermark, result, trackBar1.Value, trackBar2.Value);
            pictureBox1.Image = result;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            trackBar1.Maximum = pictureBox1.Width - pictureBox2.Width;
            trackBar2.Maximum = pictureBox1.Height - pictureBox2.Height;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog();
            sf.Filter= "Images (*.jpg,*.png) | *.jpg; *.png;";
            if (sf.ShowDialog()==DialogResult.OK)
            {
                pictureBox1.Image.Save(sf.FileName, ImageFormat.Jpeg);
            }
        }
    }
}
