VintaSoft Imaging .NET SDK 14.0: Документация для .NET разработчика
В этом разделе
    Загрузка изображений
    В этом разделе
    SDK может загружать изображения из следующих форматов файлов изображений и документов:
    Файл изображения или документа может находиться в памяти, на диске, в сети или в базе данных.

    Также SDK может загружать изображение из объекта HBITMAP, System.Drawing.Image или System.Windows.Media.Imaging.BitmapSource, хранящегося в памяти.


    Вот C#/VB.NET код, который демонстрирует, как загрузить изображение из PNG файла и сохранить его в JPEG файл:
    // 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")
    


    Вот C#/VB.NET код, который демонстрирует, как загрузить изображения из нескольких файлов изображений и документов:
    // 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")
    


    Вот C#/VB.NET код, который демонстрирует, как загрузить изображение из файлового потока:
    // 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)
    


    Вот C#/VB.NET код, который демонстрирует, как загрузить изображение из массива байтов:
    // 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)
    


    Вот C#/VB.NET код, который демонстрирует, как загрузить изображение из базы данных MS Access:
    /// <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