EDN Admin
Well-known member
Dear all,
I have below code. But dont know why it throws the exception: {System.InvalidOperationException: Object is currently in use elsewhere.<br/>
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)<br/>
at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)<br/>
at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y, StringFormat format)<br/>
at POS.IO.Receipt.PrintBuffer.printDoc_PrintPage1(Object sender, PrintPageEventArgs e) in C:vs2010SingleStore_RestaurantPOSPOS.IOReceiptPrint_Buffer.cs:line 517<br/>
at System.Drawing.Printing.PrintDocument.OnPrintPage(PrintPageEventArgs e)<br/>
at System.Drawing.Printing.PrintDocument._OnPrintPage(PrintPageEventArgs e)<br/>
at System.Drawing.Printing.PrintController.PrintLoop(PrintDocument document)<br/>
at System.Drawing.Printing.PrintController.Print(PrintDocument document)<br/>
at System.Drawing.Printing.PrintDocument.Print()<br/>
at POS.IO.Receipt.PrintBuffer.printToWindowsPrinter() in C:vs2010SingleStore_RestaurantPOSPOS.IOReceiptPrint_Buffer.cs:line 469<br/>
at POS.IO.Receipt.PrintBuffer.printToLocalPrinter(Boolean printLogo) in C:vs2010SingleStore_RestaurantPOSPOS.IOReceiptPrint_Buffer.cs:line 562<br/>
at POS.IO.Receipt.PrintBuffer.Print(Boolean printLogo) in C:vs2010SingleStore_RestaurantPOSPOS.IOReceiptPrint_Buffer.cs:line 639<br/>
at POS.IO.Receipt.ReceiptManager.Print(IPrintable printDoc, Boolean printLogo, TransactionSummaryVO transVO) in C:vs2010SingleStore_RestaurantPOSPOS.IOReceiptReceiptManager.cs:line 180<br/>
at POS.IO.Receipt.ReceiptManager.Print(IPrintable printDoc, Boolean printLogo) in C:vs2010SingleStore_RestaurantPOSPOS.IOReceiptReceiptManager.cs:line 170<br/>
at POS.IO.Receipt.ReceiptManager.Print(IPrintable printDoc) in C:vs2010SingleStore_RestaurantPOSPOS.IOReceiptReceiptManager.cs:line 197<br/>
at POS.IO.Receipt.ReceiptManager.printEvent(EventVO eventVO, CashBreakDownVO cashVO, String path, Boolean printTwoColumns) in C:vs2010SingleStore_RestaurantPOSPOS.IOReceiptReceiptManager.cs:line 106<br/>
at POS.IO.PrintManager.printEvent(EventVO eventVO, CashBreakDownVO cashVO) in C:vs2010SingleStore_RestaurantPOSPOS.IOPrintManager.cs:line 134<br/>
at POS.Engine.POS_Engine.printDeclareFloatEvent() in C:vs2010SingleStore_RestaurantPOSPOS.EnginePOS_Engine.cs:line 4881}
// -----------------------------------------------------------------------------------------------
private void printToWindowsPrinter()<br/>
{<br/>
using (this.printDoc = new PrintDocument())<br/>
{<br/>
// printDoc1_PrintPage is for new customer<br/>
printFont = new Font("Arial Narrow", 10);<br/>
this.printDoc.PrinterSettings.PrinterName = InfoCache.receipt_printer;<br/>
PageSettings pageSettings = printDoc.DefaultPageSettings;<br/>
pageSettings.Margins = new Margins(0, 0, 0, 0);<br/>
printDoc.DefaultPageSettings = pageSettings;<br/>
this.printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage1);<br/>
this.printDoc.Print();<br/>
<br/>
}<br/>
}
private void printDoc_PrintPage1(object sender, PrintPageEventArgs e)<br/>
{<br/>
float leftMargin = e.MarginBounds.Left;<br/>
float topMargin = e.MarginBounds.Top;<br/>
int yPos = 0;<br/>
if (PropertiesVO.PRINT_LOGO)<br/>
{<br/>
if (this.printLogo(e.Graphics))<br/>
yPos = (int)(1.6 * Print_Param.logoImage_Height + 2);<br/>
}<br/>
IntPtr hDC = new IntPtr();<br/>
hDC = e.Graphics.GetHdc();
for (int i = 0; i < this.printBuffer.Count; i++)<br/>
{<br/>
string aStr = this.printBuffer;
if (aStr.StartsWith(PropertiesVO.sgnrBarCode))<br/>
{<br/>
string barCode = aStr.Substring(PropertiesVO.sgnrBarCode.Length);<br/>
this.printBarCode(hDC, yPos, "*" + barCode + "*");<br/>
yPos += Print_Param.barCodeFont_Height;<br/>
}<br/>
else if (aStr.Trim().StartsWith(PropertiesVO.sgnrFontBold))<br/>
{<br/>
int length = Print_Param.lineWidth;<br/>
string bStr = aStr.Replace(PropertiesVO.sgnrFontBold, "");
if (bStr.Length < Print_Param.lineWidth)<br/>
length = bStr.Length;
e.Graphics.DrawString(bStr, printFont, Brushes.Black,<br/>
leftMargin, yPos, new StringFormat());<br/>
//PrintBuffer.TextOut(hDC, 0, yPos, bStr, length);<br/>
//PrintBuffer.TextOut(hDC, 1, yPos, bStr, length);<br/>
yPos += Print_Param.lineHeight;<br/>
}<br/>
else<br/>
{<br/>
if (aStr.Length > Print_Param.lineWidth)<br/>
{<br/>
aStr = aStr.Substring(0, Print_Param.lineWidth);<br/>
}<br/>
e.Graphics.DrawString(aStr, printFont, Brushes.Black, <br/>
leftMargin, yPos, new StringFormat()); // Throw exception here <br/>
//PrintBuffer.TextOut(hDC, 0, yPos, aStr, aStr.Length);<br/>
yPos += Print_Param.lineHeight;<br/>
}<br/>
}
View the full article
I have below code. But dont know why it throws the exception: {System.InvalidOperationException: Object is currently in use elsewhere.<br/>
at System.Drawing.Graphics.CheckErrorStatus(Int32 status)<br/>
at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)<br/>
at System.Drawing.Graphics.DrawString(String s, Font font, Brush brush, Single x, Single y, StringFormat format)<br/>
at POS.IO.Receipt.PrintBuffer.printDoc_PrintPage1(Object sender, PrintPageEventArgs e) in C:vs2010SingleStore_RestaurantPOSPOS.IOReceiptPrint_Buffer.cs:line 517<br/>
at System.Drawing.Printing.PrintDocument.OnPrintPage(PrintPageEventArgs e)<br/>
at System.Drawing.Printing.PrintDocument._OnPrintPage(PrintPageEventArgs e)<br/>
at System.Drawing.Printing.PrintController.PrintLoop(PrintDocument document)<br/>
at System.Drawing.Printing.PrintController.Print(PrintDocument document)<br/>
at System.Drawing.Printing.PrintDocument.Print()<br/>
at POS.IO.Receipt.PrintBuffer.printToWindowsPrinter() in C:vs2010SingleStore_RestaurantPOSPOS.IOReceiptPrint_Buffer.cs:line 469<br/>
at POS.IO.Receipt.PrintBuffer.printToLocalPrinter(Boolean printLogo) in C:vs2010SingleStore_RestaurantPOSPOS.IOReceiptPrint_Buffer.cs:line 562<br/>
at POS.IO.Receipt.PrintBuffer.Print(Boolean printLogo) in C:vs2010SingleStore_RestaurantPOSPOS.IOReceiptPrint_Buffer.cs:line 639<br/>
at POS.IO.Receipt.ReceiptManager.Print(IPrintable printDoc, Boolean printLogo, TransactionSummaryVO transVO) in C:vs2010SingleStore_RestaurantPOSPOS.IOReceiptReceiptManager.cs:line 180<br/>
at POS.IO.Receipt.ReceiptManager.Print(IPrintable printDoc, Boolean printLogo) in C:vs2010SingleStore_RestaurantPOSPOS.IOReceiptReceiptManager.cs:line 170<br/>
at POS.IO.Receipt.ReceiptManager.Print(IPrintable printDoc) in C:vs2010SingleStore_RestaurantPOSPOS.IOReceiptReceiptManager.cs:line 197<br/>
at POS.IO.Receipt.ReceiptManager.printEvent(EventVO eventVO, CashBreakDownVO cashVO, String path, Boolean printTwoColumns) in C:vs2010SingleStore_RestaurantPOSPOS.IOReceiptReceiptManager.cs:line 106<br/>
at POS.IO.PrintManager.printEvent(EventVO eventVO, CashBreakDownVO cashVO) in C:vs2010SingleStore_RestaurantPOSPOS.IOPrintManager.cs:line 134<br/>
at POS.Engine.POS_Engine.printDeclareFloatEvent() in C:vs2010SingleStore_RestaurantPOSPOS.EnginePOS_Engine.cs:line 4881}
// -----------------------------------------------------------------------------------------------
private void printToWindowsPrinter()<br/>
{<br/>
using (this.printDoc = new PrintDocument())<br/>
{<br/>
// printDoc1_PrintPage is for new customer<br/>
printFont = new Font("Arial Narrow", 10);<br/>
this.printDoc.PrinterSettings.PrinterName = InfoCache.receipt_printer;<br/>
PageSettings pageSettings = printDoc.DefaultPageSettings;<br/>
pageSettings.Margins = new Margins(0, 0, 0, 0);<br/>
printDoc.DefaultPageSettings = pageSettings;<br/>
this.printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage1);<br/>
this.printDoc.Print();<br/>
<br/>
}<br/>
}
private void printDoc_PrintPage1(object sender, PrintPageEventArgs e)<br/>
{<br/>
float leftMargin = e.MarginBounds.Left;<br/>
float topMargin = e.MarginBounds.Top;<br/>
int yPos = 0;<br/>
if (PropertiesVO.PRINT_LOGO)<br/>
{<br/>
if (this.printLogo(e.Graphics))<br/>
yPos = (int)(1.6 * Print_Param.logoImage_Height + 2);<br/>
}<br/>
IntPtr hDC = new IntPtr();<br/>
hDC = e.Graphics.GetHdc();
for (int i = 0; i < this.printBuffer.Count; i++)<br/>
{<br/>
string aStr = this.printBuffer;
if (aStr.StartsWith(PropertiesVO.sgnrBarCode))<br/>
{<br/>
string barCode = aStr.Substring(PropertiesVO.sgnrBarCode.Length);<br/>
this.printBarCode(hDC, yPos, "*" + barCode + "*");<br/>
yPos += Print_Param.barCodeFont_Height;<br/>
}<br/>
else if (aStr.Trim().StartsWith(PropertiesVO.sgnrFontBold))<br/>
{<br/>
int length = Print_Param.lineWidth;<br/>
string bStr = aStr.Replace(PropertiesVO.sgnrFontBold, "");
if (bStr.Length < Print_Param.lineWidth)<br/>
length = bStr.Length;
e.Graphics.DrawString(bStr, printFont, Brushes.Black,<br/>
leftMargin, yPos, new StringFormat());<br/>
//PrintBuffer.TextOut(hDC, 0, yPos, bStr, length);<br/>
//PrintBuffer.TextOut(hDC, 1, yPos, bStr, length);<br/>
yPos += Print_Param.lineHeight;<br/>
}<br/>
else<br/>
{<br/>
if (aStr.Length > Print_Param.lineWidth)<br/>
{<br/>
aStr = aStr.Substring(0, Print_Param.lineWidth);<br/>
}<br/>
e.Graphics.DrawString(aStr, printFont, Brushes.Black, <br/>
leftMargin, yPos, new StringFormat()); // Throw exception here <br/>
//PrintBuffer.TextOut(hDC, 0, yPos, aStr, aStr.Length);<br/>
yPos += Print_Param.lineHeight;<br/>
}<br/>
}
View the full article