Drawing image with trasparency

Is it possible to render a PNG icon to another Image, with custom Opacity?

I need to create a transparent PNG image, and rendering non-transparent (all pixels with alpha = 255) PNG icon files on it, with the alpha chanel set to different values for each icon.

(I’ve tried TextureBrush with Opacity set to a custom value with no success.)

Hi Dániel,


Thank you for contacting Aspose support.

Please check the following piece of code that creates a blank canvas of type PNG with transparency and draws another image over it while specifying the desired opacity (ColorMatrix.Matrix33). Please note, 0.3f means 30% so you may set it to any desired value. Moreover, you may change the opacity for every next image if you set this property in a loop.

C#

using (PngImage pngImage = new PngImage(1500, 1500, PngColorType.TruecolorWithAlpha))
{
ColorMatrix cmxPic = new ColorMatrix();
cmxPic.Matrix33 = 0.3f;
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”);
}