ComponentOne Bitmap for WPF
Quick Start

This quick start gets you started with using Bitmap for loading an image. You begin by creating a WPF application in Visual Studio, adding a sample image to your application, and adding code to load the sample image in a standard image control using Bitmap. The code given in this section illustrates loading an image into bitmap through stream object.

Complete the steps given below to see how Bitmap can be used to load an image in a standard image control.

  1. Setting up the application and adding a sample image
  2. Adding code to load image using Bitmap

The following image shows how the application displays an image loaded in bitmap on a button click.

Step 1: Setting up the application and adding a sample image

  1. Create a WPF application in Visual Studio.
  2. Add the following references to your application.
    • C1.WPF.4
    • C1.WPF.Bitmap.4
    • C1.WPF.Automation.4
    • C1.WPF.DX.4
  3. In the Solution Explorer, right click your project name and select Add | New Folder and name it 'Resources'.
  4. In Visual Studio, add a sample image to the Resources folder and set its Build Action property to Embedded Resource from the Properties pane.
  5. Add a standard Button control for loading a sample image on button click, and an image control for displaying the sample image onto the MainWindow.
  6. Set the Content property of the button to a suitable text in XAML view.

Step 2: Adding code to load image using Bitmap

  1. Switch to the code view and add the following import statements.
    Imports C1.WPF
    Imports C1.WPF.Bitmap
    Imports C1.Util.DX
    Imports System.Reflection
    Imports System.IO
    
    using C1.WPF;
    using C1.WPF.Bitmap;
    using C1.Util.DX;
    using System.Reflection;
    using System.IO;
    
  2. Initialize a bitmap in the MainWindow class.
    'Initialize a bitmap
    Private bitmap As C1Bitmap
    
    Public Sub New()
    
        ' This call is required by the designer.
        InitializeComponent()
        bitmap = New C1Bitmap()
        ' Add any initialization after the InitializeComponent() call.
    
    End Sub
    
    //Initialize a bitmap
    C1Bitmap bitmap = new C1Bitmap();
    
  3. Subscribe a button click event and add the following code for loading the sample image into bitmap from a stream object.
    'Load image through stream on button click
    Private Sub Btn_Load_Click(sender As Object, e As RoutedEventArgs) _
        Handles Btn_Load.Click
        Dim t As Type = Me.GetType
        Dim asm As Assembly = t.Assembly
        Dim stream As Stream =
            asm.GetManifestResourceStream(t, "GrapeCity.png")
        bitmap.Load(stream,
                    New FormatConverter(PixelFormat.Format32bppPBGRA))
        UpdateImage()
    End Sub
    
    //Load image through stream on button click
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Assembly asm = typeof(MainWindow).Assembly;
        using (Stream stream = 
            asm.GetManifestResourceStream("Bitmap.Resources.GrapeCity.png"))
        {
            bitmap.Load(stream, 
                new FormatConverter(PixelFormat.Format32bppPBGRA));
        }
    
        UpdateImage();
    }
    
  4. Add the following code to define UpdateImage method for displaying the sample image.
    'Display the loaded image 
    Private Sub UpdateImage()
        Me.image.Source = bitmap.ToWriteableBitmap()
        Me.image.Width = bitmap.PixelWidth
        Me.image.Height = bitmap.PixelHeight
    End Sub
    
    //Display the image loaded in bitmap in the image control
    private void UpdateImage()
    {
        this.image.Source = bitmap.ToWriteableBitmap();
        this.image.Width = bitmap.PixelWidth;
        this.image.Height = bitmap.PixelHeight;
    }
    
See Also