zoukankan      html  css  js  c++  java
  • C# comport 打印图像

      public string GetLogo()
        {
            string logo = "";
            if (!File.Exists(@"C:itmap.bmp"))
                return null;
             BitmapData data = GetBitmapData(@"C:itmap.bmp");
             BitArray dots = data.Dots;
             byte[] width = BitConverter.GetBytes(data.Width);
    
             int offset = 0;
             MemoryStream stream = new MemoryStream();
             BinaryWriter bw = new BinaryWriter(stream);
    
             bw.Write((char)0x1B);
             bw.Write('@');
    
             bw.Write((char)0x1B);
             bw.Write('3');
             bw.Write((byte)24);
    
             while (offset < data.Height)
             {
                 bw.Write((char)0x1B);
                 bw.Write('*');         // bit-image mode
                 bw.Write((byte)33);    // 24-dot double-density
                 bw.Write(width[0]);  // width low byte
                 bw.Write(width[1]);  // width high byte
    
                 for (int x = 0; x < data.Width; ++x)
                 {
                     for (int k = 0; k < 3; ++k)
                     {
                         byte slice = 0;
                         for (int b = 0; b < 8; ++b)
                         {
                             int y = (((offset / 8) + k) * 8) + b;
                             // Calculate the location of the pixel we want in the bit array.
                             // It'll be at (y * width) + x.
                             int i = (y * data.Width) + x;
    
                             // If the image is shorter than 24 dots, pad with zero.
                             bool v = false;
                             if (i < dots.Length)
                             {
                                 v = dots[i];
                             }
                             slice |= (byte)((v ? 1 : 0) << (7 - b));
                         }
    
                         bw.Write(slice);
                     }
                 }
                 offset += 24;
                 bw.Write((char)0x0A);
             }
             // Restore the line spacing to the default of 30 dots.
             bw.Write((char)0x1B);
             bw.Write('3');
             bw.Write((byte)30);
    
             bw.Flush();
             byte[] bytes = stream.ToArray();
             return logo + Encoding.Default.GetString(bytes);
        }
    
        public BitmapData GetBitmapData(string bmpFileName)
        {
            using (var bitmap = (Bitmap)Bitmap.FromFile(bmpFileName))
            {
                var threshold = 127;
                var index = 0;
                double multiplier = 570; // this depends on your printer model. for Beiyang you should use 1000
                double scale = (double)(multiplier/(double)bitmap.Width);
                int xheight = (int)(bitmap.Height * scale);int xwidth =(int)(bitmap.Width* scale);var dimensions = xwidth * xheight;var dots =newBitArray(dimensions);for(var y =0; y < xheight; y++){for(var x =0; x < xwidth; x++){var _x =(int)(x / scale);var _y =(int)(y / scale);var color = bitmap.GetPixel(_x, _y);var luminance =(int)(color.R *0.3+ color.G *0.59+ color.B *0.11);
                        dots[index]=(luminance < threshold);
                        index++;}}returnnewBitmapData(){Dots= dots,Height=(int)(bitmap.Height*scale),Width=(int)(bitmap.Width*scale)};}}publicclassBitmapData{publicBitArrayDots{
                get;set;}publicintHeight{
                get;set;}publicintWidth{
                get;set;}}
  • 相关阅读:
    绝对干货:供个人开发者赚钱免费使用的一些好的API接口
    科普技术贴:个人开发者的那些赚钱方式
    北漂程序员的笑与泪
    非著名程序员公众号
    北漂程序员的笑与泪
    【有人@我】Android中高亮变色显示文本中的关键字
    新时代的coder如何成为专业程序员
    自定义圆形控件RoundImageView并认识一下attr.xml
    偷天换日:网络劫持,网页js被伪装替换。
    jeesite 去掉 /a
  • 原文地址:https://www.cnblogs.com/honeynm/p/4597989.html
Copyright © 2011-2022 走看看