Features / Loading and Saving an Image
In This Topic
Loading and Saving an Image
In This Topic

Bitmap allows loading an image in C1Bitmap object using the Load method of the C1Bitmap class. With Load and LoadAsync method overloads, the image can be loaded from StorageFile or IInputStream, or from System.IO.Stream. Bitmap also allows loading image metadata, which can be used to determine size of the image, its resolution (DPI), or pixel format. Additionally, several images can be loaded or imported, one by one, into the same instance of C1Bitmap.

Like loading, the image from C1Bitmap can be saved to a StorageFile or IOutputStream, and to System.IO.Stream. The Bitmap provides general Save methods in C1Bitmap class, that accept the container format as an argument. It also provides a specific SaveAs method for each of the supported container formats which has the corresponding encoder.

Here, we discuss loading an arbitrary image from a file. To load an image from a stream, refer to Quick Start.

The following steps illustrate loading an arbitrary image from a file. This code uses LoadAsync method to load an image from a StorageFile.

  1. Create and initialize the following class objects.
    Dim btmp As New C1Bitmap()
    Dim sb As SoftwareBitmap
    Dim sbs As SoftwareBitmapSource
    
  2. Add the following code to load an image from a StorageFile.
    Dim picker = New FileOpenPicker()
    
    picker.FileTypeFilter.Add(".ico")
    picker.FileTypeFilter.Add(".bmp")
    picker.FileTypeFilter.Add(".gif")
    picker.FileTypeFilter.Add(".png")
    picker.FileTypeFilter.Add(".jpg")
    picker.FileTypeFilter.Add(".jpeg")
    picker.FileTypeFilter.Add(".jxr")
    picker.FileTypeFilter.Add(".tif")
    picker.FileTypeFilter.Add(".tiff")
    
    Dim file As StorageFile = Await picker.PickSingleFileAsync()
    
    If file IsNot Nothing Then
        Await btmp.LoadAsync(file, New FormatConverter(PixelFormat.Format32bppPBGRA))
        Await UpdateImageSource()
    End If
    
    Note: To call an asynchronous method on an event, we have used async keyword and await operator.
  3. Create a method, named UpdateImageSource, to create a SoftwareBitmap, set the source SoftwareBitmap and assign it to the image source.
    Private Async Function UpdateImageSource() As Task
        sb = btmp.ToSoftwareBitmap()
        sbs = New SoftwareBitmapSource()
        Await sbs.SetBitmapAsync(sb)
        img.Source = sbs
    End Function
    
  4. Save the loaded arbitrary image using the following code. The SaveAsPngAsync method is used here to save the image to a StorageFile in PNG format.
    Dim picker = New FileSavePicker()
    picker.FileTypeChoices.Add("png", New List(Of String)() From {
        ".png"
    })
    picker.DefaultFileExtension = ".png"
    
    Dim file As StorageFile = Await picker.PickSaveFileAsync()
    
    If file IsNot Nothing Then
        Await btmp.SaveAsPngAsync(file, Nothing)
        Dim md As New MessageDialog("Your file have been saved.")
    
        Await md.ShowAsync()
    End If
    
    btmp.Dispose()
    
See Also