打印时出现CLR错误

时间:2018-02-09 17:56:36

标签: c# printing

我在c#中有一个项目,它在第11次打印时崩溃(总是)。否则效果很好。我浏览了网页,但似乎没有明确的答案。它在调试和释放模式下都是这样做的,它适用于x86。

正在打电话      var Printinstance = new Printing_Form();      Printinstance.PrintCard();

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

namespace COM_Caller_CS 
{ 
public class Printing_Form 
{ 
public void PrintCard() 
{
    PrintDocument ppd = new PrintDocument();

    ppd.DefaultPageSettings.PaperSize = new PaperSize("Card", 340, 210);
    ppd.PrintPage += new PrintPageEventHandler(ppd_PrintPage);
    ppd.Print();
}


private static void ppd_PrintPage(object sender, PrintPageEventArgs ev)
{
    string spare = "";
    int numvalue = Convert.ToInt32(Global.Global_PN);
    try
    {
        if (Global.GUPprintingposition != "Do Not Print")
        {
            TypeConverter converter2 = TypeDescriptor.GetConverter(typeof(Point));
            Point point2 = (Point)converter2.ConvertFromString(Global.GUPprintingposition);
            ev.Graphics.DrawString("Test Image", new Font("Times New Roman", 8, FontStyle.Regular), Brushes.Black, point2);
        }
        if (Global.printnumberbool == true)
        {
            if (Global.printingposition != "Do Not Print")
            {
                TypeConverter converter1 = TypeDescriptor.GetConverter(typeof(Point));
                Point point1 = (Point)converter1.ConvertFromString(Global.printingposition);
                ev.Graphics.DrawString(spare + (Global.Global_PrintPN), new Font("Times New Roman", 8, FontStyle.Regular), Brushes.Black, point1);
            }
        }
        if (Global.Global_pnamebool == true)
        {
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            stringFormat.LineAlignment = StringAlignment.Center;
            Rectangle rect1 = new Rectangle(0, 50, 340, 210);
            ev.Graphics.DrawString(Global.Global_pname, new Font("Times New Roman", 12, FontStyle.Regular), Brushes.Black, rect1, stringFormat);
        }

        if (Global.printlogobool == true)
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(Global.Global_Path);
            Point loc = new Point(5, 10);
            ev.Graphics.DrawImage(img, 10, 195, 114, 10);
        }
    }
    catch (Exception ex)

    {
        MessageBox.Show("Error, Application with now Close", ex.ToString());
        Application.Exit();
    }
   }
  }
 }

我做了建议的更改,我得到了#34; System.ExecutionEngineException"发生在System.Windows.Forms.DLL

1 个答案:

答案 0 :(得分:-1)

感谢您分享代码。从上面的片段我理解的一些事情是:

- 这看起来像一个Java代码 - 基于你的函数声明:)这就是它们在C中调用的内容。你不必在C中启动函数。

- 您的代码甚至可能正在运行,因为您在IDE中运行它而不是在纯C编译器上运行它。 Borland C等等。:)

- 因此,如果没有命令行错误并且这是运行时错误,那么知道“崩溃”是什么意思会很有趣。它是否顺利退出还是会给你一个运行时错误?尝试添加像“完成打印”之类的结束消息。 point2 *

- 我无法在你的代码段的第一个块中看到传递的参数。

- 但最重要的是你没有尝试连续打印。 Point loc = new Point(5,10); 参数给出页码 - 你称之为“loc ”。这是一个起点和终点 - 即页面范围。

- 似乎是在指示打印第5页到第10页。如果程序正在进行干净退出,则必须打印第5页到第10页并终止。

此外,这只是一种风格偏好,但我会通过将Font存储到变量Font1或类似的东西中来保持格式化数据的分离,然后将其作为参数传递。

* ev.Graphics.DrawString(Global.Global_pname, new Font(“Times New Roman”,12,FontStyle.Regular), Brushes.Black,rect1,stringFormat); *

可以写成:

* Font Font1 = new Font(“Times New Roman”,12,FontStyle.Regular);

ev.Graphics.DrawString(Global.Global_pname,Font1,Brushes.Black,rect1,stringFormat); *

更干净。

- 此外,您在此处提供页面范围 Point loc = new Point(5,10)。您是否打算允许用户稍后作为更新输入页面范围值?