Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.78 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. using System.Drawing.Imaging;
  11. using System.Runtime.InteropServices;
  12. using Graphics;
  13. using System.Drawing.Printing;
  14.  
  15. namespace Graphics
  16. {
  17.     public partial class Form1 : Form
  18.     {
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         private Bitmap bmp;
  25.         private Pen pen_for_draw = new Pen(Color.Black, 4);
  26.         private string name_of_image;
  27.  
  28.         private void button1_Click(object sender, EventArgs e)
  29.         {
  30.             {
  31.                 OpenFileDialog open_dialog = new OpenFileDialog();
  32.                 open_dialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*";
  33.                 if (open_dialog.ShowDialog() == DialogResult.OK)
  34.                 {
  35.                     try
  36.                     {
  37.                         name_of_image = open_dialog.FileName;
  38.                         bmp = new Bitmap(open_dialog.FileName);
  39.                         this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
  40.                         pictureBox1.Image = bmp;
  41.                         pictureBox1.Invalidate();
  42.                         button2.Visible = true;
  43.                         button3.Visible = true;
  44.                         button4.Visible = true;
  45.                         textBox1.Visible = true;
  46.                         textBox2.Visible = true;
  47.                         textBox3.Visible = true;
  48.                         textBox4.Visible = true;
  49.                         textBox5.Visible = true;
  50.                         label1.Visible = true;
  51.                         label1.Text = bmp.Width.ToString() + " " + bmp.Height.ToString();
  52.                     }
  53.                     catch
  54.                     {
  55.                         DialogResult rezult = MessageBox.Show("Impossible to open selected file",
  56.                         "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.  
  62.         private void button4_Click(object sender, EventArgs e)
  63.         {
  64.             try
  65.             {
  66.                 int per = Int32.Parse(textBox1.Text);
  67.  
  68.                 Image myBitmap = pictureBox1.Image;
  69.                 Size nSize = new Size(pictureBox1.Image.Width * per / 100, pictureBox1.Image.Height * per / 100);
  70.                 Image gdi = new Bitmap(myBitmap, nSize);
  71.  
  72.                 pictureBox1.Image = gdi;
  73.  
  74.             }
  75.             catch (Exception er)
  76.             {
  77.                 MessageBox.Show(er.Message);
  78.             }
  79.         }
  80.  
  81.         private void button3_Click(object sender, EventArgs e)
  82.         {
  83.             button5.Visible = true;
  84.         }
  85.  
  86.         private void textBox1_TextChanged(object sender, EventArgs e)
  87.         {
  88.         }
  89.  
  90.         private void button2_Click(object sender, EventArgs e)
  91.         {
  92.             try
  93.             {
  94.                 int x = Int32.Parse(textBox2.Text);
  95.                 int y = Int32.Parse(textBox3.Text);
  96.                 int width = Int32.Parse(textBox4.Text);
  97.                 int height = Int32.Parse(textBox5.Text);
  98.                 Image image = pictureBox1.Image;
  99.  
  100.                 Rectangle Rect = new Rectangle(x, y, width - x, height - y);
  101.                 Bitmap newb = new Bitmap(Rect.Width, Rect.Height);
  102.  
  103.                 using (var g = System.Drawing.Graphics.FromImage(newb))
  104.                     g.DrawImage(image, 0, 0, Rect, GraphicsUnit.Pixel);
  105.  
  106.                 pictureBox1.Image = newb;
  107.             }
  108.             catch (Exception er)
  109.             {
  110.                 MessageBox.Show(er.Message);
  111.             }
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement