Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
Loading an Image From a Buffer
Take the following steps to start a project and to add some code that loads and displays an image from a buffer:
  1. Start Visual Studio .NET.
  2. Choose File->New->Project… from the menu.
  3. 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.
  4. Type the project name as "Loading an Image From a Buffer" 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 .
  5. 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 16.5\Bin\DotNet\Win32 " folder and select the following DLLs:
    • Leadtools.dll
    • Leadtools.Codecs.dll
    • Leadtools.WinForms.dll
    Click the Select button and then press the OK button to add the above DLLs to the application.
  6. 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 (x86)\LEAD Technologies\LEADTOOLS 16.5\Bin\DotNet\Win32" and then click Open and then click OK.
  7. 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.IO
    Imports Leadtools
    Imports Leadtools.Codecs
    Imports Leadtools.WinForms
    
    [C#]
    
    using System.Runtime.InteropServices;
    using System.IO;
    using Leadtools;
    using Leadtools.Codecs;
    using Leadtools.WinForms;
    
  8. Add an event handler to the Form1 Load event and add the following code:

    [Visual Basic]

    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventA) Handles MyBase.Load
       ' Initialize a new RasterCodecs object
       RasterCodecs.Startup()
       Dim codecs As New RasterCodecs()
    
       Dim fileName As String = "C:\Users\Public\Documents\LEADTOOLS Images\Image1.cmp"
    
       ' Load the file data into a byte array
       Dim buffer() As Byte = File.ReadAllBytes(fileName)
    
       ' Load it
       Dim ms As New MemoryStream(buffer)
       Dim image As RasterImage = codecs.Load(ms)
       MessageBox.Show(String.Format("Loaded, image size is {0} by {1} pixels", image.ImageWidth, image.ImageHeight))
       image.Dispose()
       ms.Dispose()
    
       RasterCodecs.Shutdown()
    End Sub
    
    [C#]
    
    unsafe private void Form1_Load(object sender, System.EventArgs e)
    {
       // Initialize a new RasterCodecs object
       RasterCodecs.Startup();
       RasterCodecs codecs = new RasterCodecs();
    
       string fileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Image1.cmp";
    
       // Load the file data into a byte array
       byte[] buffer = File.ReadAllBytes(fileName);
    
       // Load it
       MemoryStream ms = new MemoryStream(buffer);
       RasterImage image = codecs.Load(ms);
       MessageBox.Show(string.Format("Loaded, image size is {0} by {1} pixels", image.ImageWidth, image.ImageHeight));
       image.Dispose();
       ms.Dispose();
    
       // Load the file data into an unmanaged pointer
       IntPtr ptr = Marshal.AllocHGlobal(buffer.Length);
       Marshal.Copy(buffer, 0, ptr, buffer.Length);
    
       // Load it
       UnmanagedMemoryStream ums = new UnmanagedMemoryStream((byte*)ptr.ToPointer(), buffer.Length);
       image = codecs.Load(ums);
       MessageBox.Show(string.Format("Loaded, image size is {0} by {1} pixels", image.ImageWidth, image.ImageHeight));
       image.Dispose();
       ums.Dispose();
    
       Marshal.FreeHGlobal(ptr);
    
       RasterCodecs.Shutdown();
    }
    
  9. Build, and Run the program to test it.