Creating, Opening and Saving Images
Creating Image Files
Aspose.Imaging for .NET allows developers to create their own images. Use the static Create method exposed by the Image class to create new images. All you need to do is to supply the appropriate object of one of the classes from the ImageOptions namespace for the desired output image format. To create an image file, first create an instance of one of the classes from the ImageOptions namespace. These classes determine output image format. Below are some classes from the ImageOptions namespace:
- BmpOptions sets the options for creating a BMP file
- GifOptions sets the options for creating a GIF file
- JpegOptions sets the options for creating a JPEG file
- PngOptions sets the options for creating a PNG file
- TiffOptions sets the options for creating a TIFF file
- PsdOptions sets the options for creating a PSD file
Image files can be created setting an output path or by setting a stream.
Creating by Setting Path
Create an object of any desired class from the ImageOptions namespace and set the various properties. The most important property to set is the Source property. This property specifies where the image data resides (in a file or a stream). In the example below, the source is a file. After setting the properties, pass the object to one of the static Create methods along with width and height parameter.The width and height are defined in pixels. In the example below, we are creating a BMP file so we need to create an instance of ImageOptions.BmpOptions.
using Aspose.Imaging; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Creates an instance of BmpOptions and set its various properties | |
BmpOptions ImageOptions = new BmpOptions(); | |
ImageOptions.BitsPerPixel = 24; | |
// Define the source property for the instance of BmpOptions Second boolean parameter determines if the file is temporal or not | |
ImageOptions.Source = new FileCreateSource(dataDir + "result1.bmp", false); | |
// Creates an instance of Image and call Create method by passing the BmpOptions object | |
using (Image image = Image.Create(ImageOptions, 500, 500)) | |
{ | |
image.Save(dataDir + "result2.bmp"); | |
} | |
File.Delete(dataDir + "result1.bmp"); | |
File.Delete(dataDir + "result2.bmp"); |
Creating Using Stream
The process for creating an image using a stream is same as for using a path. The only difference is that you need to create an instance of StreamSource by passing a Stream object to its constructor and assigning it to the Source property. The following code snippet shows you how to create Using Stream.
using Aspose.Imaging; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Creates an instance of BmpOptions and set its various properties | |
BmpOptions ImageOptions = new BmpOptions(); | |
ImageOptions.BitsPerPixel = 24; | |
// Create an instance of System.IO.Stream | |
Stream stream = new FileStream(dataDir + "result1.bmp", FileMode.Create); | |
// Define the source property for the instance of BmpOptions Second boolean parameter determines if the Stream is disposed once get out of scope | |
ImageOptions.Source = new StreamSource(stream, true); | |
// Creates an instance of Image and call Create method by passing the BmpOptions object | |
using (Image image = Image.Create(ImageOptions, 500, 500)) | |
{ | |
// Do some image processing | |
image.Save(dataDir + "result2.bmp"); | |
} | |
File.Delete(dataDir + "result1.bmp"); | |
File.Delete(dataDir + "result2.bmp"); |
Opening Image Files
Developers can use Aspose.Imaging for .NET API to open existing image files for different purposes, like adding effects to the image or to convert an existing file to another format. Whatever the purpose is, Aspose.Imaging provides two standard ways to open existing files: from file or from a stream.
Opening from Disk
Open an image file by passing the path and file name as a parameter to the static method Load exposed by the Image class. The following code snippet shows you how to open image files from disk.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (BmpImage image = (BmpImage)Image.Load(dataDir + "template.bmp")) | |
{ | |
} |
Opening using a Stream
Sometimes the image that we need to open is stored as a stream. In such cases, use the overloaded version of the Load method. This accepts a Stream object as an argument to open the image. The following code snippet shows you how to open image files using a stream.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (FileStream fs = File.OpenRead(dataDir + "template.bmp")) | |
{ | |
using (BmpImage image = (BmpImage)Image.Load(fs)) | |
{ | |
} | |
} |
Saving Image Files
Aspose.Imaging lets you create image files from scratch. It also provides the means to edit existing image files. Once the image is created or modified, the file is usually saved to disk. Aspose.Imaging provides you with methods for saving images to a disk by specifying a path or using a Stream object.
Saving to Disk
The Image class represents an image object, so this class provides all the tools needed to create, load and save an image file. Use the Image class Save method to save images. One overloaded version of the Save method accepts the file location as a string. The following code snippet shows you how to save image files to disk.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (FileStream fs = File.OpenRead(dataDir + "template.bmp")) | |
{ | |
using (BmpImage image = (BmpImage)Image.Load(fs)) | |
{ | |
image.Save(dataDir + "result.png", new PngOptions()); | |
} | |
} | |
File.Delete(dataDir + "result.png"); |
Saving to a Stream
Another overloaded version of the Save method accepts the Stream object as an argument and saves the image file to the stream. The following code snippet shows you how to save image files to disk using a stream.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (BmpImage image = (BmpImage)Image.Load(dataDir + "template.bmp")) | |
{ | |
using (MemoryStream ms = new MemoryStream()) | |
{ | |
image.Save(ms, new PngOptions()); | |
} | |
} |
If the image is created by specifying any of the CreateOptions in the Image constructor, the image is automatically saved to the path or stream supplied during the initialization of the Image class by calling the Save method that doesn’t accept any parameter.
Save image file extension aware
Using Aspose.Imaging file extension maps to appropriative image options if you did not specified them.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
var inputFile = Path.Combine(dataDir, $"template.jpg"); | |
var outputFile = Path.Combine(dataDir, "result.png"); | |
using (var image = Image.Load(inputFile)) | |
{ | |
image.Save(outputFile); | |
} | |
File.Delete(outputFile); |
Setting for Replacing Missing Fonts
Developers can use Aspose.Imaging for .NET API to load existing image files for different purposes and this default font should be used as a replacement for all missing fonts (fonts that are not found in current Operating System). Once the image is modified, the file is will be saved to disk.
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.Sources; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
FontSettings.DefaultFontName = "Comic Sans MS"; | |
string[] files = new string[] { "template.emf", "template.odg", "template.svg", "template.wmf" }; | |
VectorRasterizationOptions[] options = new VectorRasterizationOptions[] { new EmfRasterizationOptions(), new OdgRasterizationOptions(), new SvgRasterizationOptions(), new WmfRasterizationOptions() }; | |
for (int i = 0; i < files.Length; i++) | |
{ | |
string outFile = dataDir + files[i] + ".png"; | |
using (Image img = Image.Load(dataDir + files[i])) | |
{ | |
options[i].PageWidth = img.Width; | |
options[i].PageHeight = img.Height; | |
img.Save(outFile, new PngOptions() | |
{ | |
VectorRasterizationOptions = options[i] | |
}); | |
} | |
File.Delete(outFile); | |
} |
Image load/save indication progress
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Bmp; | |
using Aspose.Imaging.ImageOptions; | |
using Aspose.Imaging.ProgressManagement; | |
using Aspose.Imaging.Sources; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Example of use of separate operation progress event handlers for load/export operations | |
using (var image = Image.Load(dataDir + "template.bmp", new LoadOptions { ProgressEventHandler = ProgressCallback })) | |
{ | |
image.Save(dataDir + "result.psd", | |
new PsdOptions() { ProgressEventHandler = ExportProgressCallback }); | |
} | |
// Example of use of operation progress event handler | |
using (var image = Image.Load(dataDir + "template.bmp", new LoadOptions { ProgressEventHandler = ProgressCallback })) | |
{ | |
image.Save(dataDir + "result.bmp"); | |
} | |
void ProgressCallback(ProgressEventHandlerInfo info) | |
{ | |
Console.WriteLine("{0} : {1}/{2}", info.EventType, info.Value, info.MaxValue); | |
} | |
void ExportProgressCallback(ProgressEventHandlerInfo info) | |
{ | |
Console.WriteLine("Export event {0} : {1}/{2}", info.EventType, info.Value, info.MaxValue); | |
} | |
File.Delete(dataDir + "result.psd"); | |
File.Delete(dataDir + "result.bmp"); |