public string GetLogo()
{
string logo = "";
if (!File.Exists(@"C:\bitmap.bmp"))
return null;
BitmapData data = GetBitmapData(@"C:\bitmap.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;}}

C# comport 打印图像的更多相关文章

  1. 据序和中序序列或者也许为了一个二进制序列,恢复二进制和打印图像(c语言)

    首先要预购和序,以恢复它: 1.首先,我们使用的是递归的方式来完成 2.递归的最小单位:一个空的树和书的前言和第一序.该序列的第一个元素是树的第一序列根,调用这种方法 3.递归的终止条件是.当这棵树的 ...

  2. 【Win 10 应用开发】打印UI元素

    Windows App支持将UI界面进行打印的功能,这与浏览器中的打印网页的用途相近,其好处就是“所见即所得”,直接把界面上呈现的内容打印下来,比重新创建打印图像方便得多. 要在通用App中实现打印, ...

  3. C# EPL USB 指令打印

    private void btnPrinter_Click(object sender, EventArgs e) { #region ESC 热敏图像点阵像素点读取打印 //Bitmap bitma ...

  4. C#调用斑马打印机打印条码标签(支持COM、LPT、USB、TCP连接方式和ZPL、EPL、CPCL指令)【转】

    原文地址:http://blog.csdn.net/ldljlq/article/details/7338772 在批量打印商品标签时一般都要加上条码或图片,而这类应用大多是使用斑马打印机,所以我也遇 ...

  5. (转)原始图像数据和PDF中的图像数据

    比较原始图像数据和PDF中的图像数据,结果见表1.1.表1.1中各种“解码器”的解释见本文后续的“PDF支持的图像格式”部分,“PDF中的图像数据”各栏中的数据来自开源的PdfView.如果您有兴趣查 ...

  6. 跟我一起学PCL打印语言(一)

    引言 本人从事打印机开发和打印驱动开发的相关工作,深感资料特别是中文资料的匮乏和不成系统,对新入门的从事该行业的人来说,门槛很高.在这里一方面是将开发中遇到的相关知识点整理出来,另一方面也能够促进自己 ...

  7. 【Android Developers Training】 52. 打印照片

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  8. Delphi 打印 Tprinter

    打印          打印对于许多 Windows 程序员来说是十分棘手的问题. Delphi 简化了打印时用户所必须了解的大部分内容.用户可以很轻松地写出简单的打印程序来输出文本和位图化了的图像. ...

  9. Python各种图像库的图像的基本读写方式

    目前主流的图像库有几下几种: 1. OpenCV      2. PIL(Pillow)       3. matplotlib.image     4. skimage      5. scipy. ...

随机推荐

  1. final可以修饰类、属性、方法

    final可以修饰类.属性.方法. 当用final修饰类的时候,此类不可被继承,即final类没有子类.这样可以用final保证用户调用时动作的一致性,可以防止子类覆盖情况的发生. 当利用final修 ...

  2. linux文件目录连接

    linux系统下提供ln指令来进行文件链接.文件链接主要分为硬链接和软链接. 硬链接:由于linux下的文件是通过索引节点(Inode)来识别文件,硬链接可以认为是一个指针,指向文件索引节点的指针,系 ...

  3. Java Web开发SpringMVC和MyBatis框架开发环境搭建和简单有用

    1.下载SpringMVC框架架包,下载地址: 点击下载 点击打开地址如图所看到的.点击下载就可以 然后把相关的jar拷贝到lib下导入 2.MyBatis(3.4.2)下载 X-Amz-Algori ...

  4. 【转】Native Thread for Win32 A- Create Thread(通俗易懂,非常好)

    http://www.bogotobogo.com/cplusplus/multithreading_win32A.php Microsoft Windows operating system's s ...

  5. ReactNative iOS源码解析

    http://awhisper.github.io/2016/06/24/ReactNative%E6%B5%81%E7%A8%8B%E6%BA%90%E7%A0%81%E5%88%86%E6%9E% ...

  6. POJ2456 Aggressive cows(二分+贪心)

    如果C(d)为满足全部牛之间的距离都不小于d. 先对牛舍的位置排序,然后二分枚举d,寻找满足条件的d. #include<iostream> #include<cstdio> ...

  7. Linux命令之乐--test

    官方文档: help test File operators: -a FILE True if file exists. -b FILE True if file is block special. ...

  8. 使用jq实现打印机的效果

    本例中使用的是jq和es6的语法,代码如下: html: <div id="box"> this is test <br/>  这是测试用的 </di ...

  9. Android测试:从零开始2——local单元测试

    上一篇分析了android项目的测试分类,这一篇讲local单元测试. 参考android官方文档. 测试前需要配置测试环境,新建项目后,目录下会出现app/src/test/java/文件夹,这个文 ...

  10. js让程序暂停运行的方法

    //自己写的暂停毫秒数的函数 function pauseTime(millTime) { var start=Date.now(); while(true){ var nowTime=Date.no ...