using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace addwaternark
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string ABS_FromFile()
{
openFileDialog.DefaultExt = "png";
openFileDialog.Filter = "Image files (*.png;*jpg;*gif)|*.png; *jpg; *gif;|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
return openFileDialog.FileName;
}
return "";
}
public string ABS_SaveFile()
{
saveFileDialog.DefaultExt = "png";
saveFileDialog.Filter = "Image files (*.png;*jpg;*gif)|*.png; *jpg; *gif;|All files (*.*)|*.*";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
pictureBox1.Refresh();
{
return saveFileDialog.FileName;
}
return "";
}
private void openButton_Click(object sender, EventArgs e)
{
Bitmap image;
AutoScroll = true;
AutoScrollMinSize = pictureBox1.Size;
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
fileNameBox.Text = openFileDialog.FileName;
}
}
{
try
{
image = new Bitmap(openFileDialog.FileName); //Bitmap для открываемой картинки
pictureBox1.Size = image.Size; //Картинка подгоняется под размер picturebox
pictureBox1.Image = image;
pictureBox1.Invalidate();
}
catch
{
DialogResult result = MessageBox.Show("Невозможно открыть выбранный файл",
"Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); //Если это НЕ картинка
}
}
}
private void saveAs_Click(object sender, EventArgs e)
{
Bitmap pngSave = (Bitmap)pictureBox1.Image;
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "png";
sfd.Filter = "Image files (*.png;*jpg;*gif)|*.png; *jpg; *gif;|All files (*.*)|*.*";
if (sfd.ShowDialog() == DialogResult.OK);
}
private void addWaterMark_Click(object sender, EventArgs e)
{
using (Image image = Image.FromFile(ABS_FromFile()))
using (Image watermarkImage = Image.FromFile(ABS_FromFile()))
using (Graphics imageGraphics = Graphics.FromImage(image))
using (TextureBrush watermarkBrush = new TextureBrush(watermarkImage))
{
int x = (image.Width / 2 - watermarkImage.Width / 2);
int y = (image.Height / 2 - watermarkImage.Height / 2);
watermarkBrush.TranslateTransform(x, y);
imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(watermarkImage.Width + 1, watermarkImage.Height)));
image.Save(ABS_SaveFile());
}
}
}
}
if (sfd.ShowDialog() == DialogResult.OK);
if (saveFileDialog.ShowDialog() == DialogResult.OK)
pictureBox1.Refresh();
{
return saveFileDialog.FileName;
}
return "";