Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace addwaternark
  12. {
  13. public partial class Form1 : Form
  14. {
  15.  
  16.  
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21.  
  22. public string Picture_FromFile()
  23. {
  24.  
  25. if (openFileDialog.ShowDialog() == DialogResult.OK)
  26.  
  27. {
  28.  
  29. return openFileDialog.FileName;
  30. }
  31. return "";
  32. }
  33. public string Picture_SaveFile()
  34. {
  35.  
  36. if (saveFileDialog.ShowDialog() == DialogResult.OK);
  37. saveFileDialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*"; //фильтруем все картинки по формату
  38. {
  39. return saveFileDialog.FileName;
  40. }
  41. return "";
  42. }
  43.  
  44. private void openButton_Click(object sender, EventArgs e)
  45. {
  46. Bitmap image;
  47. AutoScroll = true;
  48. AutoScrollMinSize = pictureBox1.Size;
  49.  
  50. {
  51. open_dialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*"; //фильтруем все картинки по формату
  52. if (openFileDialog.ShowDialog() == DialogResult.OK)
  53. {
  54.  
  55. fileNameBox.Text = openFileDialog.FileName;
  56. }
  57. }
  58.  
  59. {
  60. try
  61. {
  62. image = new Bitmap(openFileDialog.FileName); //Bitmap для открываемой картинки
  63.  
  64.  
  65. pictureBox1.Size = image.Size; //Картинка подгоняется под размер picturebox
  66. pictureBox1.Image = image;
  67. pictureBox1.Invalidate();
  68. }
  69.  
  70. catch
  71. {
  72. DialogResult result = MessageBox.Show("Невозможно открыть выбранный файл",
  73. "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); //Если это НЕ картинка
  74. }
  75. }
  76. }
  77.  
  78. private void saveAs_Click(object sender, EventArgs e)
  79. {
  80. Picture_SaveFile();
  81. }
  82.  
  83. private void addWaterMark_Click(object sender, EventArgs e)
  84. {
  85.  
  86. using (Image image = Image.FromFile(Picture_FromFile()))
  87. using (Image watermarkImage = Image.FromFile(Picture_FromFile()))
  88. using (Graphics imageGraphics = Graphics.FromImage(image))
  89. using (TextureBrush watermarkBrush = new TextureBrush(watermarkImage))
  90. {
  91. int x = (image.Width / 2 - watermarkImage.Width / 2);
  92. int y = (image.Height / 2 - watermarkImage.Height / 2);
  93. watermarkBrush.TranslateTransform(x, y);
  94. imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(watermarkImage.Width + 1, watermarkImage.Height)));
  95.  
  96. }
  97. }
  98. }
  99. }
  100.  
  101. public string Picture_SaveFile()
  102. {
  103. // фильтруем все картинки по формату
  104. saveFileDialog.Filter =
  105. "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*";
  106. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  107. {
  108. return saveFileDialog.FileName;
  109. }
  110. return "";
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement