选取图像一部分,并得到部分图像的像素数组。

 List<Point> list = new List<Point>();

private void button2_Click(object sender, EventArgs e)
        {
            List<byte[]> pointbytes = new List<byte[]>();
            // OpenFileDialog dialog = new OpenFileDialog();
            //dialog.Filter = "bmp文件(*.bmp)|*.bmp";
            //if(dialog.ShowDialog() == DialogResult.OK)
            //{
            string file = "C:\\Users\\86166\\Desktop\\11.bmp";
            string bmpname = System.IO.Path.GetFileNameWithoutExtension(file);
            string writename = "File\\" + bmpname + ".psaf";
            if (Directory.Exists("File\\" + bmpname + ".psaf"))
            {
                Directory.Delete("File\\" + bmpname + ".psaf");
            }
            Directory.CreateDirectory("File\\" + bmpname + "\\PSAF\\");
            string save_file = "File\\" + bmpname + "\\";
            Bitmap bmp = new Bitmap(file);
            byte[] addarraybyte = new byte[8820];
            for (int i = 0; i < list.Count; i++)
            {
                pointbytes.Add(convert(cut(bmp, (int)list[i].X, (int)list[i].Y)));
                Array.Copy(pointbytes[i], 0, addarraybyte, 147 * i, pointbytes[i].Length);
            }
       
        }

private static byte[] cut(Bitmap image, int rex, int rey)
        {
            Rectangle region = new Rectangle(rex, rey, 7, 7);
            byte[] bytes = new byte[7 * 7 * 3];
            int index = 0;
            for (int y = region.Y; y < region.Y + region.Height; y++)
            {
                for (int x = region.X; x < region.X + region.Width; x++)
                {
                    // 获取当前像素的颜色
                    Color pixelColor = image.GetPixel(x, y);
                    //按BGR的顺序存放到数组中
                    bytes[index] = pixelColor.B; index += 1;
                    bytes[index] = pixelColor.G; index += 1;
                    bytes[index] = pixelColor.R; index += 1;
                }
            }
            return bytes;
        }

//将数组bytes转为s型输出数组byteArray

private static byte[] cut(Bitmap image, int rex, int rey)
        {
            Rectangle region = new Rectangle(rex, rey, 7, 7);
            byte[] bytes = new byte[7 * 7 * 3];
            int index = 0;
            for (int y = region.Y; y < region.Y + region.Height; y++)
            {
                for (int x = region.X; x < region.X + region.Width; x++)
                {
                    // 获取当前像素的颜色
                    Color pixelColor = image.GetPixel(x, y);
                    //按BGR的顺序存放到数组中
                    bytes[index] = pixelColor.B; index += 1;
                    bytes[index] = pixelColor.G; index += 1;
                    bytes[index] = pixelColor.R; index += 1;
                }
            }
            return bytes;
        }
        private byte[] convert(byte[] byteArray)
        {
            List<List<List<byte>>> list = new List<List<List<byte>>>();
            int index = 0;
            // byte[,,] option = new byte[bmp.Height, bmp.Width, 3];
            for (int i = 0; i < 7; i++)
            {
                list.Add(new List<List<byte>>());
                for (int j = 0; j < 7; j++)
                {
                    list[i].Add(new List<byte>());
                    for (int k = 0; k < 3; k++)
                    {
                        list[i][j].Add(byteArray[index++]);
                    }
                    list[i][j].Reverse();
                }
            }
            for (int m = 0; m < 7; m++)
            {
                if (m % 2 != 0)
                {
                    list[m].Reverse();
                }

            }
            int ind = 0;
            for (int i = 0; i < list.Count; i++)
            {
                for (int j = 0; j < list[i].Count; j++)
                {
                    for (int k = 0; k < list[i][j].Count; k++)
                    {
                        byteArray[ind++] = list[i][j][k];
                    }
                }
            }
            return byteArray;
        }

private static byte[] cut(Bitmap image, int rex, int rey)
        {
            Rectangle region = new Rectangle(rex, rey, 7, 7);
            byte[] bytes = new byte[7 * 7 * 3];
            int index = 0;
            for (int y = region.Y; y < region.Y + region.Height; y++)
            {
                for (int x = region.X; x < region.X + region.Width; x++)
                {
                    // 获取当前像素的颜色
                    Color pixelColor = image.GetPixel(x, y);
                    //按BGR的顺序存放到数组中
                    bytes[index] = pixelColor.B; index += 1;
                    bytes[index] = pixelColor.G; index += 1;
                    bytes[index] = pixelColor.R; index += 1;
                }
            }
            return bytes;
        }
        private byte[] convert(byte[] byteArray)
        {
            List<List<List<byte>>> list = new List<List<List<byte>>>();
            int index = 0;
            // byte[,,] option = new byte[bmp.Height, bmp.Width, 3];
            for (int i = 0; i < 7; i++)
            {
                list.Add(new List<List<byte>>());
                for (int j = 0; j < 7; j++)
                {
                    list[i].Add(new List<byte>());
                    for (int k = 0; k < 3; k++)
                    {
                        list[i][j].Add(byteArray[index++]);
                    }
                    list[i][j].Reverse();
                }
            }
            for (int m = 0; m < 7; m++)
            {
                if (m % 2 != 0)
                {
                    list[m].Reverse();
                }

            }
            int ind = 0;
            for (int i = 0; i < list.Count; i++)
            {
                for (int j = 0; j < list[i].Count; j++)
                {
                    for (int k = 0; k < list[i][j].Count; k++)
                    {
                        byteArray[ind++] = list[i][j][k];
                    }
                }
            }
            return byteArray;
        }

private byte[] convert(byte[] byteArray)
        {
            List<List<List<byte>>> list = new List<List<List<byte>>>();
            int index = 0;
            // byte[,,] option = new byte[bmp.Height, bmp.Width, 3];
            for (int i = 0; i < 7; i++)
            {
                list.Add(new List<List<byte>>());
                for (int j = 0; j < 7; j++)
                {
                    list[i].Add(new List<byte>());
                    for (int k = 0; k < 3; k++)
                    {
                        list[i][j].Add(byteArray[index++]);
                    }
                    list[i][j].Reverse();
                }
            }
            for (int m = 0; m < 7; m++)
            {
                if (m % 2 != 0)
                {
                    list[m].Reverse();
                }

            }
            int ind = 0;
            for (int i = 0; i < list.Count; i++)
            {
                for (int j = 0; j < list[i].Count; j++)
                {
                    for (int k = 0; k < list[i][j].Count; k++)
                    {
                        byteArray[ind++] = list[i][j][k];
                    }
                }
            }
            return byteArray;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值