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 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 Form1
Inherits Form
'Defining a global variable for bitmap
Private bitmap As C1Bitmap
Public Sub New()
InitializeComponent()
'Initializing a bitmap
bitmap = New C1Bitmap()
End Sub
'Event to load an arbitrary image into picture box on button click
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ofd = New OpenFileDialog()
ofd.Filter = "Image Files|*.ico;*.bmp;" +
"*.gif;*.png;*.jpg;*.jpeg;*.jxr;*.tif;*.tiff"
ofd.Title = "Select the Image"
If ofd.ShowDialog() = DialogResult.OK Then
bitmap.Load(ofd.FileName,
New FormatConverter(PixelFormat.Format32bppPBGRA))
PictureBox1.Image = bitmap.ToGdiBitmap()
End If
End Sub
'Event to save the image appearing in picture box to a file on button click
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim sfd = New SaveFileDialog()
sfd.Filter = "Png Files (*.png)|*.png"
sfd.CheckPathExists = True
If sfd.ShowDialog() = DialogResult.OK Then
bitmap.Save(sfd.FileName, ContainerFormat.Png)
End If
End Sub
End Class
public partial class Form1 : Form
{
//Defining a global variable for bitmap
C1Bitmap bitmap;
public Form1()
{
InitializeComponent();
//Initializing a bitmap
bitmap = new C1Bitmap();
}
//Event to load an arbitrary image into picture box on button click
private void button1_Click(object sender, EventArgs e)
{
var ofd = new OpenFileDialog();
ofd.Filter = "Image Files|*.ico;*.bmp;" +
"*.gif;*.png;*.jpg;*.jpeg;*.jxr;*.tif;*.tiff";
ofd.Title = "Select the Image";
if (ofd.ShowDialog() == DialogResult.OK)
{
bitmap.Load(ofd.FileName,
new FormatConverter(PixelFormat.Format32bppPBGRA));
pictureBox1.Image = bitmap.ToGdiBitmap();
}
}
//Event to save the image appearing in picture box to a file on button click
private void button2_Click(object sender, EventArgs e)
{
var sfd = new SaveFileDialog();
sfd.Filter = "Png Files (*.png)|*.png";
sfd.CheckPathExists = true;
if (sfd.ShowDialog() == DialogResult.OK)
{
bitmap.Save(sfd.FileName, ContainerFormat.Png);
}
}
}