请教在c#桌面程序(也就是c/s程序)内到底能不能把系统的打印机调出来?并能打印出桌面程序界面上的一些

wjx120 2006-10-20 08:38:44
请教在c#桌面程序(也就是c/s程序)内到底能不能把系统的打印机调出来?并能打印出桌面程序界面上的一些文字或图形等?谢谢!


我正在用vs2003学做一套c#桌面的管理系统方面的程序,我不想用报表来打印文件,想请问几个问题:


1.到底能不能把系统的打印机调出来?请问应该加些什么代码才能把系统的打印机调出来?能否给一段c#代码我试一试?谢谢!


2.调出打印机后能打印出桌面程序界面上的一些文字或图形吗?谢谢!(是不是就像ie中点ie的文件菜单后把打印机调出后就可打网页了吗?)




...全文
244 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
y8464480 2006-11-03
  • 打赏
  • 举报
回复
请大侠帮忙回一下下面的问题,谢谢!


我查资料后找到调用系统记事本和计算器的代码,如下,能调用系统记事本和计算器

System.Diagnostics .Process .Start ("notepad.exe");
System.Diagnostics .Process .Start ("calc.exe");

可我依壶画瓢加了调用打印机的代码,可出现一个黑屏后,闪了一下就不见了,请问是怎么回事啊?是不是我代码错了?能否给一段正确的c#代码我试一试?谢谢!还有,在调出系统打印机后,真能打c/s程序上的文字或图片吗?

调用打印机的代码
System.Diagnostics .Process .Start ("print.exe");

y8464480 2006-11-03
  • 打赏
  • 举报
回复
请大侠帮忙回一下下面的问题,谢谢!


我查资料后找到调用系统记事本和计算器的代码,如下,能调用系统记事本和计算器

System.Diagnostics .Process .Start ("notepad.exe");
System.Diagnostics .Process .Start ("calc.exe");

可我依壶画瓢加了调用打印机的代码,可出现一个黑屏后,闪了一下就不见了,请问是怎么回事啊?是不是我代码错了?能否给一段正确的c#代码我试一试?谢谢!还有,在调出系统打印机后,真能打c/s程序上的文字或图片吗?

调用打印机的代码
System.Diagnostics .Process .Start ("print.exe");
y8464480 2006-11-03
  • 打赏
  • 举报
回复
请知道怎样在c/s程序中把系统的打印机调出来的大侠帮忙回一下啊!谢谢!
wjx120 2006-10-27
  • 打赏
  • 举报
回复
请知道怎样在c/s程序中把系统的打印机调出来的大侠帮忙回一下啊!谢谢!
wjx120 2006-10-27
  • 打赏
  • 举报
回复
请知道怎样在c/s程序中把系统的打印机调出来的大侠帮忙回一下啊!谢谢!
f8931070 2006-10-26
  • 打赏
  • 举报
回复
请知道怎样在c/s程序中把系统的打印机调出来的大侠帮忙回一下啊!谢谢!
wjx120 2006-10-26
  • 打赏
  • 举报
回复
可我完全按你说的去做了,可报错如下啊!请问该怎么处理才行啊?谢谢!也请知道怎样在c/s程序中把系统的打印机调出来的大侠帮忙回一下啊!谢谢!


------ 已启动生成: 项目: EDImageSystem, 配置: Debug .NET ------

