Guest User

Untitled

a guest
Mar 13th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. public partial class Form1 : Form
  2. {
  3. public Form1()
  4. {
  5. InitializeComponent();
  6.  
  7.  
  8. }
  9. public static byte[] converterDemo(Image x)
  10. {
  11. ImageConverter _imageConverter = new ImageConverter();
  12. byte[] xByte = (byte[])_imageConverter.ConvertTo(x, typeof(byte[]));
  13. return xByte;
  14. }
  15. private void byteArrayToImage(byte[] byteArrayIn)
  16. {
  17. Image x = (Bitmap)((new ImageConverter()).ConvertFrom(byteArrayIn));
  18. pictureBox2.Image = x;
  19. }
  20. public static byte Middle(byte[] mas)
  21. {
  22.  
  23. if (mas[0] > mas[1] && mas[0] < mas[2] || mas[0] < mas[1] && mas[0] > mas[2])
  24. {
  25. return mas[0];
  26. }else
  27. if (mas[1] > mas[0] && mas[1] < mas[2] || mas[1] < mas[0] && mas[1] > mas[2])
  28. {
  29. return mas[1];
  30. }else
  31. {
  32. return mas[2];
  33. }
  34. }
  35. public byte[] Mediana(byte[] arr)
  36. {
  37. byte[] three = new byte[3];
  38. byte[] dst = new byte[arr.Length];
  39.  
  40. for (int i = 0; i < (arr.Length-2); i++)
  41. {
  42.  
  43. three[0] = arr[i];
  44. three[1] = arr[i + 1];
  45. three[2] = arr[i + 2];
  46. dst[i+1] = Middle(three);
  47. }
  48. dst[0] = arr[0];
  49. dst[arr.Length - 1] = arr[arr.Length - 2];
  50. return dst;
  51. }
  52. private void button1_Click(object sender, EventArgs e)
  53. {
  54. OpenFileDialog ofd = new OpenFileDialog();
  55. ofd.Filter = "Image Files(*.JPG;*JPEG;*GIF;*PNG)|*.JPG;*JPEG;*GIF;*PNG|All files(*.*)|*.*";
  56. if (ofd.ShowDialog() == DialogResult.OK)
  57. {
  58. try
  59. {
  60. pictureBox1.Image = new Bitmap(ofd.FileName);
  61. byte[] source = converterDemo(pictureBox1.Image);
  62. byte[] res = Mediana(source);
  63. byteArrayToImage(res);
  64. }
  65. catch
  66. {
  67. MessageBox.Show("Невозможно открыть файл");
  68. }
  69. }
  70. }
  71.  
  72.  
  73. }
  74.  
  75. Bitmap bmp = ...;
  76.  
  77. Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
  78. BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
  79.  
  80. // получаем адрес первой строки
  81. IntPtr ptr = bmpData.Scan0;
  82.  
  83. // Заводим байтовый массив нужной блины
  84. int nbytes = Math.Abs(bmpData.Stride) * bmp.Height;
  85. byte[] bytes = new byte[nbytes];
  86.  
  87. // копируем значения в массив
  88. System.Runtime.InteropServices.Marshal.Copy(ptr, bytes, 0, nbytes);
  89.  
  90. // --> тут обработка данных <--
  91. // i-ая строка начинается по индексу i * bmpData.Stride
  92. // длина в байтах одного пикселя зависит от вашего формата
  93. // чаще всего у вас будет 24 или 32 бита на пиксель, то есть 3 или 4 байта
  94.  
  95. // копируем данные назад
  96. System.Runtime.InteropServices.Marshal.Copy(bytes, 0, ptr, nbytes);
  97.  
  98. // освобождаем данные
  99. bmp.UnlockBits(bmpData);
Add Comment
Please, Sign In to add comment