VintaSoft Imaging .NET SDK 12.2: Documentation for .NET developer
In This Topic
    Load images
    In This Topic
    SDK can load images from the following image and document file formats:

    Image or document file can be located in memory, on disk, network or database.

    Also SDK can load image from HBITMAP, System.Drawing.Image or System.Windows.Media.Imaging.BitmapSource object stored in memory.


    Here is an example that shows how to load an image from PNG file and save to a JPEG file:
    // create a VintasoftImage object, which stores information about image from a PNG file
    Vintasoft.Imaging.VintasoftImage vsImage = new Vintasoft.Imaging.VintasoftImage("image.png");
    // save the image to a JPEG file
    vsImage.Save("image.jpg");
    
    ' create a VintasoftImage object, which stores information about image from a PNG file
    Dim vsImage As New Vintasoft.Imaging.VintasoftImage("image.png")
    ' save the image to a JPEG file
    vsImage.Save("image.jpg")
    


    Here is an example that shows how to load images from several image and document files:
    // create an image collection
    Vintasoft.Imaging.ImageCollection images = new Vintasoft.Imaging.ImageCollection();
    
    // add all image from an animated GIF file to the image collection
    images.Add("animated.gif");
    // add all image from a multipage TIFF file to the image collection
    images.Add("multipage.tif");
    // add an image from a JPEG file to the image collection
    images.Add("image.jpg");
    // add all image from a PDF file to the image collection
    images.Add("document.pdf");
    
    ' create an image collection
    Dim images As New Vintasoft.Imaging.ImageCollection()
    
    ' add all image from an animated GIF file to the image collection
    images.Add("animated.gif")
    ' add all image from a multipage TIFF file to the image collection
    images.Add("multipage.tif")
    ' add an image from a JPEG file to the image collection
    images.Add("image.jpg")
    ' add all image from a PDF file to the image collection
    images.Add("document.pdf")
    


    Here is an example that shows how to load an image from a file stream:
    // open a file stream
    System.IO.FileStream fs = new System.IO.FileStream(
        "image.png", System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);
    
    // create an image collection
    Vintasoft.Imaging.ImageCollection images = new Vintasoft.Imaging.ImageCollection();
    
    // add all image from the file stream to the image collection
    // IMPORTANT: Do not dispose the file stream because it will be disposed
    // automatically when the ImageCollection object is disposed
    images.Add(fs, true);
    
    ' open a file stream
    Dim fs As New System.IO.FileStream("image.png", System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite)
    
    ' create an image collection
    Dim images As New Vintasoft.Imaging.ImageCollection()
    
    ' add all image from the file stream to the image collection
    ' IMPORTANT: Do not dispose the file stream because it will be disposed
    ' automatically when the ImageCollection object is disposed
    images.Add(fs, True)
    


    Here is an example that shows how to load an image from a byte array:
    // get image as a byte array
    byte[] imageAsByteArray = System.IO.File.ReadAllBytes("image.png");
    
    // create a memory stream, which will store the image
    System.IO.MemoryStream mem = new System.IO.MemoryStream(imageAsByteArray);
    
    // create a VintasoftImage object, which is based on the memory stream
    // IMPORTANT: Do not dispose the memory stream because it will be disposed
    // automatically when the VintasoftImage object is disposed
    Vintasoft.Imaging.VintasoftImage vsImage = 
        new Vintasoft.Imaging.VintasoftImage(mem, true);
    
    ' get image as a byte array
    Dim imageAsByteArray As Byte() = System.IO.File.ReadAllBytes("image.png")
    
    ' create a memory stream, which will store the image
    Dim mem As New System.IO.MemoryStream(imageAsByteArray)
    
    ' create a VintasoftImage object, which is based on the memory stream
    ' IMPORTANT: Do not dispose the memory stream because it will be disposed
    ' automatically when the VintasoftImage object is disposed
    Dim vsImage As New Vintasoft.Imaging.VintasoftImage(mem, True)
    


    Here is an example that shows how to load image from MS Access database:
    /// <summary>
    /// Returns an image from Microsoft Access database.
    /// </summary>
    /// <param name="imageId">The image identifier in database.</param>
    /// <returns>Image.</returns>
    public Vintasoft.Imaging.VintasoftImage LoadImageFromMSAccessDatabase(int imageId)
    {
        Vintasoft.Imaging.VintasoftImage result = null;
    
        System.Data.OleDb.OleDbConnection myConnection = null;
        try
        {
            // the connection string, which is used for opening the databae
            string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=image.mdb";
            // connect to the database
            myConnection = new System.Data.OleDb.OleDbConnection(connectionString);
            // open the connection to the database
            myConnection.Open();
    
            // the text of SQL query
            string sqlCommand = string.Format("SELECT ImageData FROM [ImageDatabase] WHERE ImageId = {0}", imageId);
            // create the SQL command
            System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand(sqlCommand, myConnection);
            // execute the SQL command and get image from the database
            byte[] imageAsByteArray = (byte[])myCommand.ExecuteScalar();
            // if image is retrieved from the database
            if (imageAsByteArray != null)
            {
                // create a memory stream, which will store the image
                System.IO.MemoryStream mem = new System.IO.MemoryStream(imageAsByteArray);
    
                // create a VintasoftImage object, which is based on the memory stream
                // IMPORTANT: Do not dispose the memory stream because it will be disposed
                // automatically when the VintasoftImage object is disposed
                result = new Vintasoft.Imaging.VintasoftImage(mem, true);
            }
        }
        finally
        {
            // close the connection to the database
            myConnection.Close();
        }
    
        return result;
    }
    
    ''' <summary>
    ''' Returns an image from Microsoft Access database.
    ''' </summary>
    ''' <param name="imageId">The image identifier in database.</param>
    ''' <returns>Image.</returns>
    Public Function LoadImageFromMSAccessDatabase(imageId As Integer) As Vintasoft.Imaging.VintasoftImage
        Dim result As Vintasoft.Imaging.VintasoftImage = Nothing
    
        Dim myConnection As System.Data.OleDb.OleDbConnection = Nothing
        Try
            ' the connection string, which is used for opening the databae
            Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=image.mdb"
            ' connect to the database
            myConnection = New System.Data.OleDb.OleDbConnection(connectionString)
            ' open the connection to the database
            myConnection.Open()
    
            ' the text of SQL query
            Dim sqlCommand As String = String.Format("SELECT ImageData FROM [ImageDatabase] WHERE ImageId = {0}", imageId)
            ' create the SQL command
            Dim myCommand As New System.Data.OleDb.OleDbCommand(sqlCommand, myConnection)
            ' execute the SQL command and get image from the database
            Dim imageAsByteArray As Byte() = DirectCast(myCommand.ExecuteScalar(), Byte())
            ' if image is retrieved from the database
            If imageAsByteArray IsNot Nothing Then
                ' create a memory stream, which will store the image
                Dim mem As New System.IO.MemoryStream(imageAsByteArray)
    
                ' create a VintasoftImage object, which is based on the memory stream
                ' IMPORTANT: Do not dispose the memory stream because it will be disposed
                ' automatically when the VintasoftImage object is disposed
                result = New Vintasoft.Imaging.VintasoftImage(mem, True)
            End If
        Finally
            ' close the connection to the database
            myConnection.Close()
        End Try
    
        Return result
    End Function