Printing problems (need answer from Jeffrey Tan[MSFT])

P

pavel.mikhailyuk

I tried to send email to Jeffrey, but got error. So I post here.

Hi!
We are developing big project and have 1 very annoying problem:
"Handle
is invalid" error while printing to HP Photosmart 335 (photoprinter).

And I DIDN'T FIND ANY solution.

As I googled for 1 month, it's very rarely problem.
One of results is topic
http://www.developermania.com/newsgroups/item/24/RE_Printing_problem_with_2_0_Framework.aspx
Unfortunately, I can't read another messages in that topic (site
returns
error). Can you help me?

What we have:
- WinXP Pro + SP2;
- .NET Framework 2.0 + SPs;
- ASP.NET WebServices impersonated under user, which in Administrators
group;
- HP Photosmart 335 (photo printer by Hewlett Packard) with installed
soft and drivers
- Printing from web service method with code like PrintPostCard method
in TestPrint project in attach.
- Webservice called by another Administrator user program locally or
remotely.

What happens:
From time to time we got exception
System.ComponentModel.Win32Exception: Handle non valido
at
System.Drawing.Printing.StandardPrintController.OnStartPrint(PrintDocument
document, PrintEventArgs e)
at
System.Windows.Forms.PrintControllerWithStatusDialog.OnStartPrint(PrintDocument

document, PrintEventArgs e)
at System.Drawing.Printing.PrintController.Print(PrintDocument
document)
at System.Drawing.Printing.PrintDocument.Print()
at PrintTest.Program.PrintPostCard() in
D:\Programs\GPS\Test\PrintTest\Program.cs:line 113
at PrintTest.Program.Main(String[] args) in
D:\Programs\GPS\Test\PrintTest\Program.cs:line 35

On English WinXP exception title is "Handle is invalid".
Sometime printer renaming (!!!) helps, sometime we must delete
\recreate
printer, sometime IISRESET helps.
Attach is console application with code very close to web service
code,
where problem appears in same way.

Can you help us: tell me solution or workaround (may be we should user
WinAPI?) or what another posters decided to do in that topick on
www.developermania.com/ ?

Upd: I can't add attach via groups.google, so this is Program.cs:
======
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;

namespace PrintTest
{
class Program
{
private Bitmap _image;
private string _printerName;

static void Main(string[] args)
{
if (args.Length < 1)
{
PrintUsage();
return;
}
string path = args[0];

string printerName = null;
if (args.Length > 1)
printerName = args[1];

string errorMessage = null;

try
{
using (Bitmap image = (Bitmap)Bitmap.FromFile(path))
{
Console.WriteLine("image.Width = {0}, image.Height = {1},
printerName = {2}", image.Width, image.Height, printerName);
Program program = new Program(image, printerName);
errorMessage = program.PrintPostCard();
}
}
catch (Exception e)
{
errorMessage = e.ToString();
}

if (string.IsNullOrEmpty(errorMessage))
{
Console.WriteLine("Printed.");
}
else
{
Console.WriteLine("Error!\nDetails: {0}", errorMessage);
}
}

private static void PrintUsage()
{
string applicationName =
AppDomain.CurrentDomain.SetupInformation.ApplicationName;

Console.WriteLine("Usage:");
Console.WriteLine("{0} ImageFile [PrinterName]",
applicationName);
}

private Program(Bitmap image, string printerName)
{
_image = image;
_printerName = printerName;
}

protected bool SetPrinter(PrinterSettings printerSettings)
{
if (string.IsNullOrEmpty(_printerName))
{
foreach (string installedPrinter in
PrinterSettings.InstalledPrinters)
{
printerSettings.PrinterName = installedPrinter;
Console.WriteLine("installedPrinter = {0}, IsDefaultPrinter
= {1}, printerSettings.PrinterName = {2}",
installedPrinter, printerSettings.IsDefaultPrinter,
printerSettings.PrinterName);
if (printerSettings.IsDefaultPrinter)
return true;
}
}
else
{
printerSettings.PrinterName = _printerName;
Console.WriteLine("printerSettings.PrinterName = {0},
printerSettings.IsValid = {1}",
printerSettings.PrinterName, printerSettings.IsValid);
if (printerSettings.IsValid)
return true;
}
return false;
}

private string PrintPostCard()
{
using (PrintDocument printDocument = new PrintDocument())
{
if (!SetPrinter(printDocument.PrinterSettings))
return "Printer not found";

if ((double)((double)_image.Width / (double)_image.Height) >=
1.0)
printDocument.DefaultPageSettings.Landscape = true;
else
printDocument.DefaultPageSettings.Landscape = false;

Console.WriteLine("Ratio: {0},
printDocument.DefaultPageSettings.Landscape = {1}",
_image.Width / _image.Height,
printDocument.DefaultPageSettings.Landscape);

printDocument.PrintPage += new
PrintPageEventHandler(printDocument_PrintPage);

try
{
printDocument.DocumentName = "aaa";
Console.WriteLine("printDocument.Print();");
printDocument.Print();
}
catch (InvalidPrinterException ex)
{
return ex.ToString();
}
}

return null;
}

private void printDocument_PrintPage(object sender,
PrintPageEventArgs e)
{
Console.WriteLine("_image.SetResolution(300, 300);");
_image.SetResolution(300, 300);
Console.WriteLine("e.Graphics.DrawImage(_image, new Point(0,
0));");
e.Graphics.DrawImage(_image, new Point(0, 0));
Console.WriteLine("e.HasMorePages = false;");
e.HasMorePages = false;
}
}
}

======
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top