图片水印效果 出现问题了 在线等啊
/**//// <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;
}
}
帮我看看呐