(Leadtools.Controls)

フレームを表示

RasterImagePrinterクラス




メンバ 
RasterImageのプリントをサポートします。
構文
public class RasterImagePrinter 
'Declaration
 
Public Class RasterImagePrinter 
解説

RasterImagePrinterは、より簡単なRasterImageを印刷するプロセスを行うために、プロパティとメソッドを提供します。

.NET frameworkを用いた印刷は、PrintPageイベントにハンドラを追加することが必要です。そのイベントハンドラに、セットアップRasterImagePrinterクラスの新しいインスタンス、そのプロパティが印刷するためにそれにRasterImageを渡しているプリントメソッドと必要に応じてその後言うセットアップ、印刷するページ番号とPrintPageEventHandlerから得られるPrintPageEventArgsオブジェクト。

注意:RasterViewerCenterMode型は、バージョン15で名前を変えられました。代わりにRasterPaintAlignMode列挙体を使います。

サンプル

このサンプルはimageをロードして、それからプリンターにそれを送ります。プリントと関連した様々な長方形を使うことのサンプルのために、ImageRectangleを参照します。

Copy Code  
Imports Leadtools
Imports Leadtools.Controls
Imports Leadtools.Codecs
Imports Leadtools.Drawing

' The image we are printing
Private myImage As RasterImage = Nothing
' The current page number being printed
Private currentPrintPageNumber As Integer
<TestMethod>
Public Sub RasterImagePrinterExample()
   ' Check if there are printers installed on this machine
   If PrinterSettings.InstalledPrinters Is Nothing OrElse PrinterSettings.InstalledPrinters.Count < 1 Then
      MessageBox.Show("There are no printers installed on this machine")
      Return
   End If
   ' Load the image
   Using codecs As RasterCodecs = New RasterCodecs()
      Me.myImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif"))
   End Using

   ' Create the print document object
   Using document As PrintDocument = New PrintDocument()
      ' Setup the document pages
      document.PrinterSettings.MinimumPage = 1
      document.PrinterSettings.MaximumPage = Me.myImage.PageCount
      document.PrinterSettings.FromPage = 1
      document.PrinterSettings.ToPage = Me.myImage.PageCount

      Dim result As DialogResult = System.Windows.Forms.DialogResult.OK

      ' Select the printer
      Using printDlg As PrintDialog = New PrintDialog()
         printDlg.Document = document

         printDlg.AllowSomePages = True

         result = printDlg.ShowDialog()
      End Using

      ' Setup the page
      If result = System.Windows.Forms.DialogResult.OK Then
         Using pageSetupDlg As PageSetupDialog = New PageSetupDialog()
            pageSetupDlg.Document = document
            pageSetupDlg.ShowDialog()
         End Using
      End If

      If result = System.Windows.Forms.DialogResult.OK Then
         ' Add handlers for Begin/Print and End print events
         AddHandler document.BeginPrint, AddressOf document_BeginPrint
         AddHandler document.PrintPage, AddressOf document_PrintPage
         AddHandler document.EndPrint, AddressOf document_EndPrint

         ' Use the .NET print preview dialog
         Using printPreviewDlg As PrintPreviewDialog = New PrintPreviewDialog()
            printPreviewDlg.Document = document
            printPreviewDlg.WindowState = FormWindowState.Maximized
            result = printPreviewDlg.ShowDialog()
         End Using
      End If
   End Using

   ' Clean up
   Me.myImage.Dispose()
End Sub

Private Sub document_BeginPrint(ByVal sender As Object, ByVal e As PrintEventArgs)
   ' Reset the current page number
   ' Since we are using the print preview dialog, this event will be called twice (once
   ' to generate the print preview and once for actual printing). So, we must set this back
   ' to the first print page
   Dim document As PrintDocument = TryCast(sender, PrintDocument)
   Me.currentPrintPageNumber = document.PrinterSettings.FromPage
End Sub

Private Sub document_EndPrint(ByVal sender As Object, ByVal e As PrintEventArgs)
   ' Nothing to do here
End Sub

Private Sub document_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
   ' Get the print document object
   Dim document As PrintDocument = TryCast(sender, PrintDocument)

   ' Create an new LEADTOOLS image printer class
   Dim printer As RasterImagePrinter = New RasterImagePrinter()

   ' Set the document object so page calculations can be performed
   printer.PrintDocument = document

   ' We want to fit and center the image into the maximum print area
   printer.SizeMode = RasterPaintSizeMode.FitAlways
   printer.HorizontalAlignMode = RasterPaintAlignMode.Center
   printer.VerticalAlignMode = RasterPaintAlignMode.Center

   ' Account for FAX images that may have different horizontal and vertical resolution
   printer.UseDpi = True

   ' Print the whole image
   printer.ImageRectangle = Rectangle.Empty

   ' Use maximum page dimension ignoring the margins, this will be equivalant of printing
   ' using Windows Photo Gallery
   printer.PageRectangle = RectangleF.Empty
   printer.UseMargins = False

   ' Print the current page
   printer.Print(Me.myImage, Me.currentPrintPageNumber, e)

   ' Go to the next page
   Me.currentPrintPageNumber += 1

   ' Inform the printer whether we have more pages to print
   If Me.currentPrintPageNumber <= document.PrinterSettings.ToPage Then
      e.HasMorePages = True
   Else
      e.HasMorePages = False
   End If

   ' De-couple our PrintDocument from the RasterImagePrinter
   printer.PrintDocument = Nothing
