赞
踩
C#任意语言任意大小任意字体任意排列 字符取模及显示 源码,也可以自由编辑点阵
pc端
android手机端(OTG串口发送)
- //示测试
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- if (textBox1.Text.Length > 0)
- {
- // textBox1.Text = "小黄人软件";
- Bitmap bmp = GetStrBMP(textBox1.Text, 16, 16); // 获取待分析的字符位图
- pictureBox2.Image = Display(bmp, 4, 1); //点放大为4像素,隔线线为1像素
- }
- }
- //把字符串中 每个字符放到格式里
- public Bitmap GetStrBMP(string str, int width, int height) //str字符串 width单个宽度 height单个高度
- {
- int allwidth = str.Length * width;
- Bitmap bmp = new Bitmap(allwidth, height); // 新建位图变量
- Graphics g = Graphics.FromImage(bmp);
- for (int i = 0; i < str.Length; i++)
- {
- g.DrawString(str[i].ToString(), new Font("宋体", 10), Brushes.Black, new PointF(i*width, 0.0F));
- }
- return bmp;
- }
- //把整个字符放到格式里
- public Bitmap GetStrBMP2(string str, int width, int height) //str字符串 width单个宽度 height单个高度
- {
- int allwidth=str.Length*width;
- Bitmap bmp = new Bitmap(allwidth, height); // 新建位图变量
- Graphics g = Graphics.FromImage(bmp);
- g.DrawString(str, new Font("宋体", 10), Brushes.Black, new PointF(0.0F, 0.0F));
- //StringFormat sf = new StringFormat(); // 设置格式
- //sf.Alignment = StringAlignment.Center;
- //sf.LineAlignment = StringAlignment.Center;
- // g.DrawString(str, new Font("宋体", 32), Brushes.Black, new Rectangle(0, 0, allwidth, height), sf); // 向图像变量输出字符
- return bmp;
- }
- public Bitmap Display(Bitmap bmp, int pixelSize, int jg) //bmp要显示的图 pixelSize单个点大小 jg点与点间隔 ,返回转换后的图
- {
- Bitmap bmp2 = new Bitmap(bmp.Width * (pixelSize+jg), bmp.Height * (pixelSize+jg)); // 新建位图变量
- Graphics g = Graphics.FromImage(bmp2);
- Brush temp;
- Brush backgroud = Brushes.Black; //点阵分隔线颜色
- g.FillRectangle(backgroud, new Rectangle(0, 0, bmp2.Width, bmp2.Height));//实心正方形
-
- for (int i = 0; i < bmp.Width; i++)
- {
- for (int j = 0; j < bmp.Height; j++)
- {
- if (bmp.GetPixel(i, j) == Color.FromArgb(0x0, 0, 0)) //黑
- temp = Brushes.White; //亮点颜色
- else
- temp = Brushes.Blue; //暗点颜色
- g.FillRectangle(temp, new Rectangle(i * pixelSize+i*jg, j * pixelSize+j*jg, pixelSize, pixelSize));//点阵实心正方形
- }
- }
- return bmp2;
- }
- //取得字节(低位在前,纵向 从上到下 从左到右,16*16)
- public void CreateCharSet(string str, int width, int height) //str要取模的字符 width单个宽度 height单个高度 ,得到byteArray[]数据 长度为byteArrayLength
- {
-
- //Byte[] byteArray = new Byte[height * width / 8]; //得到的取模数据
- //int byteArrayLength = 0; //得到的取模数据 长度
- int temp = 0;
- foreach (char ch in str)
- {
- int mode = (int)ch;
- if (mode <= 127) { width = width/2; } //如果是字符ASCII,宽度为8 如果是汉字,宽度为16
- Bitmap bmp = GetStrBMP2(ch.ToString(), width, height); // 获取待分析的字符位图
-
- for (int i = 0; i < bmp.Width; i++)
- {
- for (int j = 0; j < bmp.Height; j++) //先列 纵向
- {
- temp = temp >> 1;
- if (bmp.GetPixel(i, j) == Color.FromArgb(0, 0, 0)) //黑色
- temp = temp | 0x80; //判断获取到的像素点
- else
- temp = temp & 0x7f;
-
- if (j % 8 == 7) //8个像素点一个字节
- {
- //byteArray[byteArrayLength] = Convert.ToByte(temp);
- //byteArrayLength++;
- sen[k] = Convert.ToByte(temp);
- k++;
- temp = 0;
-
- }
- }
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。