正在准备资源...
正在更新引用...
正在执行主编译...
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(49,53): error CS1002: ; expected
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(52,36): error CS1519: Invalid token ')' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(60,26): error CS1519: Invalid token '=' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(60,50): error CS1519: Invalid token '(' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(61,42): error CS1519: Invalid token '(' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(61,47): error CS1519: Invalid token ',' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(61,58): error CS1519: Invalid token ',' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(61,88): error CS1519: Invalid token ',' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(61,106): error CS1519: Invalid token ',' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(61,124): error CS1519: Invalid token ')' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(69,21): error CS1519: Invalid token 'if' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(69,32): error CS1519: Invalid token '/' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(69,54): error CS1519: Invalid token ')' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(69,66): error CS1519: Invalid token '/' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(69,89): error CS1519: Invalid token ')' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(71,31): error CS1519: Invalid token '=' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(71,53): error CS1519: Invalid token ';' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(72,32): error CS1519: Invalid token '=' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(72,50): error CS1002: ; expected
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(72,70): error CS1519: Invalid token '/' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(72,83): error CS1519: Invalid token ';' in class, struct, or interface member declaration
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(74,21): error CS0116: A namespace does not directly contain members such as fields or methods
c:\documents and settings\hwm\my documents\visual studio projects\edimagesystem\printservice.cs(86,9): error CS1022: Type or namespace definition, or end-of-file expected

生成完成 -- 23 个错误,0 个警告
正在生成附属程序集...
由于缺少主项目输出,未能生成附属程序集。


---------------------- 完成 ---------------------

生成: 0 已成功, 1 已失败, 0 已跳过
wjx120 2006-10-22
  • 打赏
  • 举报
回复
谢谢楼上的大哥,你下面的这些代码到底要加在什么位置才行啊?(我把他加在Form1里面,可报好多的错啊!)能不能说一下具体添加的步骤啊?谢谢!也就是下面的代码要加在什么位置才行啊???


using System;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.IO;

namespace EDImageSystem
{
/// <summary>
/// PrintService 的摘要说明。
/// </summary>
public class PrintService
{
public PrintService()
{
//
// TODO: 在此处添加构造函数逻辑
//
this.docToPrint.PrintPage += new PrintPageEventHandler(docToPrint_PrintPage);
}//将事件处理函数添加到PrintDocument的PrintPage中

private System.Drawing.Printing.PrintDocument docToPrint =
new System.Drawing.Printing.PrintDocument();//创建一个PrintDocument的实例

private System.IO.Stream streamToPrint;
string streamType;

public void StartPrint(Stream streamToPrint, string streamType)
{

this.streamToPrint = streamToPrint;
this.streamType = streamType;

System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();//创建一个PrintDialog的实例。
PrintDialog1.AllowSomePages = true;

PrintDialog1.ShowHelp = true;

PrintDialog1.Document = docToPrint;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例

DialogResult result = PrintDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框

if (result == DialogResult.OK)
{
docToPrint.Print();//开始打印
}

}

System.Drawing.Printing.PrintPageEventArgs e)//设置打印机开始打印的事件处理函数
{

switch (this.streamType)
{
case "txt":
string text = null;
System.Drawing.Font printFont = new System.Drawing.Font
("Arial", 35, System.Drawing.FontStyle.Regular);

System.IO.StreamReader streamReader = new StreamReader(this.streamToPrint);
text = streamReader.ReadToEnd();
e.Graphics.DrawString(text, printFont, System.Drawing.Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y);
break;
case "image":
System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
int x = e.MarginBounds.X;
int y = e.MarginBounds.Y;
int width = image.Width;
int height = image.Height;
if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
{
width = e.MarginBounds.Width;
height = image.Height * e.MarginBounds.Width / image.Width;
}
else
{
height = e.MarginBounds.Height;
width = image.Width * e.MarginBounds.Height / image.Height;
}
System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
break;
default:
break;
}

}

}
}

tshark 2006-10-22
  • 打赏
  • 举报
回复
using System;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.IO;

