图片水印效果 出现问题了 在线等啊

a64991875 2009-12-09 01:00:09
/**//// <summary>
/// 会产生graphics异常的PixelFormat
/// </summary>
private static PixelFormat[] indexedPixelFormats = { PixelFormat.Undefined, PixelFormat.DontCare,
PixelFormat.Format16bppArgb1555, PixelFormat.Format1bppIndexed, PixelFormat.Format4bppIndexed,
PixelFormat.Format8bppIndexed
};




protected void Button1_Click1(object sender, EventArgs e)
{
if (File1.PostedFile.FileName.Trim() != "")
{
//上传文件
string extension = Path.GetExtension(File1.PostedFile.FileName).ToLower();
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
string path = Server.MapPath(".");
File1.PostedFile.SaveAs(path);

//加文字水印,注意,这里的代码和以下加图片水印的代码不能共存
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
Graphics g = Graphics.FromImage(image);
g.DrawImage(image, 0, 0, image.Width, image.Height);
Font f = new Font("Verdana", 16);
Brush b = new SolidBrush(Color.Blue);
string addText = "图片加水印";
g.DrawString(addText, f, b, 10, 10);
g.Dispose();

//保存加水印过后的图片,删除原始图片
string newPath = Server.MapPath(".");
image.Save(newPath);
image.Dispose();
if (File.Exists(path))
{
File.Delete(path);
}

Image1.ImageUrl = newPath;
// Response.Redirect(newPath);
}

}

protected void Button2_Click1(object sender, EventArgs e)
{
//上传文件
string extension = Path.GetExtension(File1.PostedFile.FileName).ToUpper();
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
string path = Server.MapPath(".")+"\\upload\\" + fileName + extension;
File1.PostedFile.SaveAs(path);




//加图片水印
System.Drawing.Image image = System.Drawing.Image.FromFile(path);

if (IsPixelFormatIndexed(image.PixelFormat))
{
Bitmap bmp = new Bitmap(image.Width, image.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(image, 0, 0);
}
}
else
{
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Server.MapPath(".") + "\\upload\\" + fileName + extension);
Graphics g = Graphics.FromImage(image);
g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
g.Dispose();
}
//保存加水印过后的图片,删除原始图片
string newPath = Server.MapPath(".") + "\\upload\\" + fileName+extension;
image.Save(newPath);出现GDI+ 中发生一般性错误。 image.Dispose();
if (File.Exists(path))
{
File.Delete(path);
}

Image1.ImageUrl = newPath;
}

private static bool IsPixelFormatIndexed(PixelFormat imgPixelFormat)
{
foreach (PixelFormat pf in indexedPixelFormats)
{
if (pf.Equals(imgPixelFormat)) return true;
}

return false;
}

}





帮我看看呐
...全文
78 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2009-12-09
  • 打赏
  • 举报
回复
a64991875 2009-12-09
  • 打赏
  • 举报
回复
File1.PostedFile.SaveAs(path);
这个不是么?
wuyq11 2009-12-09
  • 打赏
  • 举报
回复
服务器不能对本地的图片文件进行GDI操作
先FileUpload1.SaveAs再使用
a64991875 2009-12-09
  • 打赏
  • 举报
回复
是 GIF格式的
wuyq11 2009-12-09
  • 打赏
  • 举报
回复
是什么图片,可是GIF格式
a64991875 2009-12-09
  • 打赏
  • 举报
回复
权限已经给了 还是那个样子啊

62,025

社区成员

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

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

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

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