PrinterSetup.OpenPrinter prints in draft (low resolution?) instead of p.ShowPrinterDialog?

Why when i print directly to the printer with OpenPrinter my prints are in low resolution (draft) while showing PrinterDialog the prints are ok and in high res ?

Var g As Graphics
Var p As New PrinterSetup

// clone to NSPrintInfo
dim info as new NSPrintInfoMBS(p.SetupString)

dim printers() as string = NSPrinterMBS.printerNames
dim printer as NSPrinterMBS = NSPrinterMBS.printerWithName(“OKI”)

// now set this printer
info.printer = printer

// and clone back
p.SetupString = info.SetupString

If StampaDiretta = “SI” Then
// Stampa direttamente
g = PrinterSetup.OpenPrinter(p)
End If
If StampaDiretta = “NO” Then
//Mostra il dialogo della stampante
g = p.ShowPrinterDialog
End If

If g <> Nil Then

Var X, Y As Integer
Var Inch As Double = 2.54
Var DPI_X As Integer = p.MaxHorizontalResolution // Get te Printer Resolution
Var DPI_Y As Integer = p.MaxHorizontalResolution // Get te Printer Resolution
g.FontName = “Times New Roman”
//Se si vuole stampare 2 cm da sinistra e 2,5 cm dall’alto
// Eliminare 0,4 cm da entrambi i lati
// X = (DPI_X * (2-0.4) / 2.54
//Y = (DPI_Y * (2.5-0.4) / 2.54

g.FontSize = 10
X = (DPI_X * (5.5-0.4) / 2.54)
Y = (DPI_Y * (0.65-0.4) / 2.54)
g.DrawString(p2tfCronologico.value, X, Y)
End If

You’re using Max…Resolution incorrectly. Set the Max resolution BEFORE you open the printer. That is a request to the printer for the resolution. Then after OpenPrinter, you can read the actual resolution with p.HorizontalResolution and p.VerticalResolution.

That should work ?
I’ve tried it but the result is always the same (poor quality as draft with direct print (OpenPrinter), great quality using the ShowPrinterDialog panel first).

Var g As Graphics
Var p As New PrinterSetup
Var DPI_X As Integer = p.MaxHorizontalResolution // Get te Printer Resolution
Var DPI_Y As Integer = p.MaxHorizontalResolution // Get te Printer Resolution
// clone to NSPrintInfo
dim info as new NSPrintInfoMBS(p.SetupString)

dim printers() as string = NSPrinterMBS.printerNames
dim printer as NSPrinterMBS = NSPrinterMBS.printerWithName(“OKI”)

// now set this printer
info.printer = printer

// and clone back
p.SetupString = info.SetupString

If StampaDiretta = “SI” Then
// Stampa direttamente
g = PrinterSetup.OpenPrinter(p)
End If
If StampaDiretta = “NO” Then
//Mostra il dialogo della stampante
g = p.ShowPrinterDialog
End If

If g <> Nil Then

Var X, Y As Integer
Var Inch As Double = 2.54
g.FontName = “Times New Roman”
//Se si vuole stampare 2 cm da sinistra e 2,5 cm dall’alto
// Eliminare 0,4 cm da entrambi i lati
// X = (DPI_X * (2-0.4) / 2.54
//Y = (DPI_Y * (2.5-0.4) / 2.54

g.FontSize = 11
X = (DPI_X * (1.5-0.4) / 2.54)
Y = (DPI_Y * (2.1-0.4) / 2.54)
g.DrawString(p1lblStudio.value, X, Y)
End If

You still have it backwards

p.MaxHorizontalResolution = 600
p.MaxVerticalResolution = 600
g = PrinterSetup.OpenPrinter( p )
DPI_X = p.HorizontalResolution
DPI_Y = p.VerticalResolution

So this should be correct ?

Var g As Graphics
Var p As New PrinterSetup
Var DPI_X As Integer
Var DPI_Y As Integer
p.MaxHorizontalResolution = 600
p.MaxVerticalResolution = 600
// clone to NSPrintInfo
dim info as new NSPrintInfoMBS(p.SetupString)

dim printers() as string = NSPrinterMBS.printerNames
dim printer as NSPrinterMBS = NSPrinterMBS.printerWithName(“OKI”)

// now set this printer
info.printer = printer

// and clone back
p.SetupString = info.SetupString

If StampaDiretta = “SI” Then
// Stampa direttamente
g = PrinterSetup.OpenPrinter(p)
End If
If StampaDiretta = “NO” Then
//Mostra il dialogo della stampante
g = p.ShowPrinterDialog
End If
DPI_X = p.HorizontalResolution
DPI_Y = p.VerticalResolution
If g <> Nil Then

Var X, Y As Integer
Var Inch As Double = 2.54
g.FontName = “Times New Roman”
//Se si vuole stampare 2 cm da sinistra e 2,5 cm dall’alto
// Eliminare 0,4 cm da entrambi i lati
// X = (DPI_X * (2-0.4) / 2.54
//Y = (DPI_Y * (2.5-0.4) / 2.54

g.FontSize = 11
X = (DPI_X * (1.5-0.4) / 2.54)
Y = (DPI_Y * (2.1-0.4) / 2.54)
g.DrawString(p1lblStudio.value, X, Y)
End If

That looks correct.

Thanks Tim for your help.
It’s odd but inserting:

p.MaxHorizontalResolution = 360
p.MaxVerticalResolution = 180
(these are the max value of my Oki 24-pin printer)
the printer prints at very little characters far from readable.
If delete that leaving only this code below when in
StampaDiretta = “NO” with

g = p.ShowPrinterDialog

all works properly
when in StampaDiretta = “SI” with

g = PrinterSetup.OpenPrinter(p)

again the printer prints in draft
why ?

Var g As Graphics
Var p As New PrinterSetup

Var DPI_X As Integer
Var DPI_Y As Integer
//p.MaxHorizontalResolution = 360
//p.MaxVerticalResolution = 180

// clone to NSPrintInfo
dim info as new NSPrintInfoMBS(p.SetupString)

dim printers() as string = NSPrinterMBS.printerNames
dim printer as NSPrinterMBS = NSPrinterMBS.printerWithName(“OKI”)

// now set this printer
info.printer = printer

// and clone back
p.SetupString = info.SetupString

If StampaDiretta = “SI” Then
// Stampa direttamente
g = PrinterSetup.OpenPrinter(p)
End If
If StampaDiretta = “NO” Then
//Mostra il dialogo della stampante
g = p.ShowPrinterDialog
End If

DPI_X = p.HorizontalResolution
DPI_Y = p.VerticalResolution

If g <> Nil Then

Var X, Y As Integer
Var Inch As Double = 2.54
'Var DPI_X As Integer = p.MaxHorizontalResolution // Get te Printer Resolution
'Var DPI_Y As Integer = p.MaxHorizontalResolution // Get te Printer Resolution
g.FontName = “Times New Roman”
//Se si vuole stampare 2 cm da sinistra e 2,5 cm dall’alto
// Eliminare 0,4 cm da entrambi i lati
// X = (DPI_X * (2-0.4) / 2.54
//Y = (DPI_Y * (2.5-0.4) / 2.54

g.FontSize = 11
X = (DPI_X * (1.5-0.4) / 2.54)
Y = (DPI_Y * (2.1-0.4) / 2.54)
g.DrawString(p1lblStudio.value, X, Y)

You need to scale your font size as well as your coordinates. Something like

g.FontSize = 11 * (DPI_X / 72)

Tim why / 72 ?
And only for DPI_X ?
or also for DPI_Y ?
Btw why when pass from dialog it prints ok ?
Because in that case it takes the values directly from the driver of the printer (setup at 360x180) ?
and very last: do you suggest to delete
//p.MaxHorizontalResolution = 360
//p.MaxVerticalResolution = 180
(Sorry for these multiple questions.)

72 is the default resolution. If you don’t request anything else, that’s what you get. An 11 point font at 72 DPI will be much larger than 11 points at 360 DPI, so you have to scale it up to get the same size.

You’ll have to try DPI_X and DPI_Y to see which gives you the best results. All the printers I use have the same X and Y values.

What DPI do you get back from the dialog? I only ever use saved printer setups, so I can’t really help you with that. I suspect you’re getting 72 though.

Do not remove the max resolution values.

Do i have to detect from code ?
From the printer panel i see 360x180

Assuming the max resolution of my printer is 360x180 i should insert these value, is it correct ?

