使用WPF和GifBitmapEncoder创建具有1位透明背景的GIF,有人这样做过吗?

您所在的位置:网站首页 flash背景透明gif 使用WPF和GifBitmapEncoder创建具有1位透明背景的GIF,有人这样做过吗?

使用WPF和GifBitmapEncoder创建具有1位透明背景的GIF,有人这样做过吗?

2023-03-29 15:38| 来源: 网络整理| 查看: 265

我已经能够使用WPF和GifBitmapEncoder在C#中创建GIF文件,但它们没有透明度。我可以使用alpha技术删除代码中的黑色背景,并在我的MainWindow中的图像上显示透明的背景图像,但是GIF不支持alpha透明,因此GIF文件最终具有非透明的白色背景。我知道GIF支持“1位透明”的背景,但我不知道如何使用WPF和GiBitmapEncoder来设置它。我搜索了StackOverflow、必应、谷歌、DuckDuck等,似乎我唯一的结论是微软忽略了GifBitmapEncoder中的这一重要功能。我错了吗?有大量的在线服务可以免费完成这一转换,但这不会对我有帮助。如果有一种方法可以通过使用调色板、ColorProfiles、MetaData或Codec来获得1位透明度,那么它肯定是很好的隐藏信息。

关于我在这里尝试的价值:(我在一个带有黑色背景的gif中阅读BitmapSources列表,然后在执行无用的alpha透明之后将这些内容重新呈现到另一个GIF中。)

GifBitmapDecoder GifDC = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.DelayCreation, BitmapCacheOption.Default); List listBmpSources = new List(); foreach (var thisBF in GifDC.Frames) { //Extract pixels for this BitmapFrame from dropped file byte[] thesePixels = new byte[thisBF.PixelHeight * thisBF.PixelWidth]; thisBF.CopyPixels(thesePixels, thisBF.PixelWidth, 0); //Construct new BitmapSource list with Black set to Transparent List thisListColors = new List(); SetBlackAsTransparent(thisBF, thisListColors); BitmapPalette thisBmpPalette = new BitmapPalette(thisListColors); BitmapSource thisBitmapSourceImage = BitmapSource.Create(thisBF.PixelWidth, thisBF.PixelHeight, thisBF.DpiX, thisBF.DpiY, thisBF.Format, thisBmpPalette, thesePixels, thisBF.PixelWidth); //Store bmp sources for GIFEncoder listBmpSources.Add(thisBitmapSourceImage); } //Useless alpha transparency that doesnt survive rendering out to a Gif file: private void SetBlackAsTransparent(BitmapSource thisBF, List listSWMC) { foreach (var thisColor in thisBF.Palette.Colors) { { if (thisColor == Colors.Black) { listSWMC.Add(Colors.Transparent); continue; } if (Feather(5, thisColor, listSWMC)) continue; if (Feather(10, thisColor, listSWMC)) continue; if (Feather(15, thisColor, listSWMC)) continue; if (Feather(25, thisColor, listSWMC)) continue; if (Feather(50, thisColor, listSWMC)) continue; listSWMC.Add(thisColor); } } } void EncodeGifFile(List pListBmpSources, string pSaveAs) { GifBitmapEncoder encoder = new GifBitmapEncoder(); foreach (var thisBmpSource in pListBmpSources) { var thisBmpFrame = BitmapFrame.Create(thisBmpSource); encoder.Palette = thisBmpSource.Palette; encoder.Frames.Add(thisBmpFrame); } var stream = new FileStream(pSaveAs, FileMode.CreateNew); encoder.Save(stream); stream.Close(); }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3