namespace EDImageSystem
{
/// <summary>
/// PrintService 的摘要说明。
/// </summary>
public class PrintService
{
public PrintService()
{
//
// TODO: 在此处添加构造函数逻辑
//
this.docToPrint.PrintPage += new PrintPageEventHandler(docToPrint_PrintPage);
}//将事件处理函数添加到PrintDocument的PrintPage中

private System.Drawing.Printing.PrintDocument docToPrint =
new System.Drawing.Printing.PrintDocument();//创建一个PrintDocument的实例

private System.IO.Stream streamToPrint;
string streamType;

public void StartPrint(Stream streamToPrint, string streamType)
{

this.streamToPrint = streamToPrint;
this.streamType = streamType;

System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();//创建一个PrintDialog的实例。
PrintDialog1.AllowSomePages = true;

PrintDialog1.ShowHelp = true;

PrintDialog1.Document = docToPrint;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例

DialogResult result = PrintDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框

if (result == DialogResult.OK)
{
docToPrint.Print();//开始打印
}

}

System.Drawing.Printing.PrintPageEventArgs e)//设置打印机开始打印的事件处理函数
{

switch (this.streamType)
{
case "txt":
string text = null;
System.Drawing.Font printFont = new System.Drawing.Font
("Arial", 35, System.Drawing.FontStyle.Regular);

System.IO.StreamReader streamReader = new StreamReader(this.streamToPrint);
text = streamReader.ReadToEnd();
e.Graphics.DrawString(text, printFont, System.Drawing.Brushes.Black, e.MarginBounds.X, e.MarginBounds.Y);
break;
case "image":
System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
int x = e.MarginBounds.X;
int y = e.MarginBounds.Y;
int width = image.Width;
int height = image.Height;
if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
{
width = e.MarginBounds.Width;
height = image.Height * e.MarginBounds.Width / image.Width;
}
else
{
height = e.MarginBounds.Height;
width = image.Width * e.MarginBounds.Height / image.Height;
}
System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
break;
default:
break;
}

}

}
}


然后在Form1里面加一个openFileDialog1,在2005里面openFileDialog1是不能显示在上面的,所以需要让它显示出来,我用这个只是为了获得一个流,LZ可以按自己需求,只要得到流就可以了。
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();

Stream stream = openFileDialog1.OpenFile();
byte[] byteData = new byte[stream.Length];
stream.Read(byteData, 0, byteData.Length);

PrintService ps = new PrintService();
ps.StartPrint(stream, Text);
}

点按钮就会弹出打印机界面。命名空间为System.IO
tshark 2006-10-22
  • 打赏
  • 举报
回复
我建立这个项目的时候是叫EDImageSystem,而上面这段代码是一个添加的类。类名叫PrintService
wjx120 2006-10-22
  • 打赏
  • 举报
回复
请大侠帮忙回一下下面的问题,谢谢!


我查资料后找到调用系统记事本和计算器的代码,如下,能调用系统记事本和计算器

System.Diagnostics .Process .Start ("notepad.exe");
System.Diagnostics .Process .Start ("calc.exe");

可我依壶画瓢加了调用打印机的代码,可出现一个黑屏后,闪了一下就不见了,请问是怎么回事啊?是不是我代码错了?能否给一段正确的c#代码我试一试?谢谢!还有,在调出系统打印机后,真能打c/s程序上的文字或图片吗?

调用打印机的代码
System.Diagnostics .Process .Start ("print.exe");
wjx120 2006-10-22
  • 打赏
  • 举报
回复
知道的回一下啊!谢谢!
wjx120 2006-10-20
  • 打赏
  • 举报
回复
谢谢!

我查资料后找到调用系统记事本和计算器的代码,如下,能调用系统记事本和计算器

System.Diagnostics .Process .Start ("notepad.exe");
System.Diagnostics .Process .Start ("calc.exe");

可我依壶画瓢加了调用打印机的代码,可出现一个黑屏后,闪了一下就不见了,请问是怎么回事啊?是不是我代码错了?能否给一段正确的c#代码我试一试?谢谢!还有,在调出系统打印机后,真能打c/s程序上的文字或图片吗?

调用打印机的代码
System.Diagnostics .Process .Start ("print.exe");



Elvewyn 2006-10-20
  • 打赏
  • 举报
回复
可以的。偶在两年前做过。不过我给忘了怎么做了。不过可以肯定的是可以的。你还可以在打印机上随意画图,然后把图打印出来。

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