C#代码获取已安装或选定打印机的最大dpi

时间:2012-12-05 09:57:08

标签: c# printing dpi

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

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

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

4 个答案:

答案 0 :(得分: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));

答案 1 :(得分:0)

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

答案 2 :(得分: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);
    }       
}

答案 3 :(得分: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;
}