Advertisement
Ang377ou

browseimage

Mar 23rd, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.49 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.Drawing.Imaging;
  7. using System.Drawing.Drawing2D;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Windows.Forms;
  11. using AForge.Imaging.Filters;
  12. using AForge.Imaging;
  13. using AForge;
  14.  
  15. namespace browseimage
  16. {
  17. public partial class Form1 : Form
  18. {
  19. public Form1()
  20. {
  21. InitializeComponent();
  22. }
  23. InterpolationMode interp = InterpolationMode.Default;
  24. System.Drawing.Image loadedimg;
  25. private void button1_Click(object sender, EventArgs e)
  26. {
  27. OpenFileDialog file = new OpenFileDialog()
  28. {
  29. Filter = "Image Files(*.jpg)|*.jpg|Bitmaps (*.bmp)|*.bmp"
  30.  
  31. };
  32. if (file.ShowDialog() == DialogResult.OK)
  33. {
  34. loadedimg = System.Drawing.Image.FromFile(file.FileName);
  35. pictureBox1.Image = System.Drawing.Image.FromFile(file.FileName, true);
  36. pictureBox2.Image = System.Drawing.Image.FromFile(file.FileName, true);
  37. }
  38. }
  39.  
  40. private void button3_Click(object sender, EventArgs e)
  41. {
  42. pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
  43. pictureBox1.Refresh();
  44. }
  45.  
  46. private void button2_Click(object sender, EventArgs e)
  47. {
  48. pictureBox1.Image.RotateFlip(RotateFlipType.Rotate270FlipNone);
  49. pictureBox1.Refresh();
  50. }
  51.  
  52. private void radioButton1_CheckedChanged(object sender, EventArgs e)
  53. {
  54. tbHeight.Enabled = tbWidth.Enabled = radioButton1.Checked;
  55. }
  56.  
  57. private void radioButton2_CheckedChanged(object sender, EventArgs e)
  58. {
  59. textBox3.Enabled = radioButton2.Checked;
  60. }
  61.  
  62. private void button4_Click(object sender, EventArgs e)
  63. {
  64. System.Drawing.Image original = loadedimg;
  65. int newWidth = 0, newHeight = 0;
  66.  
  67. if (radioButton2.Checked)
  68. {
  69. float percent = float.Parse(textBox3.Text.Replace("%", null)) / (float)100;
  70.  
  71.  
  72. // 200 by 200
  73. newWidth = (int)(original.Width * percent); // 200 * 1.5
  74. newHeight = (int)(original.Height * percent); // 200 * 1.5
  75. }
  76.  
  77. if (radioButton1.Checked)
  78. {
  79. newWidth = int.Parse(tbWidth.Text);
  80. newHeight = int.Parse(tbHeight.Text);
  81. }
  82.  
  83. System.Drawing.Image resized = Image_Resize.Resize(original, new Size(newWidth, newHeight),
  84. interp, radioButton2.Checked);
  85.  
  86. //new Form2(resized).ShowDialog();
  87. pictureBox1.Image = resized;
  88. }
  89.  
  90. private void button5_Click(object sender, EventArgs e)
  91. {
  92. pictureBox1.Image = pictureBox2.Image;
  93. }
  94.  
  95. private void Form1_Load(object sender, EventArgs e)
  96. {
  97. txtbinarize.Hide();
  98. }
  99.  
  100. private void btngrayscale_Click(object sender, EventArgs e)
  101. {
  102. pictureBox1.Image = MakeGrayscale3((Bitmap)pictureBox1.Image);
  103. }
  104. public Bitmap MakeGrayscale3(Bitmap original)
  105. {
  106. //create a blank bitmap the same size as original
  107. Bitmap newBitmap = new Bitmap(original.Width, original.Height);
  108.  
  109. //get a graphics object from the new image
  110. Graphics g = Graphics.FromImage(newBitmap);
  111.  
  112. //create the grayscale ColorMatrix
  113. ColorMatrix colorMatrix = new ColorMatrix(
  114. new float[][]
  115. {
  116. new float[] {.3f, .3f, .3f, 0, 0},
  117. new float[] {.59f, .59f, .59f, 0, 0},
  118. new float[] {.11f, .11f, .11f, 0, 0},
  119. new float[] {0, 0, 0, 1, 0},
  120. new float[] {0, 0, 0, 0, 1}
  121. });
  122.  
  123. //create some image attributes
  124. ImageAttributes attributes = new ImageAttributes();
  125.  
  126. //set the color matrix attribute
  127. attributes.SetColorMatrix(colorMatrix);
  128.  
  129. //draw the original image on the new image
  130. //using the grayscale color matrix
  131. g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
  132. 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
  133.  
  134. //dispose the Graphics object
  135. g.Dispose();
  136. return newBitmap;
  137. }
  138.  
  139. private void btnblackwhite_Click(object sender, EventArgs e)
  140. {
  141. pictureBox1.Image = BlackandWhite((Bitmap)pictureBox1.Image);
  142. }
  143. public Bitmap BlackandWhite(Bitmap original)
  144. {
  145. //Bitmap bm = new Bitmap(source.Width, source.Height);
  146. //for (int y = 0; y < bm.Height; y++)
  147. //{
  148. // for (int x = 0; x < bm.Width; x++)
  149. // {
  150. // Color c = source.GetPixel(x, y);
  151. // int luma = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
  152. // bm.SetPixel(x, y, luma > 127 ? Color.White : Color.Black);
  153. // }
  154. //}
  155. //return bm;
  156. //create a blank bitmap the same size as original
  157. Bitmap newBitmap = new Bitmap(original.Width, original.Height);
  158.  
  159. //get a graphics object from the new image
  160. Graphics g = Graphics.FromImage(newBitmap);
  161.  
  162. //create the grayscale ColorMatrix
  163. ColorMatrix colorMatrix = new ColorMatrix(
  164. new float[][]
  165. {
  166. new float[] {1.5f, 1.5f, 1.5f, 0, 0},
  167. new float[] {1.5f, 1.5f, 1.5f, 0, 0},
  168. new float[] {1.5f, 1.5f, 1.5f, 0, 0},
  169. new float[] {0, 0, 0, 1, 0},
  170. new float[] {-1, -1, -1, 0, 1}
  171. });
  172.  
  173. //create some image attributes
  174. ImageAttributes attributes = new ImageAttributes();
  175.  
  176. //set the color matrix attribute
  177. attributes.SetColorMatrix(colorMatrix);
  178.  
  179. //draw the original image on the new image
  180. //using the grayscale color matrix
  181. g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
  182. 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
  183.  
  184. //dispose the Graphics object
  185. g.Dispose();
  186. return newBitmap;
  187. }
  188.  
  189. private void btnbinaries_Click(object sender, EventArgs e)
  190. {
  191.  
  192. txtbinarize.Text = Binarize((Bitmap)pictureBox1.Image);
  193. txtbinarize.Show();
  194. }
  195. public string Binarize(Bitmap origPic)
  196. {
  197. string binary = "";
  198. Bitmap orig = origPic;
  199. Bitmap img = origPic;
  200.  
  201. for (int i = 0; i < img.Height; i++)
  202. {
  203. for (int j = 0; j < img.Width; j++)
  204. {
  205. //richTextBox1.Font = new Font(Font.Name, 10, Font.Style);
  206. 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))
  207. {
  208. binary = binary + "1";
  209.  
  210. }
  211. else
  212. {
  213. binary = binary + "0";
  214. }
  215. }
  216. binary = binary + "\r\n";
  217. }
  218. txtbinarize.Size = new Size(orig.Width * 5, orig.Height * 5);
  219. txtbinarize.Font = new Font("Lucida Console", 3);
  220. return binary;
  221. }
  222.  
  223. private void btnhistogram_Click(object sender, EventArgs e)
  224. {
  225. histogram HISTOGRAM = new histogram();
  226. HISTOGRAM.Show();
  227. }
  228.  
  229. private void btnedge_Click(object sender, EventArgs e)
  230. {
  231. SobelEdgeDetector qwe = new SobelEdgeDetector();
  232. pictureBox1.Image = qwe.Apply(Grayscale.CommonAlgorithms.BT709.Apply((Bitmap)pictureBox2.Image));
  233. }
  234.  
  235. private void btncorner_Click(object sender, EventArgs e)
  236. {
  237. SusanCornersDetector s = new SusanCornersDetector();
  238. List<IntPoint> c = s.ProcessImage((Bitmap)pictureBox1.Image);
  239. Graphics g = Graphics.FromImage(pictureBox1.Image);
  240. SolidBrush b = new SolidBrush(Color.Red);
  241. Pen p = new Pen(b);
  242.  
  243. foreach (IntPoint k in c)
  244. {
  245. g.DrawRectangle(p, k.X - 1, k.Y - 1, 3, 3);
  246. }
  247. pictureBox1.Image = (Bitmap)pictureBox1.Image;
  248. }
  249.  
  250. private void txtbinarize_TextChanged(object sender, EventArgs e)
  251. {
  252.  
  253. }
  254.  
  255. private void btnpaint_Click(object sender, EventArgs e)
  256. {
  257. draw dr = new draw();
  258. dr.Show();
  259. }
  260. }
  261. }
  262. resize class
  263. using System;
  264. using System.Collections.Generic;
  265. using System.Linq;
  266. using System.Text;
  267. using System.Drawing;
  268. using System.Drawing.Drawing2D;
  269.  
  270. namespace browseimage
  271. {
  272. class Image_Resize
  273. {
  274. public static Image Resize(Image image, Size size, InterpolationMode interp, bool preserveAspectRatio)
  275. {
  276. int newWidth, newHeight;
  277.  
  278. if (preserveAspectRatio)
  279. {
  280. int origWidth = image.Width;
  281. int origHeight = image.Height;
  282.  
  283. float percentWidth = (float)size.Width / (float)origWidth;
  284. float percentHeight = (float)size.Height / (float)origHeight;
  285. float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
  286.  
  287. newWidth = (int)(origWidth * percent);
  288. newHeight = (int)(origHeight * percent);
  289. }
  290. else
  291. {
  292. newHeight = size.Height;
  293. newWidth = size.Width;
  294. }
  295.  
  296. Image newImg = new Bitmap(newWidth, newHeight);
  297. using (Graphics gfx = Graphics.FromImage(newImg))
  298. {
  299. gfx.InterpolationMode = interp;
  300. gfx.DrawImage(image, 0, 0, newWidth, newHeight);
  301. }
  302. //Graphics gfx = Graphics.FromImage(newImg);
  303. //gfx.InterpolationMode = interp;
  304. //gfx.DrawImage(image, 0, 0, newWidth, newHeight);
  305. //gfx.Dispose();
  306.  
  307. return newImg;
  308. }
  309. }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement