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

Bitmap comes with various methods to load images. The C1Bitmap class provides several Load method overloads to load image from various sources such as a file or memory stream. It also allows you to load image metadata, which can be used to determine image size, pixel format, or resolution (in dots-per-inch). 

The loaded image can be saved to a file or a memory stream. The C1Bitmap class provides general Save methods that accept the container format as an argument. C1Bitmap also provides separate SaveAs methods for each of the supported container formats.

The following code illustrates loading and saving an arbitrary image on button clicks. The code example uses OpenFileDialog and SaveFileDialog to access an arbitrary image file kept anywhere on the user's machine. To know how an image can be loaded from a stream object, see the Quick start section.

Partial Public Class MainWindow
    Inherits Window

    'Initializing bitmap as a global variable
    Dim bitmap As New C1Bitmap()

    'Event to load an arbitrary image into the image control on button click
    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
        Dim ofd = New OpenFileDialog()
        ofd.Filter = "Image Files|*.ico;*.bmp;*.gif;" +
                "*.png;*.jpg;*.jpeg;*.jxr;*.tif;*.tiff"

        If ofd.ShowDialog().Value Then
            bitmap.Load(ofd.FileName,
                        New FormatConverter(PixelFormat.Format32bppPBGRA))
            Image.Source = bitmap.ToWriteableBitmap()
            Image.Width = bitmap.PixelWidth
            Image.Height = bitmap.PixelHeight
        End If
    End Sub

    'Event to save the image to a file on button click
    Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
        Dim sfd As New SaveFileDialog()
        sfd.Filter = "Png Files (*.png)|*.png"
        sfd.CheckPathExists = True

        If sfd.ShowDialog().Value Then
            bitmap.Save(sfd.FileName, ContainerFormat.Png)
        End If
    End Sub
End Class
public partial class MainWindow : Window
{
    //Defining a global variable for bitmap
    C1Bitmap bitmap; 

    public MainWindow()
    {
        InitializeComponent();

        //Initializing a bitmap
        bitmap = new C1Bitmap();
    }

    //Event to load an arbitrary image into the image control on button click
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var ofd = new OpenFileDialog();
        ofd.Filter = "Image Files|*.ico;*.bmp;*.gif;" + 
            "*.png;*.jpg;*.jpeg;*.jxr;*.tif;*.tiff";

        if (ofd.ShowDialog().Value)
        {
            bitmap.Load(ofd.FileName, new 
                FormatConverter(PixelFormat.Format32bppPBGRA));
            image.Source = bitmap.ToWriteableBitmap();
            image.Width = bitmap.PixelWidth;
            image.Height = bitmap.PixelHeight;
        }
    }

    //Event to save the image to a file on button click
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        SaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "Png Files (*.png)|*.png";
        sfd.CheckPathExists = true;

         if (sfd.ShowDialog().Value)
         {
             bitmap.Save(sfd.FileName, ContainerFormat.Png);
         }

    }
}
See Also