p.MaxHorizontalResolution = 360
p.MaxVerticalResolution = 180

Yes. After ShowPrinterDialog, what are the values in DPI_X and DPI_Y?

It would actually be better to pass -1 for the Max, so you’re not making any assumptions about the printer. You won’t have to recode it if you use another printer.

Tim how to catch that value ?
Now works better adjusting as you’ve suggested
g.FontSize = 11 * (DPI_X / 72)
But when in “Stampa diretta” mode the characters are in the correct size now but draft.
In “NON Stampa diretta” mode the characters are perfect.

Try this:

Var p As New PrinterSetup

If p.ShowPageSetupDialog Then

  Var g As Graphics = p.ShowPrinterDialog
  Var scaleFactor As Double = p.HorizontalResolution / 72

  If g <> Nil Then

    g.FontSize = 11 * scaleFactor
    g.DrawString("Hello World", 50, 50)

  End If

End If

The issue (draft fonts) comes only when i’m in this scenario:

If StampaDiretta = “SI” Then
// Stampa direttamente
g = PrinterSetup.OpenPrinter(p)
End If

With StampaDiretta = “SI” i’d like to print without open the printer dialog to print directly …
With StampaDiretta = “NO” i’d like to print opening first the printer dialog, before print
The issue (draft font) as said happens only when i’ve the value “SI” to StampaDiretta

I see in the debug these values:
DPI_X is 360
DPI_Y = 0

I’ve tried also with the code suggested by @Martin_T (deleting the p.ShowPrinterDialog cause in the StampaDiretta = “SI” i need to print without show the printer panel) but the issue is still here …

I’ve also tried with 360 DPI as shown in debug:
Var scaleFactor As Double = p.HorizontalResolution / 360

in this case all characters are smaller and the same in StampaDiretta = “SI” are in draft while in StampaDiretta = “NO” are good but smaller too

Why don’t you set the printers Resolution to the Maximum?

Var p As New PrinterSetup

If p.ShowPageSetupDialog Then

  p.MaximumHorizontalResolution = -1
  p.MaximumVerticalResolution = -1

  Var g As Graphics = p.ShowPrinterDialog
  Var scaleFactor As Double = p.HorizontalResolution / 72

  If g <> Nil Then

    g.FontSize = 11 * scaleFactor
    g.DrawString("Hello World", 50, 50)

  End If

End If

Yes i’ve done it.
Here’s the code:

Var g As Graphics
Var p As New PrinterSetup

Var DPI_X As Integer
Var DPI_Y As Integer

p.MaxHorizontalResolution = -1
p.MaxVerticalResolution = -1

// clone to NSPrintInfo
dim info as new NSPrintInfoMBS(p.SetupString)

dim printers() as string = NSPrinterMBS.printerNames
dim printer as NSPrinterMBS = NSPrinterMBS.printerWithName(“OKI”)

// now set this printer
info.printer = printer

// and clone back
p.SetupString = info.SetupString

If StampaDiretta = “SI” Then
// HERE THE CODE NOT WORKS THE CHARACTERS ARE SHOWN IN DRAFT
g = PrinterSetup.OpenPrinter(p)
End If

If StampaDiretta = “NO” Then
// HERE THE CODE WORKS PROPERLY I THINK
// BECAUSE IT TAKES THE CORRECT VALUES //FROM PRINTER ?

g = p.ShowPrinterDialog
End If

DPI_X = p.HorizontalResolution
DPI_Y = p.VerticalResolution
Var scaleFactor As Double = p.HorizontalResolution / 72

If g <> Nil Then

Var X, Y As Integer
Var Inch As Double = 2.54
g.FontName = “Times New Roman”
g.FontSize = 11 * scaleFactor
X = (DPI_X * (1.5-0.4) / 2.54)
Y = (DPI_Y * (2.1-0.4) / 2.54)
g.DrawString(p1lblStudio.value, X, Y)
End If

Is it possible that when use the Printer Dialog

g = p.ShowPrinterDialog

it overwrites some value and make all works properly ?
this is the value from Printer Dialog

Schermata 2020-09-27 alle 16.21.39

while when i’m in
g = PrinterSetup.OpenPrinter(p)

some value is missed ?
I’m really don’t know why in StampaDiretta= “SI” it still not works