给透明gif添加水印,画出来的图透明部分变黑色了。。。。

寒叶gg 2011-04-23 10:03:25
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
/// <summary>
/// imagehandler1 的摘要说明
/// </summary>
public class imagehandler1 : IHttpHandler
{
public imagehandler1()
{
//
// TODO: 在此处添加构造函数逻辑
//
/// <summary>


}

#region IHttpHandler 成员

public bool IsReusable
{
get { return false; }
}
/// 会产生graphics异常的PixelFormat
/// </summary>
private static System.Drawing.Imaging.PixelFormat[] indexedPixelFormats = { System.Drawing.Imaging.PixelFormat.Undefined, System.Drawing.Imaging.PixelFormat.DontCare,
System.Drawing.Imaging.PixelFormat.Format16bppArgb1555, System.Drawing.Imaging.PixelFormat.Format1bppIndexed, System.Drawing.Imaging.PixelFormat.Format4bppIndexed,
System.Drawing.Imaging.PixelFormat.Format8bppIndexed
};
/// <summary>
/// 判断图片的PixelFormat 是否在 引发异常的 PixelFormat 之中
/// </summary>
/// <param name="imgPixelFormat">原图片的PixelFormat</param>
/// <returns></returns>
private static bool IsPixelFormatIndexed(System.Drawing.Imaging.PixelFormat imgPixelFormat)
{
foreach (System.Drawing.Imaging.PixelFormat pf in indexedPixelFormats)
{
if (pf.Equals(imgPixelFormat)) return true;
}

return false;
}



public void ProcessRequest(HttpContext context)
{
string fn = context.Request.PhysicalPath;
using (System.Drawing.Image img = System.Drawing.Image.FromFile(fn))
{
//如果原图片是索引像素格式之列的,则需要转换
if (IsPixelFormatIndexed(img.PixelFormat))
{
Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmp))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(img, 0, 0);
}
Graphics gif = Graphics.FromImage(bmp);
gif.DrawString("全局添加水印", new Font("宋体", 20), new SolidBrush(Color.Red), new PointF(20.0f, bmp.Height - 80.0f));
gif.Dispose();
context.Response.ContentType = "image/jpeg";
bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
context.Response.End();
}
else //否则直接操作
{
Graphics gif = Graphics.FromImage(img);
gif.DrawString("全局添加水印", new Font("宋体", 20), new SolidBrush(Color.Red), new PointF(20.0f, img.Height - 80.0f));
gif.Dispose();
context.Response.ContentType = "image/jpeg";
img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
context.Response.End();
}
}



#endregion
}
}



咋回事啊,闷.请高手指点
...全文
347 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
寒叶gg 2011-05-05
  • 打赏
  • 举报
回复
求帮忙啊
寒叶gg 2011-04-23
  • 打赏
  • 举报
回复
楼上的代码没能解决我的问题。。

报错 :无法从带有索引像素格式的图像创建 Graphics 对象。
子夜__ 2011-04-23
  • 打赏
  • 举报
回复
public static void CreateImageWaterMark(string imageFile, System.Drawing.Image waterImage)
{
if (string.IsNullOrEmpty(imageFile) || !File.Exists(imageFile) || waterImage == null)
{
return;
}

System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imageFile);

if (originalImage.Width - 10 < waterImage.Width || originalImage.Height - 10 < waterImage.Height)
{
return;
}

Graphics graphics = Graphics.FromImage(originalImage);

int x = originalImage.Width - waterImage.Width - 10;
int y = originalImage.Height - waterImage.Height - 10;
int width = waterImage.Width;
int height = waterImage.Height;

graphics.DrawImage(waterImage, new Rectangle(x, y, width, height), 0, 0, width, height, GraphicsUnit.Pixel);
graphics.Dispose();

MemoryStream stream = new MemoryStream();
originalImage.Save(stream, ImageFormat.Jpeg);
originalImage.Dispose();

System.Drawing.Image imageWithWater = System.Drawing.Image.FromStream(stream);

imageWithWater.Save(imageFile);
imageWithWater.Dispose();
}
/// <summary>
/// 对一个指定的图片加上文字水印效果。
/// </summary>
/// <param name="imageFile">图片文件地址</param>
/// <param name="waterText">水印文字内容</param>
public static void CreateTextWaterMark(string imageFile, string waterText)
{
if (string.IsNullOrEmpty(imageFile) || string.IsNullOrEmpty(waterText) || !File.Exists(imageFile))
{
return;
}

System.Drawing.Image originalImage = System.Drawing.Image.FromFile(imageFile);

Graphics graphics = Graphics.FromImage(originalImage);

graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;

SolidBrush brush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
Font waterTextFont = new Font("Arial", 16, FontStyle.Regular);
SizeF waterTextSize = graphics.MeasureString(waterText, waterTextFont);

float x = (float)originalImage.Width - waterTextSize.Width - 10F;
float y = (float)originalImage.Height - waterTextSize.Height - 10F;

graphics.DrawString(waterText, waterTextFont, brush, x, y);

graphics.Dispose();
brush.Dispose();

MemoryStream stream = new MemoryStream();
originalImage.Save(stream, ImageFormat.Jpeg);
originalImage.Dispose();

System.Drawing.Image imageWithWater = System.Drawing.Image.FromStream(stream);

imageWithWater.Save(imageFile);
imageWithWater.Dispose();
}
redboy999 2011-04-23
  • 打赏
  • 举报
回复
做水印到时研究过,就是不带透明效果的那种,这个透明效果也是没做成功,我甚至把一张透明的图片整上去都不行,一同求解
zftow110 2011-04-23
  • 打赏
  • 举报
回复
我也想知道如何给GIF动画加水印,做个记号关注

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