Create a new image
There are two methods for creating a new empty image; KalikoImage(int width, int height)
will create a transparent image in the specified size while KalikoImage(int width, int height, Color bgcolor)
will create an image filled with the color past as the last parameter.
// Creating a new transparent image var image = new KalikoImage(640, 480); // Creating a white image var image = new KalikoImage(640, 480, Color.White);
Load an image
Images can be loaded from the file system, the web or streams. The easiest way to load an image is to use the corresponding constructor.
// Open image from file system var image = new KalikoImage(@"C:\MyImages\Image.png"); // Open image from URL var image = new KalikoImage(@"http://www.mysite.domain/myimage.jpg"); // Open image from a stream MemoryStream memoryStream = ... var image = new KalikoImage(memoryStream);
Save an image
Images can be saved in several formats, either to the file system or as streams. In addition to format specific methods (such as SaveJpg(string fileName, long quality)
) there's a generic save function that takes an ImageFormat as a parameter which controls the format the image will be saved in.
// Save image to file system in the selected format image.SaveImage(@"C:\MyImages\Output.tif", ImageFormat.Tiff);
Saving in JPEG format
The following methods are available for saving images in JPEG format: StreamJpg(long quality, string fileName)
(write image as HttpResponse
), SaveJpg(Stream stream, long quality)
and SaveJpg(string fileName, long quality)
. Quality parameter is given as a number between 1 and 100 where higher is better quality but also larger file size.
Here's a couple of samples of saving images in JPEG format:
// Save image to file system in jpg format with quality setting 90 image.SaveJpg(@"C:\MyImages\Output.jpg", 90); // Save image to stream in jpg format with quality setting 90 MemoryStream memoryStream = new MemoryStream(); image.SaveJpg(memoryStream, 90); // Send the image in jpg format as a HttpResponse with quality setting 80 image.StreamJpg(80, "MyImage.jpg");
Saving in BMP format
The following methods are available for saving images in BMP format: SaveBmp(Stream stream)
, SaveBmp(string fileName)
and SaveBmp(Stream stream, ImageFormat format)
Saving in GIF format
The following methods are available for saving images in GIF format: StreamGif(string fileName)
(write image as HttpResponse
), SaveGif(Stream stream)
and SaveGif(string fileName)
Saving in PNG format
The following methods are available for saving images in PNG format: SavePng(Stream stream, long quality)
* and SavePng(string fileName, long quality)*
(* please ignore the quality parameter for the PNG related methods.)
Blitting and image filling
There are two ways to place one image into another. Either by using BlitImage
that will take the source image and place it on the destination image at the given coordinates or by using BlitFill
that will use the source image as a pattern and fill the complete destination image - tiled if necessary.
Blitting an image
// Place the source image on top, left of our image var sourceImage = new KalikoImage(@"C:\Img\Stamp.png"); image.BlitImage(sourceImage, 0, 0); // Repeat the above, but in a single call image.BlitImage(@"C:\Img\Stamp.png", 0, 0);
Blitt fill an image
// Create a new image and fill the source image all over var image = new KalikoImage(640, 480); var patternImage = new KalikoImage(@"C:\Img\Checkered.png"); image.BlitFill(patternImage); // Repeat the above, but in a just one additional call var image = new KalikoImage(640, 480); image.BlitFill(@"C:\Img\Checkered.png");
Image resolution
Original resolution is stored when loading an image, but in order to save the resolution with the image the saveResolution
parameter in the save function needs to be set to true
. If not set or set to false
the image will be saved with screen resolution (96 DPI).
var image = new KalikoImage("image.jpg"); image.SaveJpg("image-keep-dpi.jpg", 90, true);
It's also possible to get or set the current resolution by using the HorizontalResolution
and VerticalResolution
properties.
var image = new KalikoImage("image.jpg"); image.VerticalResolution = 300; image.HorizontalResolution = 300; image.SaveJpg("image-300-dpi.jpg", 90, true);
Image dimensions
There are three properties to check the ratio of the image, these are IsLandscape
, IsPortrait
and IsSquare
.
Use Resize(int width, int height)
in order to resize the image into the given width and height - not taking the ratio in between in consideration.
If you want to resize the image but also make sure that it keeps its ratio, then the GetThumbnailImage(int width, int height, ThumbnailMethod method)
function should be used. There are three different ways; ThumbnailMethod.Crop
that will fill the complete given dimensions which might lead to cropping, ThumbnailMethod.Fit
that will fit the entire image within the given dimensions and ThumbnailMethod.Pad
that will fit the entire image within the given dimensions and pad with background color to full dimension if needed.
For more information about creating thumbnails check this tutorial .
Writing text
This library supports writing text onto images. This is done through the functions SetFont(string fileName, float size, FontStyle fontStyle)
that sets the font to use, WriteText(string txt, int x, int y)
to render the text onto the image and WriteText(string txt, int x, int y, float angle)
that renders the text but in any angle.
// Write Hello World on image with semi transparent white var image = new KalikoImage(@"C:\Img\MyImage.jpg"); // Load the font, relative path from the application path image.SetFont("84_rock.ttf", 120, FontStyle.Regular); image.Color = Color.FromArgb(64, Color.White); image.WriteText("Hello World!", 0, 0);