This tutorial shows how to create and save a transparent area in an image applying two different techniques in a C# .NET Console application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to add transparency to images in a C# .NET 6 application. |
Completion Time | 20 minutes |
Visual Studio Project | Download tutorial project (2 KB) |
Platform | C# .NET 6 Console Application |
IDE | Visual Studio 2022 |
Runtime Target | .NET 6 or Higher |
Development License | Download LEADTOOLS |
Try it in another language |
|
Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on this tutorial.
Start with a copy of the project created in the Add References and Set a License tutorial. If the project is not available, follow the steps in that tutorial to create it.
The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both). For this project, the following references are needed:
If NuGet references are used, this tutorial requires the following NuGet package:
Leadtools.Formats.Raster.Common
If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS23\Bin\net
:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Core.dll
Leadtools.ImageProcessing.Core.dll
For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.
The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.
There are two types of runtime licenses:
With the project created, the references added, and the license set, coding can begin.
Open Program.cs
in the Solution Explorer and add the following statements to the using block at the top.
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing;
In the Program class add a new method called AddAlphaTransparency()
that takes an image and sets all white and near-white areas in it as transparent by manipulating the image's alpha channel.
static RasterImage AddAlphaTransparency(RasterImage inputImage)
{
// Create an 8-bit image to use later as Alpha channel
RasterImage alpha = inputImage.CreateAlphaImage();
// Fill the alpha channel with white to cause all pixels to be opaque
FillCommand fill = new FillCommand(RasterColor.White);
fill.Run(alpha);
// Find white and near-white pixels in the input image and create region from them
RasterColor colorLow = new RasterColor(245, 245, 245), colorHi = RasterColor.White;
inputImage.AddColorRgbRangeToRegion(colorLow, colorHi, RasterRegionCombineMode.Set);
// Copy the region to the alpha image
alpha.SetRegion(null, inputImage.GetRegion(null), RasterRegionCombineMode.Set);
inputImage.MakeRegionEmpty();
// Fill the region with black to represent transparent pixels.
fill.Color = RasterColor.Black;
fill.Run(alpha);
var outputImage = inputImage.Clone();
outputImage.SetAlphaImage(alpha);
return outputImage;
}
Add a new method called AddTransparentColor()
that takes an image and converts all white and near-white areas to pure white, then marks the white color as transparent.
static RasterImage AddTransparentColor(RasterImage inputImage)
{
var outputImage = inputImage.Clone();
// Find white and near-white pixels in the input image and create region from them
RasterColor colorLow = new RasterColor(245, 245, 245), colorHi = RasterColor.White;
outputImage.AddColorRgbRangeToRegion(colorLow, colorHi, RasterRegionCombineMode.Set);
// Fill the region with a single white color to enable setting it as a unique transparent color
FillCommand fill = new FillCommand(RasterColor.White);
fill.Run(outputImage);
outputImage.Transparent = true;
outputImage.TransparentColor = RasterColor.White;
outputImage.MakeRegionEmpty();
return outputImage;
}
In the Main
method, load a base image to have transparency added to it and pass it to the 2 methods created above:
AddAlphaTransparency()
then save the resulting image as 32-bit PNG to store the transparency alpha channel with it.AddTransparentColor()
then save the resulting image either as PNG or GIF, since both formats support transparent color.// Add code below to the Main() method after the call to SetLicense()
using RasterCodecs codecs = new();
RasterImage image = codecs.Load(@"C:\LEADTOOLS23\Resources\Images\testframe1.jpg");
// Create a new image with trasparency alpha channel from near-white parts of the image
RasterImage imageWithAlpha = AddAlphaTransparency(image);
// Save as 32-bit PNG to store alpha channel with image
codecs.Save(imageWithAlpha, @"C:\LEADTOOLS23\Resources\Images\imagePlusAlpha.png", RasterImageFormat.Png, 32);
// Create a new image with trasparency alpha channel from near-white parts of the image
RasterImage imageWithTransparentColor = AddTransparentColor(image);
// Save as 8-bit GIF to store image with transparent color. Also the PNG format supports transparent color.
codecs.Save(imageWithTransparentColor, @"C:\LEADTOOLS23\Resources\Images\imageWithTranspColor.gif", RasterImageFormat.Gif, 8);
To load the file using MemoryStream
, replace the code in the Main()
method with the following:
static void Main(string[] args)
{
InitLEAD();
// Memory Stream to load and save the image
using RasterCodecs codecs = new RasterCodecs();
string filePath = @"C:\LEADTOOLS23\Resources\Images\testframe1.jpg";
byte[] bytes = File.ReadAllBytes(filePath);
using MemoryStream ms = new MemoryStream(bytes);
RasterImage image = codecs.Load(ms);
// Create a new image with transparency alpha channel from near-white parts of the image
RasterImage imageWithAlpha = AddAlphaTransparency(image);
// Save as 32-bit PNG to store alpha channel with image
codecs.Save(imageWithAlpha, @"C:\LEADTOOLS23\Resources\Images\imagePlusAlpha.png", RasterImageFormat.Png, 32);
// Create a new image with transparency alpha channel from near-white parts of the image
RasterImage imageWithTransparentColor = AddTransparentColor(image);
// Save as 8-bit GIF to store image with transparent color. Also PNG formats supports transparent color.
codecs.Save(imageWithTransparentColor, @"C:\LEADTOOLS23\Resources\Images\imageWithTranspColor.gif", RasterImageFormat.Gif, 8);
}
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps are followed correctly, the application loads a JPEG image, adds transparency to it in two different ways and saves the resulting images with transparency to two different files.
This tutorial showed how to add the necessary references to produce images with transparency as well as how to save them to image files.