在C#中的照片纸上打印图片

时间:2016-09-07 08:55:56

标签: c# printing

我必须使用DNP DS620打印机打印照片。图片是打印的,但不完全是。这是我的代码:

PrintController printController = new StandardPrintController();
pd.PrintController = printController;
pd.DefaultPageSettings.Margins = new Margins(0,0, 0, 0);
pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);

pd.PrintPage += (sndr, args) =>
{
    System.Drawing.Image i = System.Drawing.Image.FromFile(@"C:\Users\EMOSI\Desktop\photo36.jpg");

    //Adjust the size of the image to the page to print the full image without loosing any part of the image.
    System.Drawing.Rectangle m = args.MarginBounds;

    //Logic below maintains Aspect Ratio.
    if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height) // image is wider
    {
         m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width)-20;
         m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height) +10 ; // ajouter +
    }
    else
    {
         m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height)-20;
    }

    //Calculating optimal orientation.
    pd.DefaultPageSettings.Landscape = m.Width > m.Height;

    //Putting image in center of page.
    m.Y = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Height - m.Height) / 2);
    m.X = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Width - m.Width) / 2)-10; //chiffre bon
    args.Graphics.DrawImage(i, m);
};
pd.Print();

如何打印完整图片?使用此代码,图片在左侧剪切。纸张尺寸为6x4,可通过Windows打印。

1 个答案:

答案 0 :(得分:0)

我找到了答案。这是代码,对于那些感兴趣的人。

PrintDocument pd = new PrintDocument();
PrintController printController = new StandardPrintController();
pd.PrintController = printController;
pd.DefaultPageSettings.Margins = new Margins(0,0, 0, 0);
pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 17);

pd.PrintPage += (sndr, args) =>
{
     System.Drawing.Image i = System.Drawing.Image.FromFile(@"C:\Users\EMOSI\Desktop\photo36.jpg");

     //Adjust the size of the image to the page to print the full image without loosing any part of the image.
     System.Drawing.Rectangle m = args.MarginBounds;

     //Logic below maintains Aspect Ratio.
     if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height) // image is wider
     {
          m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width)-20;
          m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height) +5; // ajouter +
     }
     else
     {
          m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height);
     }

     //Calculating optimal orientation.
     pd.DefaultPageSettings.Landscape = m.Width > m.Height;

     //Putting image in center of page.
     m.Y = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Height - m.Height) / 2);
     m.X = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Width - m.Width) / 2) - 2; //chiffre bon
     args.Graphics.DrawImage(i, m);
};
pd.Print();