我正在尝试使用 PrintDocument 对象将图像打印到打印机,但图像总是在右侧被切掉一点。
private PrintDocument _printDocument;
public void PrintCurrentPage()
{
_printPage = 1;
if (_printDocument != null)
{
try
{
_printDocument.Print();
}
catch
{
}
}
}
private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
int savePage = iv.Image.Page;
try
{
iv.Image.Page = _printPage;
_printPage++;
using (Image img = RasterImageConverter.ConvertToImage(iv.Image, ConvertToImageOptions.None))
{
if (LeadToolsHelper.CanRunPrintPreview)
{
e.Graphics.DrawImage(img, 0, 0);
}
}
e.HasMorePages = _printPage <= iv.Image.PageCount;
if (_printPage > iv.Image.PageCount)
_printPage = 1;
}
finally
{
iv.Image.Page = savePage;
}
}
我怀疑(但不是100%确定)这与利润率有关。我尝试添加以下内容但没有成功。
_printDocument.DefaultPageSettings.Margins.Top = 0;
_printDocument.DefaultPageSettings.Margins.Bottom = 0;
_printDocument.DefaultPageSettings.Margins.Left = 0;
_printDocument.DefaultPageSettings.Margins.Right = 0;
_printDocument.DefaultPageSettings.PrinterSettings.DefaultPageSettings.Margins.Top = 0;
_printDocument.DefaultPageSettings.PrinterSettings.DefaultPageSettings.Margins.Bottom = 0;
_printDocument.DefaultPageSettings.PrinterSettings.DefaultPageSettings.Margins.Left = 0;
_printDocument.DefaultPageSettings.PrinterSettings.DefaultPageSettings.Margins.Right = 0;
有什么想法可以让打印出来的内容适合页面/纸张尺寸而不是纸张打印吗?
我在 Microsoft 网站上找到了更多信息,最终得出以下内容来解决我的问题。它效果很好,图像现在完美地在页面边界内居中。
public class MyFancyPrintClass: UserControl
{
private PrintDocument _printDocument;
private PrintAction printAction = PrintAction.PrintToPrinter;
private int _printPage;
// Constructor
public MyFancyPrintClass()
{
InitializeComponent();
// Check if there is printer installed
try
{
if (PrinterSettings.InstalledPrinters != null && PrinterSettings.InstalledPrinters.Count > 0)
{
_printDocument = new PrintDocument();
_printDocument.PrintPage += new PrintPageEventHandler(PrintDocument_PrintPage);
_printDocument.BeginPrint += new PrintEventHandler(PrintDocument_BeginPrint);
}
else
_printDocument = null;
}
catch (Exception)
{
_printDocument = null;
}
}
// Methods
public void PrintCurrentPage()
{
_printPage = 1;
if (_printDocument != null)
{
try
{
if (_printDocument.PrinterSettings.CanDuplex)
_printDocument.PrinterSettings.Duplex = Duplex.Vertical;
_printDocument.Print();
}
catch
{
}
}
}
// Printer events
private void PrintDocument_BeginPrint(object sender, PrintEventArgs e)
{
// Save our print action so we know if we are printing
// a preview or a real document.
printAction = e.PrintAction;
// Set some preferences, our method should print a box with any
// combination of these properties being true/false.
_printDocument.OriginAtMargins = false; //true = soft margins, false = hard margins
_printDocument.DefaultPageSettings.Landscape = false;
}
private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
// The "iv" object is an object I have that contains my Image. Change it to your control's name.
int savePage = iv.Image.Page;
try
{
iv.Image.Page = _printPage;
_printPage++;
Graphics g = e.Graphics;
RectangleF marginBounds = e.MarginBounds;
RectangleF printableArea = e.PageSettings.PrintableArea;
if (printAction == PrintAction.PrintToPreview)
g.TranslateTransform(printableArea.X, printableArea.Y);
int availableWidth = (int)Math.Floor(_printDocument.OriginAtMargins
? marginBounds.Width
: (e.PageSettings.Landscape
? printableArea.Height
: printableArea.Width));
int availableHeight = (int)Math.Floor(_printDocument.OriginAtMargins
? marginBounds.Height
: (e.PageSettings.Landscape
? printableArea.Width
: printableArea.Height));
using (Image img = RasterImageConverter.ConvertToImage(iv.Image, ConvertToImageOptions.None))
{
//g.DrawRectangle(Pens.Red, 0, 0, availableWidth - 1, availableHeight - 1);
g.DrawImage(img, 0, 0, availableWidth - 1, availableHeight - 1);
}
e.HasMorePages = _printPage <= iv.Image.PageCount;
if (_printPage > iv.Image.PageCount)
_printPage = 1;
}
finally
{
// Go to the next page if there is one
iv.Image.Page = savePage;
}
}
}