Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

print bitmap company logo c sharp

    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 = new BitArray(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++;
                }
            }

            return new BitmapData()
            {
                Dots = dots,
                Height = (int)(bitmap.Height*scale),
                Width = (int)(bitmap.Width*scale)
            };
        }
    }

    public class BitmapData
    {
        public BitArray Dots
        {
            get;
            set;
        }

        public int Height
        {
            get;
            set;
        }

        public int Width
        {
            get;
            set;
        }
    }
Comment

PREVIOUS NEXT
Code Example
Csharp :: nuget Microsoft.EntityFrameworkCore.InMemory": "1.0.0" 
Csharp :: how to subtract two rows asp ne gridview in asp.net 
Csharp :: how to make enemy killed by bullet unity2D 
Csharp :: infinit range loop c# 
Csharp :: c# entity mvc get user from razor layout 
Csharp :: user (current login user) in viewcomponent 
Csharp :: How to return a list to view after foreach in c# 
Csharp :: Xamarin forms XAML change value 
Csharp :: if exist request c# 
Csharp :: c# XmlElement from string 
Csharp :: How to determine whether Task.Run is completed within a loop in c# 
Csharp :: How to change color of a column in RDLC report 
Csharp :: c# simplified if statement 
Csharp :: mvc input number rounding 
Csharp :: select every second row in html table 
Csharp :: c# Showing a hidden WPF window 
Csharp :: params string[] 
Csharp :: how to pass value to anothe form c# winform 
Csharp :: asserting exceptions c# 
Csharp :: creating an object 
Csharp :: ExpandoObject Convert to Json or Json to ExpandoObject 
Csharp :: 1180 - Lowest Number and Position C# 
Csharp :: c# how to return 2 strings 
Csharp :: c# user and password verification 
Csharp :: add css class based on model value razor 
Csharp :: C# Floating Point Literals 
Csharp :: Last N lines from file 
Csharp :: asp.net mvc select from many to many relationship 
Csharp :: c# excel fix first row 
Csharp :: unity number generator 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =