C#实现虚拟打印机HPColorLaserJet4500(3)PRN⽂件的显
⽰
这是最后⼀篇了..完结了 后期慢慢改把
上⼀篇 ⾥增加了两个获取宽..这⾥要⽤. 修正了符合TRUE的显⽰和⼀些字符的定义... 重新下载把.
我们获取到 HP Color LaserJet 4500 打印出的RPN⽂件...然后使⽤代码来显⽰
ImagePRN _HPGL = new ImagePRN(listBox1.SelectedItem.ToString());
m_PrintImageList = _HPGL.PrintBitmap;
属性 PrintBitmap 是返回的打印图形的结果集.
具体使⽤可能会出现⼀些PCL或则HPGL的语法不识别或则不正确什么的..可以把PRN⽂件发送给我.先谢谢拉.忙没时间详细测试.
WORD⾥添加⼀些图⽚和⽂字 然后打印到PRN⽂件 显⽰的效果图
下⾯是全部PRN的类
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Zgke.MyImage.ImageFile
{
/
// <summary>
/// Image PRN
///
/// qq:116149
/// </summary>
public class ImagePRN
{
#region 私有⽅法
/// <summary>
/// 打印机缓冲区
/// </summary>
private PrintBuffer m_PrintBuffer = new PrintBuffer();
/// <summary>
/// PCL命令执⾏器
/// </summary>
private PCL m_PCL;
/// <summary>
/// HPGL命令执⾏器
/// </summary>
private HPGL m_HPGL;
/// <summary>
/// ⽂件头
/
// </summary>
private byte[] m_HeardText = new byte[] { 0x1B, 0x25, 0x2D, 0x31, 0x32, 0x33, 0x34, 0x35, 0x58 }; /// <summary>
/// ⽂件头的ASCII⽂字信息
/// </summary>
private IList<string> m_PrintSet = new List<string>();
/// <summary>
/// 执⾏到的位置
/// </summary>
private int m_ReadIndex = 0;
/// <summary>
/// 打印完成的图形
/// </summary>
private IList<Image> m_PrintBitmap = new List<Image>();
#endregion
/// <summary>
/// 图形
/// </summary>
public IList<Image> PrintBitmap { get { return m_PrintBitmap; } }
/// 获取打印的Image
/// </summary>
/// <param name="p_File">HP Color LaserJet 4500 打印机保存出的命令</param>
public ImagePRN(string p_File)
{
m_PCL = new PCL(m_PrintBuffer, m_PrintBitmap);
m_HPGL = new HPGL(m_PrintBuffer, m_PrintBitmap);
if (!File.Exists(p_File)) throw new Exception("⽂件不存在!");
byte[] _HPGLBytes = File.ReadAllBytes(p_File);
if (Encoding.ASCII.GetString(m_HeardText) != Encoding.ASCII.GetString(_HPGLBytes, 0, 9)) throw new Exception("⽂件格式不正确!");
m_ReadIndex = 9;
LoadFile(_HPGLBytes);
StarCommand(_HPGLBytes);
}
/// <summary>
/// 获取⽂件头的ASCII信息 并且保存到m_PrintSet ⾥
/// </summary>
/// <param name="p_FileBytes">⽂件字符</param>
private void LoadFile(byte[] p_FileBytes)
{
int _StarIndex = m_ReadIndex;
while (m_ReadIndex + 1 < p_FileBytes.Length)
{
m_ReadIndex++;
switch (p_FileBytes[m_ReadIndex])
{
case 0x0A: //@
m_PrintSet.Add(Encoding.ASCII.GetString(p_FileBytes, _StarIndex, m_ReadIndex - _StarIndex));
_StarIndex = m_ReadIndex + 1;
break;
case 0x0D:
m_ReadIndex++;
return;
}
}
}
/// 开始执⾏命令
/
// </summary>
/// <param name="p_FileBytes"></param>
private void StarCommand(byte[] p_FileBytes)
{
while (m_ReadIndex < p_FileBytes.Length)
{
switch (p_FileBytes[m_ReadIndex])
{
case 0x0D:
m_PrintBuffer.PCL_HorizontalCursorPositioning = 0;
m_PrintBuffer.PCL_VerticalCursorPositioning = 0;
m_ReadIndex++;
continue;
case 0x0C:
m_PrintBitmap.Add(GetPrintOfPageSize());
m_ReadIndex++;
continue;
}
m_ReadIndex++;
if (p_FileBytes[m_ReadIndex] == 0x1B) continue;
switch ((char)p_FileBytes[m_ReadIndex])
{
case 'E':
break;
case '*':
m_PCL.StartCommandLine(p_FileBytes, ref m_ReadIndex);
break;
case '&':
m_PCL.StartCommandLine(p_FileBytes, ref m_ReadIndex);
break;
case '(':
m_PCL.StartCommandLine(p_FileBytes, ref m_ReadIndex);
break;
generic打印机
case ')':
m_PCL.StartCommandLine(p_FileBytes, ref m_ReadIndex);
break;
case '%':
if (p_FileBytes[m_ReadIndex + 1] == 0x2D) return;
m_HPGL.StartCommandLine(p_FileBytes, ref m_ReadIndex);
break;
default:
System.Windows.Forms.MessageBox.Show(m_ReadIndex.ToString(), ((char)p_FileBytes[m_ReadIndex]).ToString());
break;
}
}
}
/// 获取最终打印⼤⼩
/// </summary>
/// <returns></returns>
private Image GetPrintOfPageSize()
{
decimal _Width = 0;
decimal _Height = 0;
decimal _WidthMM = 0;
decimal _HeightMM = 0;
decimal _Scale = 25.4M;
switch (m_PrintBuffer.PCL_PageSize)
{
case 26: //A4
_WidthMM = 210;
_HeightMM = 297;
break;
default:
throw new Exception("未实现的纸张⼤⼩!");
}
switch (m_PrintBuffer.PCL_PageOrientation)
{
case 1:
_Width = _HeightMM / _Scale * 150;
_Height = _WidthMM / _Scale * 150;
break;
default:
_Width = _WidthMM / _Scale * 150;
_Height = _HeightMM / _Scale * 150;
break;
}
Bitmap _Page = new Bitmap((int)_Width, (int)_Height);
Graphics _PageGraphics = Graphics.FromImage(_Page);
_PageGraphics.Clear(Color.White);
int _StarX = (_Page.Width - m_PrintBuffer.m_PrintBitmap.Width) / 2;
int _StarY = (_Page.Height - m_PrintBuffer.m_PrintBitmap.Height) / 2;
if (_StarX < 0) _StarX = 0;
if (_StarY < 0) _StarY = 0;
_PageGraphics.DrawImage(m_PrintBuffer.m_PrintBitmap, _StarX, _StarY, m_PrintBuffer.m_PrintBitmap.Width, m_PrintBuffer.m_PrintBitmap.Height);
_PageGraphics.Dispose();
m_PrintBuffer.m_Graphics.Clear(Color.White);
return _Page;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论