End Sub

Public NotInheritable Class LEAD_VARS
Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
using Leadtools;
using Leadtools.Controls;
using Leadtools.Codecs;
using Leadtools.Drawing;

// The image we are printing
private RasterImage myImage = null;
// The current page number being printed
private int currentPrintPageNumber;
[TestMethod]
public void RasterImagePrinterExample()
{
   // Check if there are printers installed on this machine
   if (PrinterSettings.InstalledPrinters == null || PrinterSettings.InstalledPrinters.Count < 1)
   {
      MessageBox.Show("There are no printers installed on this machine");
      return;
   }
   // Load the image
   using (RasterCodecs codecs = new RasterCodecs())
   {
      this.myImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif"));
   }

   // Create the print document object
   using (PrintDocument document = new PrintDocument())
   {
      // Setup the document pages
      document.PrinterSettings.MinimumPage = 1;
      document.PrinterSettings.MaximumPage = this.myImage.PageCount;
      document.PrinterSettings.FromPage = 1;
      document.PrinterSettings.ToPage = this.myImage.PageCount;

      DialogResult result = DialogResult.OK;

      // Select the printer
      using (PrintDialog printDlg = new PrintDialog())
      {
         printDlg.Document = document;

         printDlg.AllowSomePages = true;

         result = printDlg.ShowDialog();
      }

      // Setup the page
      if (result == DialogResult.OK)
      {
         using (PageSetupDialog pageSetupDlg = new PageSetupDialog())
         {
            pageSetupDlg.Document = document;
            pageSetupDlg.ShowDialog();
         }
      }

      if (result == DialogResult.OK)
      {
         // Add handlers for Begin/Print and End print events
         document.BeginPrint += new PrintEventHandler(document_BeginPrint);
         document.PrintPage += new PrintPageEventHandler(document_PrintPage);
         document.EndPrint += new PrintEventHandler(document_EndPrint);

         // Use the .NET print preview dialog
         using (PrintPreviewDialog printPreviewDlg = new PrintPreviewDialog())
         {
            printPreviewDlg.Document = document;
            printPreviewDlg.WindowState = FormWindowState.Maximized;
            result = printPreviewDlg.ShowDialog();
         }
      }
   }

   // Clean up
   this.myImage.Dispose();
}

private void document_BeginPrint(object sender, PrintEventArgs e)
{
   // Reset the current page number
   // Since we are using the print preview dialog, this event will be called twice (once
   // to generate the print preview and once for actual printing). So, we must set this back
   // to the first print page
   PrintDocument document = sender as PrintDocument;
   this.currentPrintPageNumber = document.PrinterSettings.FromPage;
}

private void document_EndPrint(object sender, PrintEventArgs e)
{
   // Nothing to do here
}

private void document_PrintPage(object sender, PrintPageEventArgs e)
{
   // Get the print document object
   PrintDocument document = sender as PrintDocument;

   // Create an new LEADTOOLS image printer class
   RasterImagePrinter printer = new RasterImagePrinter();

   // Set the document object so page calculations can be performed
   printer.PrintDocument = document;

   // We want to fit and center the image into the maximum print area
   printer.SizeMode = RasterPaintSizeMode.FitAlways;
   printer.HorizontalAlignMode = RasterPaintAlignMode.Center;
   printer.VerticalAlignMode = RasterPaintAlignMode.Center;

   // Account for FAX images that may have different horizontal and vertical resolution
   printer.UseDpi = true;

   // Print the whole image
   printer.ImageRectangle = Rectangle.Empty;

   // Use maximum page dimension ignoring the margins, this will be equivalant of printing
   // using Windows Photo Gallery
   printer.PageRectangle = RectangleF.Empty;
   printer.UseMargins = false;

   // Print the current page
   printer.Print(this.myImage, this.currentPrintPageNumber, e);

   // Go to the next page
   this.currentPrintPageNumber++;

   // Inform the printer whether we have more pages to print
   if (this.currentPrintPageNumber <= document.PrinterSettings.ToPage)
      e.HasMorePages = true;
   else
      e.HasMorePages = false;

   // De-couple our PrintDocument from the RasterImagePrinter
   printer.PrintDocument = null;
}

static class LEAD_VARS
{
public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
必要条件

ターゲットプラットホーム

参照

参照

RasterImagePrinterメンバ
Leadtools.Controls名前空間