We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Set Image Transparency

Hey,

What image formats can we set the transparency on?

I found another support article that showed me how to do it for a png image (see below), but I’m looking for a full list of file formats and the documentation to show me how to do it?

Adding image with setting it’s opacity - Free Support Forum - aspose.com

using (PngImage pngImage = new PngImage(1500, 1500, PngColorType.TruecolorWithAlpha))
{
ColorMatrix cmxPic = new ColorMatrix();
cmxPic.Matrix33 = 0.6f;
ImageAttributes iaPic = new ImageAttributes();
iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

using (Image loaded = Image.Load(“d:/temp/logo.png”))
{
    Graphics gr = new Graphics(pngImage);
    gr.DrawImage(loaded, loaded.Bounds, GraphicsUnit.Pixel, iaPic);
}
pngImage.Save(“D:/newPng.png”);

}

Kind regards,
Dan

Hello, @djsomers
Let me process your request. You will be replied shortly.

@djsomers, as concerns the format lists, you can look through the article: Supported File Formats.
The formats supporting transparency are Bmp, Tiff, WebP, Png, Ico. You should also mind the next thing: the formats supporting transparency mainly use 32 bits-per-pixel ARGB pixel structure.
More details on these formats you find in the next list of articles:




Hello @Denis.Sitko,

Thanks for getting back to me so quickly.

By the looks of it I need to make .bmp and .tiff files transparent. I have taken a look at the documents that you have provided for both these file formats and I am still unsure how I can set an image (.bmp/.tiff) to transparent. There isn’t even any mention of setting transparency on the BMP article that you have provided.

I need some clear examples of how to achieve my aims as the documentation isn’t that easy to understand, is there any chance of providing me some code examples for setting transparency to ‘true’ for both .bmp and .tiff images for me to follow?

As I said, I can already do it for PNG files.

Kind regards,

Dan

Hello again, @djsomers,
Here are few examples of how to create transparent Tiff. You can also associate 32-bit with ARGB abbreviation (there few more color spaces to handle transparency, but for now ARGB is enough and it is the most common), while RGB is 24-bit with no A for alpha.
The easiest way to create transparent Tiff is to use some of these options:
new TiffOptions(TiffExpectedFormat.TiffNoCompressionRgba/TiffDeflateRgba/TiffLzwRgba);
You can configure options by yourself using the article above, but you should read this article deeply to get known with Tiff properties. So keep in mind setting this property like this
TiffOptions.BitsPerSample = new ushort[4] { 8, 8, 8, 8 }; // makes 32 bit bits per pixel, 8 bits for each component of Red, Blue, Green, Alpha (transparency)
You should try configuring TiffOptions in different ways, but mind the property above.

As concerns Bmp, it is really simple:
new BmpOptions() by the default produces image with supported transparency.
To go deeper it is:

new BmpOptions
{
    BitsPerPixel = 32,
    Compression = Imaging.FileFormats.Bmp.BitmapCompression.Bitfields
}

Hi Denis,

Sorry to continue the back and forth but I’m still having problems and I’m thinking that there might be a difference between ‘transparency’ and ‘opacity’ from an Aspose point if view.

I have used the following BmpOptions code you provided and it still does not save an image with any transparency/opacity:

new BmpOptions
{
BitsPerPixel = 32,
Compression = Imaging.FileFormats.Bmp.BitmapCompression.Bitfields
}

I have attached a PNG image for you to see my desired output. It is a base image with another transparent image above it.

sample.png (602.6 KB)

Are you sure that the above BmpOptions will allow me to do the same for .bmp files?

Kind regards,

Daniel

Hello, @djsomers,
Let me take some time to process your request. You will be replied shortly!

@djsomers, transparency and opacity seem to be opposite concepts.
new BmpOptions { BitsPerPixel = 32, Compression = Bitfields } or simply new BmpOptions() outputs an image with transparency support.

As concerns the sample illustrating your desired output, you are trying to to overlay an image over the other one based on transparency. It is called image alpha-blending.

To do so:

                int[] backgroundPixels;
                Rectangle bounds;
                using (var background = Image.Load("image-back.png") as RasterImage)
                {
                    bounds = background.Bounds;
                    backgroundPixels = background.LoadArgb32Pixels(background.Bounds);                
                }

                int[] foregroundPixels;
                using (var foreground = Image.Load("image-fore.png") as RasterImage)
                {
                    foregroundPixels = foreground.LoadArgb32Pixels(foreground.Bounds);
                }

                var blended = ApplyAlphaBlending(backgroundPixels, foregroundPixels);

                using (var png = new PngImage(bounds.Width, bounds.Height, PngColorType.TruecolorWithAlpha))
                {
                    png.SaveArgb32Pixels(bounds, blended);
                    png.Save("output.png");
                }

            private static int[] ApplyAlphaBlending(int[] backgroundPixels, int[] foregroundPixels)
            {
                int[] alphaBlended = null;
                // here is your alpha-blending function
                return alphaBlended;
            }