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

Free Support Forum - aspose.com

Generated image is not displaying properly in dark mode of Microsoft photo application

Hello Team,

We have generated image from source image using Aspose.Imaging latest version in .NET. When we open generated image in Microsoft photo app having dark mode, image is not displaying properly. It’s look like image have black overlay. How can we overcome this issue? For reference, we have attached sample application with source,generated and expected output image. Please suggest.

SampleApplication.zip (8.7 MB)

@siriussynoptek, your image has transparent background. Black overlay if effect of transparent background on black theme. You can overcome this using not transparent image or setting alpha to white.

using (PngImage image2 =
                (PngImage)Image.Load(@"source_img.png"))
{
                int[] pixels = image2.LoadArgb32Pixels(image2.Bounds);

                for (int i = 0; i < pixels.Length; i++)
                {
                    int r = (pixels[i] >> 16) & 0xFF;
                    int g = (pixels[i] >> 8) & 0xFF;
                    int b = (pixels[i]) & 0xFF;

                    int argb = 255 << 24 | r << 16 | g << 8 | b;
                    pixels[i] = argb;
                }

                image2.SaveArgb32Pixels(image2.Bounds, pixels);
                
                image2.Save(
                    @"res.png",
                    new PngOptions() { ColorType = PngColorType.TruecolorWithAlpha });
}

or you can save image without alpha

using (PngImage image2 =
                (PngImage)Image.Load(@"source_img.png"))
{
                    image2.Save(
                    @"res.png",
                    new PngOptions() { ColorType = PngColorType.Truecolor});
}

Hello Team,

Thank you for providing code snippet of possible solution. We have implemented code snippet in our application and found that generated white back ground image quality is not as per quality of transparent image. We can see that fonts is looks like bold. Also, cross line is not as smooth as cross line of transparent image. Please refer attached comparison image. Please suggest us to overcome this issue.

Comparison.png (30.2 KB)

@siriussynoptek, The resulting effect is due to the fact that some elements are not completely transparent, this smooths the corners.
To get this effect, you need to apply - AlphaBlending - calculate the color of the point depending on the background color and current transparency. The formula for the calculation can be found on the Internet.

The second option is to save to Jpeg or tiff, AlphaBlending method is built in here.

        using (PngImage image2 = (PngImage)Image.Load(@"D:\source_img.png"))
        {
            image2.BackgroundColor = Color.White;
            image2.Save(@"D:\res.tif", new TiffOptions(TiffExpectedFormat.TiffDeflateRgb));
        }

Hi Team,

We have following queries based on last response. Please suggest.

Is this solution work for any transparent image? What to write if we want to save to Jpeg instead tiff here?

How can we apply AlphaBlending in sample application, shared earlier with you, to achieve desired output? If you have any reference for same, Please share with us.

How can we check whether source image is transparent or not using Aspose.Imaging?

Thank you.

@siriussynoptek

Yes.

JpegOptions

The transparency from a pixel can be obtained as follows:

var a = (pixels[i] >> 24) & 0xFF;

If the value is less than 255, then the pixel has transparency

Hi Team,

We have used below code snippet for JpepOptions and found some quality issue. Please refer attached image.

using (PngImage image2 = (PngImage)Image.Load(@“D:\source_img.png”))
{
image2.BackgroundColor = Color.White;
image2.Save(@“D:\res.tif”, new JpegOptions());
}

It is for check whether pixel has transparency or not but here we want to check whether image has transparent background or not and based on that we want to implement solution of making white background in case of image has transparent background. So how can do that?

How can we achieve this?

Comparison.jpeg (87.6 KB)
Generated.jpeg (24.8 KB)

@siriussynoptek, In order not to waste your time waiting for answers from us, to your questions, please study the API Reference by link: https://apireference.aspose.com/imaging/net
Here you will also find a description of JpegOptions. Property “Quality”, if you set it equal to 100, then you will get the expected result.

The concept of a background in the Png format does not exist, any pixel can be transparent, therefore, if at least one pixel is transparent, then this means that the image has transparency.

Unfortunately I have no link.