Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 Picture_FromFile()
- {
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- return openFileDialog.FileName;
- }
- return "";
- }
- public string Picture_SaveFile()
- {
- if (saveFileDialog.ShowDialog() == DialogResult.OK);
- saveFileDialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*"; //фильтруем все картинки по формату
- {
- return saveFileDialog.FileName;
- }
- return "";
- }
- private void openButton_Click(object sender, EventArgs e)
- {
- Bitmap image;
- AutoScroll = true;
- AutoScrollMinSize = pictureBox1.Size;
- {
- open_dialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*"; //фильтруем все картинки по формату
- 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)
- {
- Picture_SaveFile();
- }
- private void addWaterMark_Click(object sender, EventArgs e)
- {
- using (Image image = Image.FromFile(Picture_FromFile()))
- using (Image watermarkImage = Image.FromFile(Picture_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)));
- }
- }
- }
- }
- public string Picture_SaveFile()
- {
- // фильтруем все картинки по формату
- saveFileDialog.Filter =
- "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*";
- if (saveFileDialog.ShowDialog() == DialogResult.OK)
- {
- return saveFileDialog.FileName;
- }
- return "";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement