有很多要加水印,程序写好,昨天测试一下,有的图片能加了,有的图片加不上,说什么‘无法从带有索引像素格式的图像创建graphics对象’到网上查了下,说是加水印的图片为gif格式的话就会报这样的错,可是我所有的图上都是jpg的啊,为什么还会报这样的错呢!从网上找了段代码:如下:
private static PixelFormat[] indexedPixelFormats = { PixelFormat.Undefined, PixelFormat.DontCare,
PixelFormat.Format16bppArgb1555, PixelFormat.Format1bppIndexed, PixelFormat.Format4bppIndexed,
PixelFormat.Format8bppIndexed
};
/// <summary>
/// 判断图片的PixelFormat 是否在 引发异常的 PixelFormat 之中
/// </summary>
/// <param name="imgPixelFormat">原图片的PixelFormat</param>
/// <returns></returns>
private static bool IsPixelFormatIndexed(PixelFormat imgPixelFormat)
{
foreach (PixelFormat pf in indexedPixelFormats)
{
if (pf.Equals(imgPixelFormat)) return true;
}
return false;
}
//.........使用
using (Image img = Image.FromFile("原图片路径"))
{
//如果原图片是索引像素格式之列的,则需要转换
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);
}
//下面的水印操作,就直接对 bmp 进行了
//......
}
else //否则直接操作
{
//直接对img进行水印操作
}
}
根据它的代码,对自己的代码作了这样的修改:
foreach (FileInfo f in Dir.GetFiles("*.jpg")) //查找文件
{
listBox1.Items.Add(Dir + f.ToString());//listBox1中填加文件名
Random rdo = new Random();
string FileUrle = "/" + DateTime.Now.ToString("yyyy-MM-dd").ToString().Replace("-", "/");//生成路径
string path = System.IO.Path.GetFullPath(Application.StartupPath);
string paths = path.Substring(0, path.Length - 9);//根路径
string baseLocation = paths + "img/" + FileUrle + "/";// 上传路径
Directory.CreateDirectory(baseLocation);
//listBox1.Items.Add(Dir + f.ToString()); //listBox1中填加文件名
using (Image img = Image.FromFile(Dir + f.ToString()))
{
//如果原图片是索引像素格式之列的,则需要转换
if (IsPixelFormatIndexed(img.PixelFormat))
{
Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);
Graphics g;
using (g = Graphics.FromImage(bmp))
{
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);
}
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(paths + " /images/shuiyin.png");
g.DrawImage(copyImage, new Rectangle(img.Width - copyImage.Width - 10, img.Height - copyImage.Height - 10, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
g.Dispose();
string imgnames = DateTime.Now.ToString("hhmmss") + rdo.Next(100, 999).ToString() + DateTime.Now.ToString("hhmmss");
bmp.Save(baseLocation + imgnames + ".jpg");//保存加水印的原图
}
else //否则直接操作
{
System.Drawing.Image image = System.Drawing.Image.FromFile(Dir + f.ToString());
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(paths + " /images/shuiyin.png");
Graphics g = Graphics.FromImage(image);
g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width - 10, image.Height - copyImage.Height - 10, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
g.Dispose();
string imgnames = DateTime.Now.ToString("hhmmss") + rdo.Next(100, 999).ToString() + DateTime.Now.ToString("hhmmss");
image.Save(baseLocation + imgnames + ".jpg");//保存加水印的原图
}
}
}
但是这样在 g.DrawImage(copyImage, new Rectangle(img.Width - copyImage.Width - 10, img.Height - copyImage.Height - 10, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);这会报‘参数无效’。在这报错也点不知所措,毕竟这几句代码是拷别人的不是自己写的,所以就。。。根据下面的加水印代码于是把这句Graphics g = Graphics.FromImage(image);
代码,想 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);这句去了也应该没什么问题。去了就对了。
真棒!嗫!完成任务!
在这把加水印的代码写上来吧:
System.Drawing.Image image = System.Drawing.Image.FromFile(原图片路径);
System.Drawing.Image copyImage = System.Drawing.Image.FromFile(paths + " /images/shuiyin.png"); //paths + " /images/shuiyin.png"水印图片路径
Graphics g = Graphics.FromImage(image);
g.DrawImage(copyImage, new Rectangle(image.Width - copyImage.Width - 10, image.Height - copyImage.Height - 10, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
g.Dispose();
string imgnames = DateTime.Now.ToString("hhmmss") + rdo.Next(100, 999).ToString() + DateTime.Now.ToString("hhmmss");
image.Save(baseLocation + imgnames + ".jpg");//保存加水印的原图