我们的程序要和外设(LED)通信,LED要求不管数据来源是文本还是图片或其它的数据,都必须将其转化为图片,此图片的色值只有红、黑、黄、绿四种颜色。问题是对 4 行字符串,用什么方法可以转换为图片啊?

解决方案 »

  1.   

    创建TBitmap,把文本TextOut 出来,存为图片
      

  2.   

    图片都是由一个一个的像素组成的const   
          BaseTable   =   'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';   
          SMTP_RES_EX:   array[1..9]   of   String   =   ('250',   '334','334','235','250','250','354','250','221');   
        
      function   EncodeBase64(Source:   String):String;   
      var   
              Times,   LenSrc,   i:   Integer;   
              x1,   x2,   x3,   x4:   Char;   
              xt:   Byte;   
      begin   
              Result   :=   '';   
              LenSrc   :=   Length(Source);   
              if   LenSrc   mod   3   =   0   then   
                  Times   :=   LenSrc   div   3   
              else   
                  Times   :=   LenSrc   div   3   +   1;   
              for   i   :=   0   to   Times   -   1   do   
                          begin   
                          if   LenSrc   >=   (3   +   i   *   3)   then   
                          begin   
                                  x1   :=   BaseTable[(ord(Source[1   +   i   *   3])   shr   2)+1];   
                                  xt   :=   (ord(Source[1   +   i   *   3])   shl   4)   and   48;   
                                  xt   :=   xt   or   (ord(Source[2   +   i   *   3])   shr   4);   
                                  x2   :=   BaseTable[xt   +   1];   
                                  xt   :=   (Ord(Source[2   +   i   *   3])   shl   2)   and   60;   
                                  xt   :=   xt   or   (Ord(Source[3   +   i   *   3])   shr   6);   
                                  x3   :=   BaseTable[xt   +   1];   
                                  xt   :=   (ord(Source[3   +   i   *   3])   and   63);   
                                  x4   :=   BaseTable[xt   +   1];   
                          end   
                          else   if   LenSrc   >=   (2   +   i   *   3)   then   
                          begin   
                                      x1   :=   BaseTable[(Ord(Source[1   +   i   *   3])   shr   2)   +   1];   
                                      xt   :=   (Ord(Source[1   +   i   *   3])   shl   4)   and   48;   
                                      xt   :=   xt   or   (Ord(Source[2   +   i   *   3])   shr   4);   
                                      x2   :=   BaseTable[xt   +   1];   
                                      xt   :=   (Ord(Source[2   +   i   *   3])   shl   2)   and   60;   
                                      x3   :=   BaseTable[xt   +   1];   
                                      x4   :=   '=';   
                          end   else   
                          begin   
                                  x1   :=   BaseTable[(Ord(Source[1   +   i   *   3])   shr   2)+1];   
                                  xt   :=   (Ord(Source[1   +   i   *   3])   shl   4)   and   48;   
                                  x2   :=   BaseTable[xt   +   1];   
                                  x3   :=   '=';   
                                  x4   :=   '=';   
                          end;   
                                Result   :=   Result   +   x1   +   x2   +   x3   +   x4;   
                end;   
      end;   
      
    var   
          fs:   TFileStream;   
          ss:   TStringStream;   
      begin   
          if   OpenDialog1.Execute   then   
          begin   
              fs   :=   TFileStream.Create(OpenDialog1.FileName,   fmOpenRead);   
              ss   :=   TStringStream.Create('');   
              try   
                  ss.CopyFrom(fs);   
                  Base64Encode(SS.DataString);   
                  ...   
              finally   
                  fs.Free;   
                  ss.Free;   
              end;   
          end;   
      end;
      

  3.   

    太感谢zaza_bbface(我试着成熟一点~)了,能否具体点???
      

  4.   

    我使用 Canvas.TextOut可以在图片上输出文字,但如何把整个图片的背景设置为黑色呢?
      

  5.   

    画字前,canvas的brush的color设置成clblack,然后fillRect