ComponentOne Bitmap for UWP
Loading and Saving an Image
Features > Loading and Saving an Image

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
    
    C1Bitmap btmp = new C1Bitmap();
    SoftwareBitmap sb;
    SoftwareBitmapSource sbs;
    
  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
    
    var 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");
    
    StorageFile file = await picker.PickSingleFileAsync();
    
    if (file != null)
    {
            await btmp.LoadAsync(file, new FormatConverter(PixelFormat.Format32bppPBGRA));
            await UpdateImageSource();
    }
    
    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
    
    async Task UpdateImageSource()
    {
        sb = btmp.ToSoftwareBitmap();
        sbs = new SoftwareBitmapSource();
        await sbs.SetBitmapAsync(sb);
        img.Source = sbs;
    }
    
  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()
    
    var picker = new FileSavePicker();
    picker.FileTypeChoices.Add("png", new List<string> { ".png" });
    picker.DefaultFileExtension = ".png";
    
    StorageFile file = await picker.PickSaveFileAsync();
    
    if (file != null)
    {
        await btmp.SaveAsPngAsync(file, null);
        MessageDialog md = new MessageDialog("Your file have been saved.");
        await md.ShowAsync();
    }
    btmp.Dispose();
    
See Also