Reputation:
I want to get Maximum Dpi of installed or selected printer. I tried
PrinterSettings ps = new PrinterSettings();
MessageBox.Show(ps.PrinterResolutions.ToString());
and I get this output: System.Drawing.Printing.PrinterSettings+PreinterResolutionCollection (The desired output is 600x600).
Upvotes: 3
Views: 5920
Reputation: 82186
This is the one I use (.NET 2.0, so without Linq)
public static int CompareResolutions(System.Drawing.Printing.PrinterResolution y, System.Drawing.Printing.PrinterResolution x)
{
if (x.X*x.Y > y.X*y.Y)
return 1;
else if (x.X * x.Y < y.X * y.Y)
return -1;
return 0;
}
public static System.Drawing.Printing.PrinterResolution GetMaxResolution(System.Drawing.Printing.PrintDocument pd)
{
return GetMaxResolution(pd.PrinterSettings);
}
public static System.Drawing.Printing.PrinterResolution GetMaxResolution(System.Drawing.Printing.PrinterSettings ps)
{
System.Drawing.Printing.PrinterResolution prMax = null;
System.Collections.Generic.List<System.Drawing.Printing.PrinterResolution> ls = new System.Collections.Generic.List<System.Drawing.Printing.PrinterResolution>();
for (int i = 0; i < ps.PrinterResolutions.Count; ++i)
{
System.Drawing.Printing.PrinterResolution pres = ps.PrinterResolutions[i];
ls.Add(pres);
} // Next i
ls.Sort(CompareResolutions);
if (ls.Count > 0)
prMax = ls[0];
ls.Clear();
ls = null;
return prMax;
}
Upvotes: 0
Reputation: 8650
Printer resolutions is a collection, you will need to iterate through it to read all of the available resolutions, something like:
foreach (string installedPrinter in PrinterSettings.InstalledPrinters)
{
var ps = new PrinterSettings { PrinterName = installedPrinter };
var maxResolution = ps.PrinterResolutions.Cast<PrinterResolution>().OrderByDescending(pr => pr.X).First();
Console.WriteLine("{0}: {1}x{2}", installedPrinter, maxResolution.X, maxResolution.Y);
}
Or to display all printers and all of their PrinterResolutions
foreach (string installedPrinter in PrinterSettings.InstalledPrinters)
{
var ps = new PrinterSettings { PrinterName = installedPrinter };
foreach (PrinterResolution printerResolution in ps.PrinterResolutions)
{
var tpl = printerResolution.Kind == PrinterResolutionKind.Custom ? "{0}: {1} ({2}x{3})" : "{0}: {1}";
Console.WriteLine(tpl, installedPrinter, printerResolution.Kind, printerResolution.X, printerResolution.Y);
}
}
Upvotes: 0
Reputation: 9214
Using LINQ:
PrinterSettings ps = new PrinterSettings();
var maxResolution = ps.PrinterResolutions.OfType<PrinterResolution>()
.OrderByDescending(r => r.X)
.ThenByDescending(r => r.Y)
.First();
MessageBox.Show(String.Format("{0}x{1}", maxResolution.X, maxResolution.Y));
Upvotes: 4
Reputation: 8628
It looks like PrinterResolutions is a collection and your trying to convert it to a string value.
Upvotes: 0