天天看點

C#做了個相棋盤的圖檔

C#做了個相棋盤的圖檔

//好長時間沒有寫部落格了最近用C#做了個相棋盤的圖檔

//是因為前一段時間去一個公司面試他給我了一段代碼讓我給解釋一下

//當時我隻知道是一個圖檔的初始化

//他給我解釋說是先給圖檔初始化後又在圖檔上加了幾個字

//回來了就看了看圖檔這方面的資料就選生成了有字的圖檔

//後來又在網上看了個驗證碼的東東回來就做了做

//昨天沒事就又想起做這個下面是代碼

//

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace WindowsApplication2

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            Print();

        }

        private void Print()

        {

            Bitmap bit = new Bitmap(400, 520);

            Graphics g = Graphics.FromImage(bit);

            g.FillRectangle(new SolidBrush(Color.OrangeRed), new Rectangle(0, 0, 400, 520));

            Pen pen = new Pen(new SolidBrush(Color.Bisque),2);

            //行

            for (int i = 1; i < 13;i++)           

                g.DrawLine(pen, new Point(40, 40*i), new Point(360, 40*i));

            //上列

            for (int i = 1; i < 10; i++)           

                g.DrawLine(pen, new Point(40 * i, 40), new Point(40 * i,240));

            //下列

            for (int i = 1; i < 10;i+=8 )

                g.DrawLine(pen, new Point(40 * i, 40), new Point(40 * i, 480));

            //列頭列尾

            for (int i = 2; i < 9; i++)           

                g.DrawLine(pen, new Point(40 * i, 280), new Point(40 * i, 480));

            //交叉

            int[] num1 ={ 4, 6, 6, 4, 4, 6, 6, 4 };

            int[] num2 ={ 1, 3, 1, 3, 10, 12, 10, 12 };

            for (int i = 0; i < 8; i+=2)

                g.DrawLine(pen,new Point(40*num1[i],40*num2[i]),new Point(40*num1[i+1],40*num2[i+1]));

            //添加字到圖檔上

            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            g.DrawString("楚河    漢界", new Font(FontFamily.GenericSerif, 40, FontStyle.Regular, GraphicsUnit.Pixel), new SolidBrush(Color.Bisque),new PointF(80,240));

            pictureBox1.Image = bit;

        }

    }

繼續閱讀