当前位置:   article > 正文

C#源码 任意语言任意大小任意字体任意排列 字符汉字取模及显示 可以自由编辑点阵 通过串口发送 pc端和手机端_c# 汉字点阵

c# 汉字点阵

C#任意语言任意大小任意字体任意排列 字符取模及显示 源码,也可以自由编辑点阵

pc端


android手机端(OTG串口发送)

  1. //示测试
  2. private void textBox1_TextChanged(object sender, EventArgs e)
  3. {
  4. if (textBox1.Text.Length > 0)
  5. {
  6. // textBox1.Text = "小黄人软件";
  7. Bitmap bmp = GetStrBMP(textBox1.Text, 16, 16); // 获取待分析的字符位图
  8. pictureBox2.Image = Display(bmp, 4, 1); //点放大为4像素,隔线线为1像素
  9. }
  10. }
  11. //把字符串中 每个字符放到格式里
  12. public Bitmap GetStrBMP(string str, int width, int height) //str字符串 width单个宽度 height单个高度
  13. {
  14. int allwidth = str.Length * width;
  15. Bitmap bmp = new Bitmap(allwidth, height); // 新建位图变量
  16. Graphics g = Graphics.FromImage(bmp);
  17. for (int i = 0; i < str.Length; i++)
  18. {
  19. g.DrawString(str[i].ToString(), new Font("宋体", 10), Brushes.Black, new PointF(i*width, 0.0F));
  20. }
  21. return bmp;
  22. }
  23. //把整个字符放到格式里
  24. public Bitmap GetStrBMP2(string str, int width, int height) //str字符串 width单个宽度 height单个高度
  25. {
  26. int allwidth=str.Length*width;
  27. Bitmap bmp = new Bitmap(allwidth, height); // 新建位图变量
  28. Graphics g = Graphics.FromImage(bmp);
  29. g.DrawString(str, new Font("宋体", 10), Brushes.Black, new PointF(0.0F, 0.0F));
  30. //StringFormat sf = new StringFormat(); // 设置格式
  31. //sf.Alignment = StringAlignment.Center;
  32. //sf.LineAlignment = StringAlignment.Center;
  33. // g.DrawString(str, new Font("宋体", 32), Brushes.Black, new Rectangle(0, 0, allwidth, height), sf); // 向图像变量输出字符
  34. return bmp;
  35. }
  36. public Bitmap Display(Bitmap bmp, int pixelSize, int jg) //bmp要显示的图 pixelSize单个点大小 jg点与点间隔 ,返回转换后的图
  37. {
  38. Bitmap bmp2 = new Bitmap(bmp.Width * (pixelSize+jg), bmp.Height * (pixelSize+jg)); // 新建位图变量
  39. Graphics g = Graphics.FromImage(bmp2);
  40. Brush temp;
  41. Brush backgroud = Brushes.Black; //点阵分隔线颜色
  42. g.FillRectangle(backgroud, new Rectangle(0, 0, bmp2.Width, bmp2.Height));//实心正方形
  43. for (int i = 0; i < bmp.Width; i++)
  44. {
  45. for (int j = 0; j < bmp.Height; j++)
  46. {
  47. if (bmp.GetPixel(i, j) == Color.FromArgb(0x0, 0, 0)) //黑
  48. temp = Brushes.White; //亮点颜色
  49. else
  50. temp = Brushes.Blue; //暗点颜色
  51. g.FillRectangle(temp, new Rectangle(i * pixelSize+i*jg, j * pixelSize+j*jg, pixelSize, pixelSize));//点阵实心正方形
  52. }
  53. }
  54. return bmp2;
  55. }
  56. //取得字节(低位在前,纵向 从上到下 从左到右,16*16)
  57. public void CreateCharSet(string str, int width, int height) //str要取模的字符 width单个宽度 height单个高度 ,得到byteArray[]数据 长度为byteArrayLength
  58. {
  59. //Byte[] byteArray = new Byte[height * width / 8]; //得到的取模数据
  60. //int byteArrayLength = 0; //得到的取模数据 长度
  61. int temp = 0;
  62. foreach (char ch in str)
  63. {
  64. int mode = (int)ch;
  65. if (mode <= 127) { width = width/2; } //如果是字符ASCII,宽度为8 如果是汉字,宽度为16
  66. Bitmap bmp = GetStrBMP2(ch.ToString(), width, height); // 获取待分析的字符位图
  67. for (int i = 0; i < bmp.Width; i++)
  68. {
  69. for (int j = 0; j < bmp.Height; j++) //先列 纵向
  70. {
  71. temp = temp >> 1;
  72. if (bmp.GetPixel(i, j) == Color.FromArgb(0, 0, 0)) //黑色
  73. temp = temp | 0x80; //判断获取到的像素点
  74. else
  75. temp = temp & 0x7f;
  76. if (j % 8 == 7) //8个像素点一个字节
  77. {
  78. //byteArray[byteArrayLength] = Convert.ToByte(temp);
  79. //byteArrayLength++;
  80. sen[k] = Convert.ToByte(temp);
  81. k++;
  82. temp = 0;
  83. }
  84. }
  85. }
  86. }
  87. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/715929
推荐阅读
相关标签
  

闽ICP备14008679号