标签:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; //WinForm中需要一个picturebox控件 namespace emulateLCD { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { graphics = this.pictureBox1.CreateGraphics(); bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height); int i, j; for (i = 0; i < 64; i++) { for (j = 0; j < 16; j++) { data[i, j] = 0x00; } } } private Bitmap bmp; private Graphics graphics; private byte[,] data = new byte[64, 16];//12864图形数据数组 byte[] tab = { 0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40, 0x00,0x00,0x00,0x00,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0x00,0x40, 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0x00,0x40, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x14,0x00,0x00,0x00,0x01,0xC2,0x40, 0x00,0x00,0x00,0x06,0x30,0x01,0xE0,0x00,0x00,0x2A,0x00,0x00,0xD8,0x01,0xC6,0x40, 0x00,0x00,0x00,0x0F,0x7B,0x63,0xE0,0x00,0x00,0x22,0x00,0x01,0x24,0x00,0x04,0x40, 0x00,0x00,0x00,0x0F,0xFB,0x63,0x07,0x34,0x00,0x14,0x00,0x01,0x04,0x00,0x08,0xF8, 0x00,0x00,0x00,0x0D,0xDB,0x63,0x01,0xBC,0x00,0x08,0x00,0x00,0x88,0x00,0x1F,0x40, 0x00,0x04,0x00,0x0C,0x1B,0x63,0x07,0xB0,0x00,0x00,0x00,0x00,0x50,0x00,0x30,0x40, 0x00,0x06,0x00,0x0C,0x1B,0x63,0xED,0xB0,0xDB,0x00,0x01,0x00,0x20,0x00,0x00,0x40, 0x00,0x02,0x00,0x0C,0x19,0xE1,0xE7,0xB0,0xDB,0x00,0x02,0x00,0x00,0x00,0x00,0x40, 0x00,0x02,0x00,0x0C,0x19,0xE1,0xE7,0xB0,0xDB,0x00,0x02,0x00,0x00,0x00,0x00,0x40, 0x00,0x02,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x42, 0x00,0x02,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x03,0xFF,0xFE, 0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x05,0x00,0x00,0x00,0x40, 0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x0A,0x80,0x00,0x00,0x40, 0x00,0x02,0x20,0x00,0x00,0x0A,0x00,0x00,0xFF,0xF8,0xE0,0x05,0x00,0x00,0x00,0x40, 0x00,0x82,0x20,0x06,0xC0,0x15,0x00,0x1F,0xFF,0xFF,0xC0,0x02,0x00,0x00,0x00,0x40, 0x00,0x82,0x30,0x09,0x20,0x11,0x00,0xFB,0xFF,0xE1,0xF8,0x00,0x00,0x00,0x00,0x40, 0x01,0x82,0x10,0x08,0x20,0x0A,0x03,0x9F,0x00,0x9E,0x3E,0x00,0x00,0x00,0x00,0x40, 0x01,0x02,0x10,0x04,0x40,0x04,0x0E,0x70,0x00,0x81,0xC7,0x80,0x01,0x40,0x00,0x40, 0x01,0x02,0x08,0x02,0x80,0x00,0x1D,0x80,0x00,0xE0,0x61,0xE0,0x02,0xA0,0x00,0x40, 0x02,0x02,0x08,0x01,0x00,0x00,0x77,0x9F,0xFC,0xF0,0x18,0xF8,0x02,0x20,0x00,0x40, 0x06,0x02,0x08,0x00,0x00,0x01,0xDF,0x00,0x00,0xF3,0x0C,0x3C,0x01,0x40,0x00,0x00, 0x0C,0x06,0x0C,0x00,0x00,0x03,0x9E,0x00,0x00,0xF8,0x06,0x1E,0x00,0x80,0x00,0x00, 0x00,0x44,0x04,0x00,0x00,0x07,0x0E,0x30,0x01,0xFC,0x7F,0x07,0x00,0x00,0x00,0x00, 0x00,0x6C,0x06,0x00,0x00,0x07,0x0E,0x30,0x01,0xFC,0x7F,0x07,0x00,0x00,0x00,0x00, 0x00,0x38,0x03,0x00,0x00,0x7E,0xFF,0xFF,0xFF,0xFF,0xFF,0x83,0xC0,0x00,0x00,0x00, 0x00,0x18,0x00,0x00,0x0F,0xFD,0xFF,0xFF,0xFF,0xFF,0xFC,0x01,0xF0,0x00,0x78,0x00, 0x00,0x00,0x00,0x00,0x3F,0x81,0xFF,0x00,0x00,0x00,0x00,0x1F,0xF0,0x00,0x48,0x00, 0x00,0x00,0x00,0x00,0xFE,0xF0,0x00,0x00,0x01,0x00,0x00,0xC0,0x1C,0x00,0x44,0x00, 0x00,0x03,0x00,0x03,0xC0,0x07,0x80,0x00,0x03,0xE2,0x07,0x00,0x1C,0x00,0x46,0x00, 0x00,0x06,0x00,0x03,0x80,0x03,0x40,0x00,0x03,0xE0,0x0C,0x00,0x0E,0x00,0x42,0x00, 0x00,0x1C,0x00,0x07,0x00,0x00,0xC0,0x00,0x00,0x20,0x18,0x00,0x07,0x00,0x42,0x00, 0x00,0xF0,0x00,0x09,0x01,0x80,0x60,0x00,0x00,0x20,0x73,0x9F,0x03,0x80,0x42,0x00, 0x00,0x10,0x00,0x1E,0x0F,0xF2,0x20,0x00,0x00,0x20,0x67,0xFF,0xC1,0xC0,0x46,0x00, 0x00,0x10,0x00,0x1E,0x1F,0xF9,0x30,0x00,0x00,0x20,0xEF,0xFF,0xE0,0xE0,0x7C,0x00, 0x03,0xFC,0x00,0x38,0x3F,0xFC,0x90,0x00,0x00,0x20,0xCB,0xFF,0xF9,0xF0,0xFF,0x00, 0x00,0x10,0x00,0x7F,0x7F,0xFE,0x10,0x00,0x00,0x20,0xDB,0xFF,0xFF,0xF0,0x41,0xC0, 0x00,0x10,0x00,0x6E,0xF9,0xBF,0x10,0x00,0x00,0x20,0xF7,0xED,0xFF,0xF0,0x40,0x60, 0x00,0x11,0x00,0x7C,0xFC,0x3F,0x10,0x00,0x00,0x20,0x67,0xE1,0xFD,0xE0,0x40,0x30, 0x03,0xFF,0x00,0x7C,0xFC,0x3F,0x10,0x00,0x00,0x20,0x67,0xE1,0xFD,0xE0,0xC0,0x10, 0x00,0x10,0x00,0x38,0xFC,0x3F,0x18,0x00,0x00,0x20,0x87,0xE0,0xFD,0xC0,0x80,0x08, 0x00,0x20,0x00,0x38,0xF1,0x8F,0x18,0x00,0x1F,0xE7,0x87,0x9C,0x7D,0xC0,0x80,0x08, 0x00,0x60,0x00,0x1C,0xFE,0x1F,0x81,0xFF,0xFF,0xC0,0x0F,0xE0,0xFF,0x80,0x80,0x04, 0x00,0x40,0x00,0x0C,0xFC,0x3F,0x80,0x00,0x00,0x00,0x0F,0xE1,0xF8,0x00,0x80,0x04, 0x01,0x80,0x00,0x00,0xF9,0xBF,0xE0,0x00,0x00,0x00,0x03,0xEC,0xF6,0x01,0x80,0x0C, 0x01,0xC3,0x00,0x00,0xF9,0xBF,0x00,0x00,0x00,0x00,0x00,0xFF,0xCC,0x01,0x80,0x18, 0x00,0x3F,0x80,0x00,0x7F,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x00,0x81,0xF0, 0x00,0x00,0xC0,0x00,0x07,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00, 0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, }; private void Form1_Paint(object sender, PaintEventArgs e) { } private void pictureBox1_Paint(object sender, PaintEventArgs e) { } protected override void OnPaint(PaintEventArgs e) { } private void Form1_Click(object sender, EventArgs e) { DrawGraph(); } private void DrawGraph() { int i, j, k; int w = 128; int h = 64; for (i = 0; i < 64; i++) { for (j = 0; j < 16; j++) { data[i,j] = tab[i * 16 + j]; } } Gui_Line(0, 0, 100, 50, 1); for (i = 0; i < 64; i++) { for (j = 0; j < 16; j++) { for (k = 0; k < 8; k++) { if (((data[i, j] >> (7 - k)) & 0x01) == 1) { bmp.SetPixel(j * 8 + k, i, Color.Black); } } } } //显示图片 graphics.DrawImage(bmp, new Rectangle(0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height)); } void MyDrawPoint(int x, int y, int color) { int x_byte = x / 8; int x_bit = x % 8; int y_row = y; if (color == 1) { byte temp = (byte)(7 - x_bit); data[y, x_byte] |= (byte)(1 << temp); } } //画水平线函数 //参数:color=1,填充 1;color=0,填充 0; //x0,x1为起始和终点的水平坐标值,y 为垂直坐标值 //************************************************************** void Gui_Hline(int x0, int x1, int y, int color) { //******数据调整***** int temp; if (x0 > x1) { temp = x0; x0 = x1; x1 = temp; } do//从左到右逐点显示 { MyDrawPoint(x0, y, color); x0++; } while (x1 >= x0); } //画竖线函数 void Gui_Rline(int x, int y0, int y1, int color) { int temp; if (y0 > y1) { temp = y0; y1 = y0; y0 = temp; } do { MyDrawPoint(x, y0, color); y0++; } while (y1 >= y0); } //任意两点画直线函数 //参数:color=1,该线填充 1;color=0,该线填充 0; // x0:直线起点的x 坐标值,y0:直线起点的 y 坐标值 // x1:直线终点的x 坐标值,y1:直线终点的 y 坐标值 void Gui_Line(int x0, int y0, int x1, int y1, int color) { int dx;//直线 x 轴差值变量 int dy;//直线 y 轴差值变量 int dx_sym;//x 轴增长方向,为-1 时减值方向,为 1 是增值方向 int dy_sym;//y 轴增长方向,为-1 时减值方向,为 1 时增值方向 int dx_x2;//dx*2 值变量,用于加快运算速度 int dy_x2; int di;//决策变量 if (x0 == x1)//判断是否为垂直线 { Gui_Rline(x0, y0, y1, color);//画垂线 return; } if (y0 == y1)//判断是否是水平线 { Gui_Hline(x0, x1, y0, color); return; } dx = x1 - x0; dy = y1 - y0;//求两点之间的差值 //*****判断增长方向***** if (dx > 0) dx_sym = 1; else { if (dx < 0) dx_sym = -1; else { Gui_Rline(x0, y0, y1, color); return; } } if (dy > 0) dy_sym = 1; else { if (dy < 0) dy_sym = -1; else { Gui_Hline(x0, y0, y1, color); return; } } dx = dx_sym * dx;//计算 dx 的绝对值 dy = dy_sym * dy; dx_x2 = dx * 2;//dx的 2 倍 dy_x2 = dy * 2; /*使用Bresenham 法画直线*/ if (dx >= dy)//对于 dx>=dy,则以x 轴为基准 { di = dy_x2 - dx; while (x0 != x1) { MyDrawPoint(x0, y0, color); x0 += dx_sym; if (di < 0) di += dy_x2; else { di += dy_x2 - dx_x2; y0 += dy_sym; } } MyDrawPoint(x0, y0, color);//显示最后一个点 } else //对于 dx<dy,则以 y 轴为基准 { di = dx_x2 - dy; while (y0 != y1) { MyDrawPoint(x0, y0, color); y0 += dy_sym; if (di < 0) di += dx_x2; else { di += dx_x2 - dy_x2; x0 = dx_sym; } } MyDrawPoint(x0, y0, color); } } } }
项目中可能会用到12864液晶(或者其它分辨率的),用C#模拟了一下显示效果
标签:
原文地址:http://www.cnblogs.com/feifeivictor/p/4466184.html