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.Drawing.Imaging;
- using System.Drawing.Drawing2D;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using AForge.Imaging.Filters;
- using AForge.Imaging;
- using AForge;
- namespace browseimage
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- InterpolationMode interp = InterpolationMode.Default;
- System.Drawing.Image loadedimg;
- private void button1_Click(object sender, EventArgs e)
- {
- OpenFileDialog file = new OpenFileDialog()
- {
- Filter = "Image Files(*.jpg)|*.jpg|Bitmaps (*.bmp)|*.bmp"
- };
- if (file.ShowDialog() == DialogResult.OK)
- {
- loadedimg = System.Drawing.Image.FromFile(file.FileName);
- pictureBox1.Image = System.Drawing.Image.FromFile(file.FileName, true);
- pictureBox2.Image = System.Drawing.Image.FromFile(file.FileName, true);
- }
- }
- private void button3_Click(object sender, EventArgs e)
- {
- pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
- pictureBox1.Refresh();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- pictureBox1.Image.RotateFlip(RotateFlipType.Rotate270FlipNone);
- pictureBox1.Refresh();
- }
- private void radioButton1_CheckedChanged(object sender, EventArgs e)
- {
- tbHeight.Enabled = tbWidth.Enabled = radioButton1.Checked;
- }
- private void radioButton2_CheckedChanged(object sender, EventArgs e)
- {
- textBox3.Enabled = radioButton2.Checked;
- }
- private void button4_Click(object sender, EventArgs e)
- {
- System.Drawing.Image original = loadedimg;
- int newWidth = 0, newHeight = 0;
- if (radioButton2.Checked)
- {
- float percent = float.Parse(textBox3.Text.Replace("%", null)) / (float)100;
- // 200 by 200
- newWidth = (int)(original.Width * percent); // 200 * 1.5
- newHeight = (int)(original.Height * percent); // 200 * 1.5
- }
- if (radioButton1.Checked)
- {
- newWidth = int.Parse(tbWidth.Text);
- newHeight = int.Parse(tbHeight.Text);
- }
- System.Drawing.Image resized = Image_Resize.Resize(original, new Size(newWidth, newHeight),
- interp, radioButton2.Checked);
- //new Form2(resized).ShowDialog();
- pictureBox1.Image = resized;
- }
- private void button5_Click(object sender, EventArgs e)
- {
- pictureBox1.Image = pictureBox2.Image;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- txtbinarize.Hide();
- }
- private void btngrayscale_Click(object sender, EventArgs e)
- {
- pictureBox1.Image = MakeGrayscale3((Bitmap)pictureBox1.Image);
- }
- public Bitmap MakeGrayscale3(Bitmap original)
- {
- //create a blank bitmap the same size as original
- Bitmap newBitmap = new Bitmap(original.Width, original.Height);
- //get a graphics object from the new image
- Graphics g = Graphics.FromImage(newBitmap);
- //create the grayscale ColorMatrix
- ColorMatrix colorMatrix = new ColorMatrix(
- new float[][]
- {
- new float[] {.3f, .3f, .3f, 0, 0},
- new float[] {.59f, .59f, .59f, 0, 0},
- new float[] {.11f, .11f, .11f, 0, 0},
- new float[] {0, 0, 0, 1, 0},
- new float[] {0, 0, 0, 0, 1}
- });
- //create some image attributes
- ImageAttributes attributes = new ImageAttributes();
- //set the color matrix attribute
- attributes.SetColorMatrix(colorMatrix);
- //draw the original image on the new image
- //using the grayscale color matrix
- g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
- 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
- //dispose the Graphics object
- g.Dispose();
- return newBitmap;
- }
- private void btnblackwhite_Click(object sender, EventArgs e)
- {
- pictureBox1.Image = BlackandWhite((Bitmap)pictureBox1.Image);
- }
- public Bitmap BlackandWhite(Bitmap original)
- {
- //Bitmap bm = new Bitmap(source.Width, source.Height);
- //for (int y = 0; y < bm.Height; y++)
- //{
- // for (int x = 0; x < bm.Width; x++)
- // {
- // Color c = source.GetPixel(x, y);
- // int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
- // bm.SetPixel(x, y, luma > 127 ? Color.White : Color.Black);
- // }
- //}
- //return bm;
- //create a blank bitmap the same size as original
- Bitmap newBitmap = new Bitmap(original.Width, original.Height);
- //get a graphics object from the new image
- Graphics g = Graphics.FromImage(newBitmap);
- //create the grayscale ColorMatrix
- ColorMatrix colorMatrix = new ColorMatrix(
- new float[][]
- {
- new float[] {1.5f, 1.5f, 1.5f, 0, 0},
- new float[] {1.5f, 1.5f, 1.5f, 0, 0},
- new float[] {1.5f, 1.5f, 1.5f, 0, 0},
- new float[] {0, 0, 0, 1, 0},
- new float[] {-1, -1, -1, 0, 1}
- });
- //create some image attributes
- ImageAttributes attributes = new ImageAttributes();
- //set the color matrix attribute
- attributes.SetColorMatrix(colorMatrix);
- //draw the original image on the new image
- //using the grayscale color matrix
- g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
- 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
- //dispose the Graphics object
- g.Dispose();
- return newBitmap;
- }
- private void btnbinaries_Click(object sender, EventArgs e)
- {
- txtbinarize.Text = Binarize((Bitmap)pictureBox1.Image);
- txtbinarize.Show();
- }
- public string Binarize(Bitmap origPic)
- {
- string binary = "";
- Bitmap orig = origPic;
- Bitmap img = origPic;
- for (int i = 0; i < img.Height; i++)
- {
- for (int j = 0; j < img.Width; j++)
- {
- //richTextBox1.Font = new Font(Font.Name, 10, Font.Style);
- if ((img.GetPixel(j, i).A == 255 && img.GetPixel(j, i).B == 255 && img.GetPixel(j, i).G == 255 && img.GetPixel(j, i).R == 255))
- {
- binary = binary + "1";
- }
- else
- {
- binary = binary + "0";
- }
- }
- binary = binary + "\r\n";
- }
- txtbinarize.Size = new Size(orig.Width * 5, orig.Height * 5);
- txtbinarize.Font = new Font("Lucida Console", 3);
- return binary;
- }
- private void btnhistogram_Click(object sender, EventArgs e)
- {
- histogram HISTOGRAM = new histogram();
- HISTOGRAM.Show();
- }
- private void btnedge_Click(object sender, EventArgs e)
- {
- SobelEdgeDetector qwe = new SobelEdgeDetector();
- pictureBox1.Image = qwe.Apply(Grayscale.CommonAlgorithms.BT709.Apply((Bitmap)pictureBox2.Image));
- }
- private void btncorner_Click(object sender, EventArgs e)
- {
- SusanCornersDetector s = new SusanCornersDetector();
- List<IntPoint> c = s.ProcessImage((Bitmap)pictureBox1.Image);
- Graphics g = Graphics.FromImage(pictureBox1.Image);
- SolidBrush b = new SolidBrush(Color.Red);
- Pen p = new Pen(b);
- foreach (IntPoint k in c)
- {
- g.DrawRectangle(p, k.X - 1, k.Y - 1, 3, 3);
- }
- pictureBox1.Image = (Bitmap)pictureBox1.Image;
- }
- private void txtbinarize_TextChanged(object sender, EventArgs e)
- {
- }
- private void btnpaint_Click(object sender, EventArgs e)
- {
- draw dr = new draw();
- dr.Show();
- }
- }
- }
- resize class
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- namespace browseimage
- {
- class Image_Resize
- {
- public static Image Resize(Image image, Size size, InterpolationMode interp, bool preserveAspectRatio)
- {
- int newWidth, newHeight;
- if (preserveAspectRatio)
- {
- int origWidth = image.Width;
- int origHeight = image.Height;
- float percentWidth = (float)size.Width / (float)origWidth;
- float percentHeight = (float)size.Height / (float)origHeight;
- float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
- newWidth = (int)(origWidth * percent);
- newHeight = (int)(origHeight * percent);
- }
- else
- {
- newHeight = size.Height;
- newWidth = size.Width;
- }
- Image newImg = new Bitmap(newWidth, newHeight);
- using (Graphics gfx = Graphics.FromImage(newImg))
- {
- gfx.InterpolationMode = interp;
- gfx.DrawImage(image, 0, 0, newWidth, newHeight);
- }
- //Graphics gfx = Graphics.FromImage(newImg);
- //gfx.InterpolationMode = interp;
- //gfx.DrawImage(image, 0, 0, newWidth, newHeight);
- //gfx.Dispose();
- return newImg;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement