Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- public static byte[] converterDemo(Image x)
- {
- ImageConverter _imageConverter = new ImageConverter();
- byte[] xByte = (byte[])_imageConverter.ConvertTo(x, typeof(byte[]));
- return xByte;
- }
- private void byteArrayToImage(byte[] byteArrayIn)
- {
- Image x = (Bitmap)((new ImageConverter()).ConvertFrom(byteArrayIn));
- pictureBox2.Image = x;
- }
- public static byte Middle(byte[] mas)
- {
- if (mas[0] > mas[1] && mas[0] < mas[2] || mas[0] < mas[1] && mas[0] > mas[2])
- {
- return mas[0];
- }else
- if (mas[1] > mas[0] && mas[1] < mas[2] || mas[1] < mas[0] && mas[1] > mas[2])
- {
- return mas[1];
- }else
- {
- return mas[2];
- }
- }
- public byte[] Mediana(byte[] arr)
- {
- byte[] three = new byte[3];
- byte[] dst = new byte[arr.Length];
- for (int i = 0; i < (arr.Length-2); i++)
- {
- three[0] = arr[i];
- three[1] = arr[i + 1];
- three[2] = arr[i + 2];
- dst[i+1] = Middle(three);
- }
- dst[0] = arr[0];
- dst[arr.Length - 1] = arr[arr.Length - 2];
- return dst;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Filter = "Image Files(*.JPG;*JPEG;*GIF;*PNG)|*.JPG;*JPEG;*GIF;*PNG|All files(*.*)|*.*";
- if (ofd.ShowDialog() == DialogResult.OK)
- {
- try
- {
- pictureBox1.Image = new Bitmap(ofd.FileName);
- byte[] source = converterDemo(pictureBox1.Image);
- byte[] res = Mediana(source);
- byteArrayToImage(res);
- }
- catch
- {
- MessageBox.Show("Невозможно открыть файл");
- }
- }
- }
- }
- Bitmap bmp = ...;
- Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
- BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
- // получаем адрес первой строки
- IntPtr ptr = bmpData.Scan0;
- // Заводим байтовый массив нужной блины
- int nbytes = Math.Abs(bmpData.Stride) * bmp.Height;
- byte[] bytes = new byte[nbytes];
- // копируем значения в массив
- System.Runtime.InteropServices.Marshal.Copy(ptr, bytes, 0, nbytes);
- // --> тут обработка данных <--
- // i-ая строка начинается по индексу i * bmpData.Stride
- // длина в байтах одного пикселя зависит от вашего формата
- // чаще всего у вас будет 24 или 32 бита на пиксель, то есть 3 или 4 байта
- // копируем данные назад
- System.Runtime.InteropServices.Marshal.Copy(bytes, 0, ptr, nbytes);
- // освобождаем данные
- bmp.UnlockBits(bmpData);
Add Comment
Please, Sign In to add comment