c#做小票打印机的应用程序,不知道安装了驱动,用的时usb001端口,不知道怎么连接,求大神帮助。。。

急用!!!谢谢了
2024-12-16 15:56:12
推荐回答(1个)
回答1:

将小票打印机设置成默认的打印机,然后用Stream 流,下面连接的就是小票打印机打印rdlc报表,完全可以使用,你可以自己改改用调用的是PPrint()就行

#region 报表自打印
public void PPrint()
{
try
{
LocalReport report = this.reportViewer1.LocalReport;
Export(report);
m_currentPageIndex = 0;
NBPrint();
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}
catch (Exception ex)
{
MessageBox.Show("在打印过程中出现异常! " + ex.Message.ToString());
}
}
private void Export(LocalReport report)
{
//7.5in 3.66in 0 0 0 0 当前设置为A4纵向
string deviceInfo =
"" +
" EMF" +
" 72.1mm" +
" 250mm" +
" 0in" +
" 0in" +
" 0in" +
" 0in" +
"
";
Warning[] warnings;
m_streams = new List();
report.Render("Image", deviceInfo, CreateStream, out warnings);

foreach (Stream stream in m_streams)
{
stream.Position = 0;
return;
}
}
private void NBPrint()
{
if (m_streams == null || m_streams.Count == 0)
return;
PrintDocument printDoc = new PrintDocument();

if (!printDoc.PrinterSettings.IsValid)
{
string msg = String.Format("Can't find printer \"{0}\".", "默认打印机!");
MessageBox.Show(msg, "找不到默认打印机");
return;
}
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();
this.Close();
}
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
{
string filenameext = DateTime.Now.Year.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
Stream stream = new FileStream(name + "." + fileNameExtension, FileMode.Create);
m_streams.Add(stream);
if (m_streams.Count > 1)
{
m_streams.RemoveAt((int)1);
}
return stream;
}
private void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
#endregion