LEADTOOLS Support
Imaging
Imaging SDK Questions
Re: The transparency of png is gone after converting it to tif
#1
Posted
:
Sunday, July 12, 2015 9:45:35 PM(UTC)
Groups: Registered
Posts: 7
Hi,
I use the next code to convert png file to tiff, but then the (transparency) background is being changed.
This is the code:
using (var image = codecs.Load("before.png"))
{
leadToolsInType = image.OriginalFormat;
codecs.Save(image, "after.tif", RasterImageFormat.Tif, 0);
}
I've attached the before and after files
#2
Posted
:
Monday, July 13, 2015 10:53:25 AM(UTC)
Groups: Guests
Posts: 3,022
Was thanked: 2 time(s) in 2 post(s)
Hi RoiB,
I'm not seeing your before and after files.
In general, you will need to save the image with 32 bit color depth in order to maintain the alpha channel (8 bits for red, 8 bits for green, 8 bits for blue, and 8 bits for alpha).
You are passing 0 as the bitsPerPixel parameter to RasterCodecs.Save. When passing 0 here, the call will take the image.BitsPerPixel property and select the closest value supported by the RasterImageFormat passed.
https://www.leadtools.com/help/leadtools/v19/dh/co/leadtools.codecs~leadtools.codecs.rastercodecs~save(rasterimage,uri,rasterimageformat,int32).html
Typically, PNG files with alpha channel are also 32 bits per pixel, and RasterImageFormat.Tif support 32 bit write. If your before.png file is 32 bpp, then your code should preserve the alpha channel of the image. If your input image is not 32 bpp, then you could try explicitly passing 32 as the fourth parameter to RasterCodecs.Save to see if the alpha channel is maintained. However, if your input image isn't 32 bpp, then it may not have an alpha channel in the first place.
Can you share your before.png image so that I may investigate the issue that you're facing further? Thanks!
#3
Posted
:
Monday, July 13, 2015 10:57:19 AM(UTC)
Groups: Guests
Posts: 3,022
Was thanked: 2 time(s) in 2 post(s)
Also, which version of LEADTOOLS are you working with?
#4
Posted
:
Monday, July 13, 2015 8:16:26 PM(UTC)
Groups: Registered
Posts: 7
I've just sent you an email with the whole informatiion
#5
Posted
:
Tuesday, July 14, 2015 10:36:51 AM(UTC)
Groups: Guests
Posts: 3,022
Was thanked: 2 time(s) in 2 post(s)
In V17.5, 8 bit PNGs with alpha channels needed to be loaded as 32 bit images for the alpha channel to be detected.
The following code preserves the alpha channel during conversion:
RasterImage image = codecs.Load(@"C:\Users\justinf\Desktop\before.png", 32, CodecsLoadByteOrder.Bgr, 1, -1);
codecs.Save(image, @"C:\Users\justinf\Desktop\out.tif", RasterImageFormat.Tif, 32);
image.Dispose();
codecs.Dispose();
I hope that this is helpful! Please let me know if you have any further questions.
Guest attached the following image(s):
LEADTOOLS Support
Imaging
Imaging SDK Questions
Re: The transparency of png is gone after converting it to tif
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.