2011-03-15 60 views
1

我有以下C#代碼打印8x6照片在桌面應用程序。照片打印在C#

它的工作原理與普通信紙尺寸的紙張普通打印機上。
但我的客戶端使用柯達打印機與8x6紙,照片打印,但它們的大小是不同的,他們沒有完全8x6尺寸打印,我做錯了什麼。

是否有人可以指導我在正確的方向。

public void Print(List ListToBePrinted) 
{ 
    PrintDialog SelectedPrinter = new PrintDialog(); 
    if (SelectedPrinter.ShowDialog() == true) 
    { 
     PrintCapabilities printerCapabilities = SelectedPrinter.PrintQueue.GetPrintCapabilities(); 
     Size PageSize = new Size(printerCapabilities.PageImageableArea.ExtentWidth, printerCapabilities.PageImageableArea.ExtentHeight); 
     Size PrintableImageSize = new Size(); 
     foreach (Uri aUri in ListToBePrinted) 
     { 
      DrawingVisual drawVisual = new DrawingVisual(); 
      ImageBrush imageBrush = new ImageBrush(); 
      imageBrush.ImageSource = new BitmapImage(aUri); 
      imageBrush.Stretch = Stretch.Fill; 
      imageBrush.TileMode = TileMode.None; 
      imageBrush.AlignmentX = AlignmentX.Center; 
      imageBrush.AlignmentY = AlignmentY.Center; 
      if (imageBrush.ImageSource.Width > imageBrush.ImageSource.Height) 
       PrintableImageSize = new Size(768, 576); //8x6 
      else PrintableImageSize = new Size(576, 768); //6x8 
      double xcor = 0; double ycor = 0; 
      if (imageBrush.ImageSource.Width > imageBrush.ImageSource.Height) 
      { 
       if ((PageSize.Width - PrintableImageSize.Height) > 0) 
        xcor = (PageSize.Width - PrintableImageSize.Height)/2; 
       if ((PageSize.Height - PrintableImageSize.Width) > 0) 
        ycor = (PageSize.Height - PrintableImageSize.Width)/2; 
      } 
      else 
      { 
       if ((PageSize.Width - PrintableImageSize.Width) > 0) 
        xcor = (PageSize.Width - PrintableImageSize.Width)/2; 
       if ((PageSize.Height - PrintableImageSize.Height) > 0) 
        ycor = (PageSize.Height - PrintableImageSize.Height)/2; 
      } 
      using (DrawingContext drawingContext = drawVisual.RenderOpen()) 
      { 
       if (imageBrush.ImageSource.Width > imageBrush.ImageSource.Height) 
       { 
        drawingContext.PushTransform(new RotateTransform(90, PrintableImageSize.Width/2, PrintableImageSize.Height/2)); 
       } 
       drawingContext.DrawRectangle(imageBrush, null, new Rect(xcor, ycor, PrintableImageSize.Width, PrintableImageSize.Height)); 
      } 
      SelectedPrinter.PrintVisual(drawVisual, "Print"); 
     } 
    } 
} 
+0

@digEmAll:這是一些快速的編輯! – Nick 2011-03-15 16:00:58

+0

@Nick:上帝保佑Visual Studio中自動格式功能;-) – digEmAll 2011-03-15 16:01:59

回答

0

結帳的HardMarginX和HardMarginY值,如果你能找到他們。

我在我的應用程序之一(其中e是一個PrintPageEventArgs)這個代碼

e.Graphics.DrawImage(nextImage, e.PageSettings.PrintableArea.X - e.PageSettings.HardMarginX, e.PageSettings.PrintableArea.Y - e.PageSettings.HardMarginY, e.PageSettings.Landscape ? e.PageSettings.PrintableArea.Height : e.PageSettings.PrintableArea.Width, e.PageSettings.Landscape ? e.PageSettings.PrintableArea.Width : e.PageSettings.PrintableArea.Height); 

我的打印功能是有點原始,但也許你可以根據硬盤的利潤調整XCOR和YCOR。

+0

我可以用0,0 XCOR和YCOR如果我想打印完整的網頁?我嘗試過,但它似乎沒有工作 – vinee 2011-03-16 13:48:21

+0

我試過0,0 XCOR和YCOR和所有的利潤爲0,即使再有照片周圍的微小的利潤。我怎樣才能獲得無邊距打印? – vinee 2011-03-16 16:55:44