using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.IO.Ports; using System.Net.Sockets; using System.Runtime.InteropServices; using System.Text;
private static void initCompressCode() { //G H I J K L M N O P Q R S T U V W X Y 对应1,2,3,4……18,19。 //g h i j k l m n o p q r s t u v w x y z 对应20,40,60,80……340,360,380,400。 for (int i = 0; i < 19; i++) { compressDictionary.Add(new KeyValue() { Key = Convert.ToChar(71 + i), Value = i + 1 }); } for (int i = 0; i < 20; i++) { compressDictionary.Add(new KeyValue() { Key = Convert.ToChar(103 + i), Value = (i + 1) * 20 }); } } #endregion
#region 生成CPCL图像打印指令 public static byte[] getCPCLBytes() { //GRAPHICS Commands //Bit-mapped graphics can be printed by using graphics commands. ASCII hex (hexadecimal) is //used for expanded graphics data (see example). Data size can be reduced to one-half by utilizing the //COMPRESSED-GRAPHICS commands with the equivalent binary character(s) of the hex data. When //using CG, a single 8 bit character is sent for every 8 bits of graphics data. When using EG two characters //(16 bits) are used to transmit 8 bits of graphics data, making EG only half as efficient. Since this data is //character data, however, it can be easier to handle and transmit than binary data. //Format: //{command} {width} {height} {x} {y} {data} //where: //{command}: Choose from the following: //EXPANDED-GRAPHICS (or EG): Prints expanded graphics horizontally. //VEXPANDED-GRAPHICS (or VEG): Prints expanded graphics vertically. //COMPRESSED-GRAPHICS (or CG): Prints compressed graphics horizontally. //VCOMPRESSED-GRAPHICS (or VCG): Prints compressed graphics vertically. //{width}: Byte-width of image. //{height} Dot-height of image. //{x}: Horizontal starting position. //{y}: Vertical starting position. //{data}: Graphics data. //Graphics command example //Input: //! 0 200 200 210 1 //EG 2 16 90 45 F0F0F0F0F0F0F0F00F0F0F0F0F0F0F0F //F0F0F0F0F0F0F0F00F0F0F0F0F0F0F0F //FORM //PRINT
// If original bitmap is not already in 32 BPP, ARGB format, then convert if (pimage.PixelFormat != PixelFormat.Format32bppArgb) { source = new Bitmap(pimage.Width, pimage.Height, PixelFormat.Format32bppArgb); source.SetResolution(pimage.HorizontalResolution, pimage.VerticalResolution); using (Graphics g = Graphics.FromImage(source)) { g.DrawImageUnscaled(pimage, 0, 0); } } else { source = pimage; }
int sourceIndex = 0; int destinationIndex = 0; int pixelTotal = 0; byte destinationValue = 0; int pixelValue = 128; int height = source.Height; int width = source.Width; int threshold = 500;
// Iterate lines for (int y = 0; y < height; y++) { sourceIndex = y * sourceData.Stride; destinationIndex = y * destinationData.Stride; destinationValue = 0; pixelValue = 128;
// Iterate pixels for (int x = 0; x < width; x++) { // Compute pixel brightness (i.e. total of Red, Green, and Blue values) pixelTotal = sourceBuffer[sourceIndex + 1] + sourceBuffer[sourceIndex + 2] + sourceBuffer[sourceIndex + 3]; if (pixelTotal > threshold) { destinationValue += (byte)pixelValue; } if (pixelValue == 1) { destinationBuffer[destinationIndex] = destinationValue; destinationIndex++; destinationValue = 0; pixelValue = 128; } else { pixelValue >>= 1; } sourceIndex += 4; } if (pixelValue != 128) { destinationBuffer[destinationIndex] = destinationValue; } }
// Copy binary image data to destination bitmap Marshal.Copy(destinationBuffer, 0, destinationData.Scan0, imageSize);
int bfOffBits = BitConverter.ToInt32(dstBuffer, 10); result = new byte[GraphHeight * RowRealBytesCount];
//读取时需要反向读取每行字节实现上下翻转的效果,打印机打印顺序需要这样读取。 for (int i = 0; i < GraphHeight; i++) { Array.Copy(dstBuffer, bfOffBits + (GraphHeight - 1 - i) * RowSize, result, i * RowRealBytesCount, RowRealBytesCount); } } catch (Exception ex) { throw new Exception(ex.Message, ex); } finally { if (srcStream != null) { srcStream.Dispose(); srcStream = null; } if (dstStream != null) { dstStream.Dispose(); dstStream = null; } if (srcBmp != null) { srcBmp.Dispose(); srcBmp = null; } if (dstBmp != null) { dstBmp.Dispose(); dstBmp = null; } } return result; } #endregion
#region LZ77图像字节流压缩方法 public static string CompressLZ77(string text) { //将转成16进制的文本进行压缩 string result = string.Empty; char[] arrChar = text.ToCharArray(); int count = 1; for (int i = 1; i < text.Length; i++) { if (arrChar[i - 1] == arrChar[i]) { count++; } else { result += convertNumber(count) + arrChar[i - 1]; count = 1; } if (i == text.Length - 1) { result += convertNumber(count) + arrChar[i]; } } return result; }
public static string DecompressLZ77(string text) { string result = string.Empty; char[] arrChar = text.ToCharArray(); int count = 0; for (int i = 0; i < arrChar.Length; i++) { if (isHexChar(arrChar[i])) { //十六进制值 result += new string(arrChar[i], count == 0 ? 1 : count); count = 0; } else { //压缩码 int value = GetCompressValue(arrChar[i]); count += value; } } return result; }
private static int GetCompressValue(char c) { int result = 0; for (int i = 0; i < compressDictionary.Count; i++) { if (c == compressDictionary[i].Key) { result = compressDictionary[i].Value; } } return result; }
private static bool isHexChar(char c) { return c > 47 && c < 58 || c > 64 && c < 71 || c > 96 && c < 103; }
private static string convertNumber(int count) { //将连续的数字转换成LZ77压缩代码,如000可用I0表示。 string result = string.Empty; if (count > 1) { while (count > 0) { for (int i = compressDictionary.Count - 1; i >= 0; i--) { if (count >= compressDictionary[i].Value) { result += compressDictionary[i].Key; count -= compressDictionary[i].Value; break; } } } } return result; } #endregion } }
using System; using System.Configuration; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Printing; using System.IO; using System.Text; using System.Windows.Forms; using Microsoft.Reporting.WinForms; using Umisky.BarcodePrint.Core;
namespace Umisky.BarcodePrint.Codes { public class LocalReportHelper : IDisposable {
#region Properties
public String LocalReportPath { get; set; } public LocalRe