This tutorial shows how to create and save a transparent area in an image applying two different techniques in a C# .NET 6 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 (1 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 the Add Transparency to an Image - C# .NET 6 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 via NuGet packages.
This tutorial requires the following NuGet package:
Leadtools.Formats.Raster.Common
For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.
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 System;
using System.IO;
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)
{
// createa 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 RasterCodecs())
{
RasterImage image = codecs.Load(@"C:\LEADTOOLS22\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:\LEADTOOLS22\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:\LEADTOOLS22\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:\LEADTOOLS22\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 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:\LEADTOOLS22\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 PNG formats supports transparent color.
codecs.Save(imageWithTransparentColor, @"C:\LEADTOOLS22\Resources\Images\imageWithTranspColor.gif", RasterImageFormat.Gif, 8);
}
}
Console.WriteLine("Finished. Press any key to exit.");
Console.ReadKey(true);
}
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps are followed correctly, the application runs and 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.