C# code to get maximum dpi of installed or selected printer

醉酒当歌 提交于 2020-01-23 13:29:11

问题


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).


回答1:


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



回答2:


It looks like PrinterResolutions is a collection and your trying to convert it to a string value.




回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/13720470/c-sharp-code-to-get-maximum-dpi-of-installed-or-selected-printer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!