c# 骰子作画实现 图片转字符

效果图
c# 骰子作画实现 图片转字符_第1张图片
放大局部
这里写图片描述

参考算法链接:骰子作画的算法

话不多说上代码

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Image ResourceImage;
        ListmyImgList;
        static int cc = 8;
        private void button1_Click(object sender, EventArgs e)
        {
            Bitmap GrayImage = ToGray((Bitmap)ResourceImage);
            pictureBox1.Image = (Image)GrayImage;
            ResourceImage =  (Image)GrayImage;


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            myImgList = new List();
            ResourceImage = Image.FromFile("C:\\xxx.jpg");
            pictureBox1.Image = ResourceImage;
            for (int i = 1; i <= 6; i++)
            {
                Bitmap image = new Bitmap(Image.FromFile("C:\\" + i.ToString() + ".jpg"));
                Bitmap image1 = new Bitmap(cc, cc);
                Graphics g = Graphics.FromImage(image1);
                Rectangle rect = new Rectangle(0, 0, cc, cc);
                g.DrawImage(image, rect);
                myImgList.Add(image1);
            }
        }
        public static Bitmap ToGray(Bitmap bmp)
        {

            for (int i = 0; i < bmp.Width; i++)
            {
                for (int j = 0; j < bmp.Height; j++)
                {
                    //获取该点的像素的RGB的颜色
                    Color color = bmp.GetPixel(i, j);
                    //利用公式计算灰度值
                    int gray = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);

                    Color newColor = Color.FromArgb(gray, gray, gray);
                    bmp.SetPixel(i, j, newColor);
                }
            }
            return bmp;
        }

        public static Bitmap PJ16(Bitmap bmp, List myImgList, PictureBox pictureBox2)
        {

            for (int i = 0; i < bmp.Width / cc; i++)
            {
                for (int j = 0; j < bmp.Height / cc; j++)
                {
                     int sum=0;
                     for (int k = 0; k < cc; k++)
                    {
                        for (int l = 0; l < cc; l++)
                        {
                            Color color = bmp.GetPixel(i * cc + k, j * cc + l);
                            sum += color.R;
                        }


                    }
                    int x = getTage(sum / 256);
                    Graphics g = Graphics.FromImage(bmp);
                    //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
                    g.DrawImage(myImgList[x], i * cc, j * cc, (int)cc, ((int)cc));
                    GC.Collect();




                }
            }
            return bmp;
        }

        private static int getTage(int x)
        {
            if (0 <= x && x <= 10)
                return 0;
            if (10 <= x && x <= 20)
                return 2;
            if (20 <= x && x <= 30)
                return 3;
            if (30 <= x && x <= 41)
                return 4;
        if (41 < x && x <= 83)
                return 5;
        if (83 < x && x <= 124)
             return 5;
        if (124 < x && x <= 165) 
                return 5;
        if (165 < x && x <= 206)
                return 5;
        if (206 < x && x <= 255) 
                return 5;
           return 999;

        }


        private void button2_Click(object sender, EventArgs e)
        {
            ResourceImage = PJ16((Bitmap)ResourceImage, myImgList, pictureBox2);
           pictureBox2.Image = (Image)ResourceImage;
        }



    }

你可能感兴趣的:(C#,数据结构与算法,图像处理)