各位大神好,再下做一个项目,需要加水印的功能,可发现当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.求大神赐教了。