Take the following steps to start a project and to add some code that prints an
image:
Start Visual Studio .NET. Choose File->New->Project… from the menu. In the New Project dialog box, choose either "Visual C# Projects" or "Visual Basic Projects" in the Projects Type List, and choose "Windows Application " in the Templates List. Type the project name as "Print a Real Image Size" in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK . In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Reference…" from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and browse to Leadtools For .NET "\LEAD Technologies\LEADTOOLS 15\Bin\DotNet\Win32 " folder and select the following DLLs: Leadtools.dll Leadtools.Codecs.dll Leadtools.WinForms.dll
Make sure Form1 is in design view. Go to the toolbox (View->Toolbox) and Drag and drop an instance of RasterImageViewer to the form. If you do not have RasterImageViewer in your toolbox, select Tools->Add Remove Toolbox Items from the menu. Click Browse and then select Leadtools.WinForms.dll from "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Bin\DotNet\Win32" and then click Open and then click OK. From the toolbox (View->Toolbox), add a two Button controls. Change the text property for the controls to "Load" and "Print" Switch to Form1 code view (right-click Form1 in the solution explorer then select View Code ) and add the following lines at the beginning of the file: [Visual Basic]
Imports System.Drawing.Printing Imports Leadtools Imports Leadtools.Codecs Imports Leadtools.WinForms
using System.Drawing.Printing; using Leadtools; using Leadtools.Codecs; using Leadtools.WinForms;
import System.Drawing.Printing.*; import Leadtools.*; import Leadtools.Codecs.*; import Leadtools.WinForms.*;
Add an event handler to the Form1 Load event and add the following code: [Visual Basic]
Dim codecs As RasterCodecs Dim document As PrintDocument Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Initialize a new RasterCodecs object RasterCodecs.Startup() codecs = New RasterCodecs() ' Check if we have a printer on this machine If (Not PrinterSettings.InstalledPrinters Is Nothing) AndAlso (PrinterSettings.InstalledPrinters.Count > 0) Then ' initialize a new print document object document = New PrintDocument() AddHandler document.PrintPage, AddressOf printDocument_PrintPage Else MessageBox.Show("Could not find a printer!") End If End Sub Private Sub printDocument_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Dim image As RasterImage = RasterImageViewer1.Image ' find the image size in pixels at this resolution Dim destRectangle As New Rectangle( _ e.MarginBounds.Left, _ e.MarginBounds.Top, _ CType((CType(image.ImageWidth, Single) / CType(image.XResolution, Single) * 100.0F), Integer), _ CType((CType(image.ImageHeight, Single) / CType(image.YResolution, Single) * 100.0F), Integer)) Dim props As RasterPaintProperties = RasterPaintProperties.Default props.PaintEngine = RasterPaintEngine.GdiPlus image.Paint(e.Graphics, Rectangle.Empty, Rectangle.Empty, destRectangle, e.MarginBounds, props) End Sub
RasterCodecs codecs; PrintDocument document; private void Form1_Load(object sender, EventArgs e) { // Initialize a new RasterCodecs object RasterCodecs.Startup(); codecs = new RasterCodecs(); // Check if we have a printer on this machine if(PrinterSettings.InstalledPrinters != null && PrinterSettings.InstalledPrinters.Count > 0) { // initialize a new print document object document = new PrintDocument(); document.PrintPage += new PrintPageEventHandler(printDocument_PrintPage); } else MessageBox.Show("Could not find a printer!"); } private void printDocument_PrintPage(object sender, PrintPageEventArgs e) { RasterImage image = rasterImageViewer1.Image; // find the image size in pixels at this resolution Rectangle destRectangle = new Rectangle( e.MarginBounds.Left, e.MarginBounds.Top, (int)((float)image.ImageWidth / (float)image.XResolution * 100.0F), (int)((float)image.ImageWidth / (float)image.YResolution * 100.0F)); RasterPaintProperties props = RasterPaintProperties.Default; props.PaintEngine = RasterPaintEngine.GdiPlus; image.Paint(e.Graphics, Rectangle.Empty, Rectangle.Empty, destRectangle, e.MarginBounds, props); }
RasterCodecs codecs; PrintDocument document; private void Form1_Load(Object sender, System.EventArgs e) { // Initialize a new RasterCodecs object RasterCodecs.Startup(); codecs = new RasterCodecs(); // Check if we have a printer on this machine if (PrinterSettings.get_InstalledPrinters() != null && PrinterSettings.get_InstalledPrinters().get_Count() > 0) { // initialize a new print document object document = new PrintDocument(); document.add_PrintPage(new PrintPageEventHandler(printDocument_PrintPage)); } else MessageBox.Show("Could not find a printer!"); } private void printDocument_PrintPage(Object sender, PrintPageEventArgs e) { image = new RasterImage(rasterImageViewer1.get_Image()); // find the image size in pixels at this resolution r1 = e.get_MarginBounds(); Rectangle destRectangle = new Rectangle( r1.get_Left(), r1.get_Top(), (int)(image.get_ImageWidth() / image.get_XResolution()* 100.0F), (int)(image.get_ImageWidth() / image.get_YResolution() * 100.0F)); RasterPaintProperties props = new RasterPaintProperties(); props.set_PaintEngine(RasterPaintEngine.GdiPlus); image.Paint(e.get_Graphics(), Rectangle.Empty, Rectangle.Empty, destRectangle, e.get_MarginBounds(), props); }
Add an event handler to the Form1 FormClosing event and add the following code: [Visual Basic]
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles MyBase.FormClosing RasterCodecs.Shutdown() End Sub
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { RasterCodecs.Shutdown(); }
private void Form1_FormClosing(Object sender, FormClosingEventArgs e) { RasterCodecs.Shutdown(); }
Add an event handler to button1 ("Load") click event and add the following code: [Visual Basic]
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click Dim dlg As New OpenFileDialog() dlg.Filter = "All Files|*.*" If (dlg.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then Try RasterImageViewer1.Image = codecs.Load(dlg.FileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1) Catch ex As Exception MessageBox.Show(Me, ex.Message) End Try End If End Sub
private void button1_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "All Files|*.*"; if(dlg.ShowDialog(this) == DialogResult.OK) { try { rasterImageViewer1.Image = codecs.Load(dlg.FileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1); } catch(Exception ex) { MessageBox.Show(this, ex.Message); } } }
private void button1_Click_1(Object sender, System.EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.set_Filter("All Files|*.*"); if (dlg.ShowDialog(this) == DialogResult.OK) { try { image = codecs.Load(dlg.get_FileName(), 0, CodecsLoadByteOrder.BgrOrGray, 1, 1); rasterImageViewer1.set_Image(image); } catch (Exception ex) { MessageBox.Show(this, ex.getMessage()); } } }
Add an event handler to button2 ("Print") click event and add the following code: [Visual Basic]
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click If (Not RasterImageViewer1.Image Is Nothing) Then Dim dlg As New PrintPreviewDialog() dlg.Document = document dlg.ShowDialog(Me) End If End Sub
private void button2_Click(object sender, EventArgs e) { if(rasterImageViewer1.Image != null) { PrintPreviewDialog dlg = new PrintPreviewDialog(); dlg.Document = document; dlg.ShowDialog(this); } }
private void button2_Click(Object sender, System.EventArgs e) { if (rasterImageViewer1.get_Image() != null) { try { PrintPreviewDialog dlg = new PrintPreviewDialog(); dlg.set_Document(document); dlg.ShowDialog(this); } catch (Exception abc) { abc.printStackTrace(); } } }
Build, and Run the program to test it.