我的目的是将数据库中文章内容读到流中,然后输出打印,可打迎的结果是空的.
System.IO.MemoryStream stream = new MemoryStream();

string content = reader.GetString(2);
byte[] byteArray = Encoding.Default.GetBytes(content);
stream.Write(byteArray,0,byteArray.Length);StartPrint(stream);
StartPrint()方法的定义如下public void StartPrint(Stream streamToPrint) 

this.streamToPrint=streamToPrint; 
// Allow the user to choose the page range he or she would 
// like to print. 
printDialog1.AllowSomePages = true;  // Show the help button. 
printDialog1.ShowHelp = true;  // Set the Document property to the PrintDocument for 
// which the PrintPage Event has been handled. To display the 
// dialog, either this property or the PrinterSettings property 
// must be set 
printDialog1.Document = printDocument1;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例  DialogResult result = printDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框  // If the result is OK then print the document. 
if (result==DialogResult.OK) 

printDocument1.Print();//开始打印 
}  } 这跟内存流的编码什么的有关吗?

解决方案 »

  1.   

    你的printDocument1怎么进行处理的
      

  2.   

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    string text = null; 
    System.Drawing.Font printFont = new System.Drawing.Font 
    ("宋体", 12, System.Drawing.FontStyle.Regular);  // Draw the content. 
    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); 
    }
      

  3.   

    我将完整的贴出来吧 private System.IO.Stream streamToPrint; private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    string text = null; 
    System.Drawing.Font printFont = new System.Drawing.Font 
    ("宋体", 12, System.Drawing.FontStyle.Regular);  // Draw the content. 
    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); 
    } public void StartPrint(Stream streamToPrint) 

    this.streamToPrint=streamToPrint; 
    // Allow the user to choose the page range he or she would 
    // like to print. 
    printDialog1.AllowSomePages = true;  // Show the help button. 
    printDialog1.ShowHelp = true;  // Set the Document property to the PrintDocument for 
    // which the PrintPage Event has been handled. To display the 
    // dialog, either this property or the PrinterSettings property 
    // must be set 
    printDialog1.Document = printDocument1;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例  DialogResult result = printDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框  // If the result is OK then print the document. 
    if (result==DialogResult.OK) 

    printDocument1.Print();//开始打印 
    }  } //这里是调用打印的地方
    if(Convert.ToInt32(tabPage.Tag) != 0)
    {
    DBCon db = new DBCon();
    OleDbDataReader reader = db.GetLawsContentByID(Convert.ToInt32(tabPage.Tag));
    while(reader.Read())
    {
    System.IO.MemoryStream stream = new MemoryStream();

    string content = reader.GetString(2);
    byte[] byteArray = Encoding.Default.GetBytes(content);
    stream.Write(byteArray,0,byteArray.Length); StartPrint(stream);
    }
    reader.Close();
    }
      

  4.   

    我看这句好像有问题:string content = reader.GetString(2);
      

  5.   

    看你上面的代码,我估计你在string content = reader.GetString(2);时读不到数据,因为流的工作方式是前进前进再前进,你在用一个流创建另一个流的时,少了一句stream.Position = 0;我举个简单的例子吧(放到一个按钮的事件中就可以测试),你试试删掉我有注释的一句后结果是什么
    string szContext="Visual Studio 2005";
    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    Byte[] buffer=Encoding.Default.GetBytes(szContext);
    ms.Write(buffer,0,buffer.Length);
               
    ms.Position = 0;//你缺这句System.IO.StreamReader streamReader = new System.IO.StreamReader(ms);
    string text = streamReader.ReadToEnd();
    this.Text = text;
      

  6.   

    你是否为Printdocument绑定PrintPage事件,也就是需要如下:
    printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);其次,字画的位置有问题,如果是在页内,用(0,0)试试;如果是header或者rooter,需要另外操作。
      

  7.   

    刚仔细看了一下,和string content = reader.GetString(2)没关系(我误为这个reader是一个流的阅读器);另外纠正一下我上面的语法,是流创建一个阅读器.但你出错的原因和我描述的一样
      

  8.   

    谢谢各位回答,jointan和Knight94说话好象有道理,只能测试一下才行了,现在下班在家没有打印机不知道能不能测试的.
      

  9.   

    既然已经读出来,为什么还用memorystream做一次倒手,直接传递string不是更简单。
      

  10.   

    Knight94(愚翁) 你好,读出来的目的就是要打印出来呀
      

  11.   

    按jointan的方法加了一个position就可以打印了,谢谢,但又有两个问题了需要解决:
    1.由于我在数据库中保存的内容是网页格式的内容,比如说<tr>这里是内容</tr>,打印的结果是原样打印出来,如何不将<tr>之类的打印出来呢,如<b></b>就打印成粗体等等.2.里面的汉字全部成了乱码,如何解决呢
      

  12.   

    to 1.由于我在数据库中保存的内容是网页格式的内容,比如说<tr>这里是内容</tr>,打印的结果是原样打印出来,如何不将<tr>之类的打印出来呢,如<b></b>就打印成粗体等等.如果是网页数据,需要进行转换,这方面你可以参看
    An extended RichTextBox to save and load "HTML lite" files
    http://www.codeproject.com/cs/miscctrl/htmlrichtextbox.asp然后再用抓屏的方式,进行打印。to 2.里面的汉字全部成了乱码,如何解决呢change
    System.IO.StreamReader streamReader=new StreamReader(this.streamToPrint); 
    with
    System.IO.StreamReader streamReader=new StreamReader(this.streamToPrint, Encoding.Default); 
      

  13.   

    Knight94(愚翁) 你好,你说的是对的,确实不需要再转一次流了,后来我改了直接打印,目前汉字的问题已经解决了,但HTML格式我再看看你给的地址,谢谢
      

  14.   

    你看看我刚写的(没有问题,调试通过),我想你打印的时候可能是内容打到了区域的外面:
    // 按下即可打印
     private void button1_Click(object sender, EventArgs e)
            {
                if (this.printDialog1.ShowDialog() == DialogResult.OK)
                {
                    if (this.stream == null)
                    {
                        this.stream = new System.IO.MemoryStream();
                    }
                    byte[] s = System.Text.Encoding.Default.GetBytes("hollo word".ToCharArray());
                    this.stream.Write(s, 0, s.Length);
                    this.printPreviewDialog1.Document = this.printDocument1;
                    this.printPreviewDialog1.ShowDialog();
                }
            }
    //打印
       private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                if (this.stream == null)
                {
                    MessageBox.Show("Have no content!");
                    return;
                }
                byte[] s = this.stream.GetBuffer();
                
                Graphics g = e.Graphics;
                Font f = new Font("宋体", 30);
                g.DrawString(System.Text.Encoding.Default.GetString(s), f, new SolidBrush(Color.Red), new PointF(10f, 10f));
               
            }
      

  15.   

    我这里用了printPreviewDialog 但其效果跟打印输出是没什么区别的!
      

  16.   

    to 如果是采用抓屏的方式打印,那如果一页超出一屏,那就麻烦了这就是你操作细节问题了,pagesize你是可以获得,那么richtextbox也是可以获得大小的。
      

  17.   

    feixue_XXXX  可以打印内容出来了,只是HTML的格式没办法转换过来,比如说 <b></b>显示成粗体的