Public Function GetLogo() As String Dim logo As String = "" If Not File.Exists("C:\Users\Administrator\Desktop\Sensitive\Invlogo.bmp") Then Return Nothing End If Dim data As BitmapData = GetBitmapData("C:\Users\Administrator\Desktop\Sensitive\Invlogo.bmp") Dim dots As BitArray = data.Dots Dim width As Byte() = BitConverter.GetBytes(data.Width) Dim offset As Integer = 0 Using stream As New MemoryStream() Using bw As New BinaryWriter(stream) bw.Write(ChrW(&H1B)) bw.Write("@"c) bw.Write(ChrW(&H1B)) bw.Write("3"c) bw.Write(CByte(24)) While offset < data.Height bw.Write(ChrW(&H1B)) bw.Write("*"c) ' bit-image mode bw.Write(CByte(33)) ' 24-dot double-density bw.Write(width(0)) ' width low byte bw.Write(width(1)) ' width high byte For x As Integer = 0 To data.Width - 1 For k As Integer = 0 To 2 Dim slice As Byte = 0 For b As Integer = 0 To 7 Dim y As Integer = (((offset \ 8) + k) * 8) + b ' Calculate the location of the pixel we want in the bit array. ' It'll be at (y * width) + x. Dim i As Integer = (y * data.Width) + x ' If the image is shorter than 24 dots, pad with zero. Dim v As Boolean = False If i < dots.Length Then v = dots(i) End If slice = CByte(slice Or ((If(v, 1, 0) << (7 - b)))) Next bw.Write(slice) Next Next offset += 24 bw.Write(ChrW(&HA)) End While ' Restore the line spacing to the default of 30 dots. bw.Write(ChrW(&H1B)) bw.Write("3"c) bw.Write(CByte(30)) Dim ascii As New ASCIIEncoding() bw.Flush() Dim bytes As Byte() = stream.ToArray() SerialPort1.Write(bytes, 0, bytes.Length) Return logo & Encoding.Default.GetString(bytes) End Using End Using End Function Public Function GetBitmapData(bmpFileName As String) As BitmapData Using bitmap As Bitmap = CType(bitmap.FromFile(bmpFileName), Bitmap) Dim threshold As Integer = 127 Dim index As Integer = 0 Dim multiplier As Double = 570 ' this depends on your printer model. for Beiyang you should use 1000 Dim scale As Double = multiplier / bitmap.Width Dim xheight As Integer = CInt(bitmap.Height * scale) Dim xwidth As Integer = CInt(bitmap.Width * scale) Dim dimensions As Integer = xwidth * xheight Dim dots As New BitArray(dimensions) For y As Integer = 0 To xheight - 1 For x As Integer = 0 To xwidth - 1 Dim _x As Integer = CInt(x / scale) Dim _y As Integer = CInt(y / scale) Dim color As Color = bitmap.GetPixel(_x, _y) Dim luminance As Integer = CInt(color.R * 0.3 + color.G * 0.59 + color.B * 0.11) dots(index) = (luminance < threshold) index += 1 Next Next Return New BitmapData() With { .Dots = dots, .Height = CInt(bitmap.Height * scale), .Width = CInt(bitmap.Width * scale) } End Using End Function Public Class BitmapData Public Property Dots As BitArray Public Property Height As Integer Public Property Width As Integer End Class
SerialPort1
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)