You_can_call_Mr_Wang 2015-05-16 08:04 采纳率: 0%
浏览 1733

求助:透明GIF加水印后无法保持透明

各位大神好,再下做一个项目,需要加水印的功能,可发现当GIF为透明时,加水印不能保持透明。

以下是源码片段:

//GIF合成
System.Drawing.Imaging.FrameDimension frameDimension = new System.Drawing.Imaging.FrameDimension(origialGif.FrameDimensionsList[0]);
int frameCount = origialGif.GetFrameCount(frameDimension);
for (int i = 0; i < frameCount; i++)
{
if (origialGif.SelectActiveFrame(frameDimension, i) == 0)
{
int delay = Convert.ToInt32(origialGif.GetPropertyItem(20736).Value.GetValue(i));
Image img = AddWaterPic(Image.FromHbitmap(origialGif.GetHbitmap()), waterPicturePath, alpha);
Frame frame = new Frame(img, delay);
frames.Add(frame);
}
}
origialGif.Dispose();
Gif.Components.AnimatedGifEncoder gif = new Gif.Components.AnimatedGifEncoder();
gif.SetTransparent(Color.FromArgb(211, 211, 211));
gif.Start(filePath);
gif.SetRepeat(0);

        for (int i = 0; i < frames.Count; i++)
        {
            gif.SetDelay(frames[i].Delay);
            gif.AddFrame(frames[i].Image);
        }
        gif.Finish();

//加水印
public static Image AddWaterPic(Bitmap origialGif, string waterPicturePath, float alpha)
{
//由于水印是图片,我们也需要定义一个Image来装载它

using (Image imgWatermark = new Bitmap(waterPicturePath))
{
// 获取原图片的高度和宽度

int phWidth = origialGif.Width;
int phHeight = origialGif.Height;
// 获取水印图片的高度和宽度
int wmWidth = imgWatermark.Width;
int wmHeight = imgWatermark.Height;
//需要一个位图来装载水印图片。并设定其分辨率
Bitmap bmWatermark = new Bitmap(origialGif);
bmWatermark.SetResolution(origialGif.HorizontalResolution, origialGif.VerticalResolution);
// 继续,将水印图片装载到一个绘图画面grWatermark
using (Graphics grWatermark = Graphics.FromImage(bmWatermark))
{
//ImageAttributes 对象包含有关在呈现时如何操作位图和图元文件颜色的信息
ImageAttributes imageAttributes = new ImageAttributes();

                #region 设置颜色
                //Colormap: 定义转换颜色的映射
                ColorMap colorMap = new ColorMap();
                //我的水印图被定义成拥有绿色背景色的图片被替换成透明
                colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
                colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);

                ColorMap[] remapTable = { colorMap };

                imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

                float[][] colorMatrixElements = {    
                         new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f}, // red红色   
                         new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f}, //green绿色   
                         new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f}, //blue蓝色          
                         new float[] {0.0f,  0.0f,  0.0f,  alpha, 0.0f},//透明度        
                         new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}};//   
                //ColorMatrix:定义包含 RGBA 空间坐标的 5 x 5 矩阵。   
                //ImageAttributes 类的若干方法通过使用颜色矩阵调整图像颜色。   
                ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);
                imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
                 ColorAdjustType.Bitmap);
                #endregion
                //上面设置完颜色,下面开始设置位置
                int xPosOfWm = 0;
                int yPosOfWm = phHeight - wmHeight;
                // 第二次绘图,把水印印上去
                grWatermark.DrawImage(imgWatermark,
                 new Rectangle(xPosOfWm,
                                     yPosOfWm,
                                     wmWidth,
                                     wmHeight),
                                     0,
                                     0,
                                     wmWidth,
                                     wmHeight,
                                     GraphicsUnit.Pixel,
                                     imageAttributes);
                return bmWatermark;
            }
        }
    }

我在加完水印后,把每帧的图像保持下来,是透明的.但是当把这些透明的图片再次合成时,透明就会变成黑色.
在合成的循环前,循环中使用AnimatedGifEncoder.SetTransparent(Color.Black),部分帧可以变透明。(我把未能变透明的图片在PS进行采样,发现黑色是一样的黑色,但就是没有变透明)。

然后我又查了一些资料,在MSDN上写着“ Image 类不支持位图中的 Alpha 透明。 若要启用 Alpha 透明,请使用每像素 32 位的 PNG 图像。 ”

之前搜到一些转透明的文章,如“http://blog.itpub.net/12639172/viewspace-503870/”,里面提供了一些API,ToImageGif等等,可是转换都不行,要么变成白底,要么直接异常。

是这个原因吗?我用的合成是AnimatedGifEncoder类,网上也没有找到这些相关方面的资料,求大神赐教。

都是透明的图片合成以后不能透明,并且背景变成了黑色。

我全部的分数也只有21.求大神赐教了。

  • 写回答

2条回答 默认 最新

  • 知常曰明 2015-05-16 10:18
    关注

    你的水印是bitmap,bitmap不是透明的

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