2

我想获得已安装或选定打印机的最大 Dpi。我试过

PrinterSettings ps = new PrinterSettings();
        MessageBox.Show(ps.PrinterResolutions.ToString());

我得到这个输出:System.Drawing.Printing.PrinterSettings+PreinterResolutionCollection(所需的输出是 600x600)。

4

4 回答 4

4

使用 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));
于 2012-12-05T10:06:45.780 回答
0

看起来 PrinterResolutions 是一个集合,您试图将其转换为字符串值。

于 2012-12-05T10:02:29.847 回答
0

打印机分辨率是一个集合,您需要遍历它以读取所有可用的分辨率,例如:

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);
}

或显示所有打印机及其所有 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);
    }       
}
于 2012-12-05T10:05:34.270 回答
0

这是我使用的(.NET 2.0,所以没有 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;
}
于 2014-05-22T10:09:45.937 回答