日期:2014-05-19  浏览次数:20521 次

上传的图片和加水印后的图片都放在一个目录下引发的错误!
报错:“GDI+   中发生一般性错误”。后来查了一些帖子,问题出在我上传的图片和加水印后的图片都放在一个目录下,而且文件名相同。如果放在不同的目录下就不会出问题了。但是这样的话在硬盘上就会保留两张图片:一张带水银的,一张不带水印的。这不是我要的结果,我只想保留一张带水印的,我该怎么办?

------解决方案--------------------
删除原来的就可.
------解决方案--------------------
文件名不一样,保存后删掉不带水印的
------解决方案--------------------
ImageTool.GetThumbNail(MyPostedFile.FileName, 100, 100,
MyPostedFile.ContentType.ToString(), false, MyPostedFile.InputStream,imageName,true,true);


public class ImageTool
{
public ImageTool()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

public static void AddTextToImg(string fileName,string text) // 在图片上加入自己的信息,
{
//string sss = MapPath(fileName);
if ( !File.Exists ( fileName))
{
throw new FileNotFoundException( "The file don 't exist! ");
}

//还需要判断文件类型是否为图像类型,这里就不赘述了

Image image = Image.FromFile(fileName);//MapPath(fileName));
Bitmap bitmap = new Bitmap(image,image.Width,image.Height);
Graphics g = Graphics.FromImage(bitmap);

float fontSize = 22.0f; //字体大小
float textWidth = text.Length*fontSize; //文本的长度
//下面定义一个矩形区域,以后在这个矩形里画上白底黑字
float rectX = 0;
float rectY = 0;
float rectWidth = text.Length*(fontSize+18);
float rectHeight = fontSize+18;
//声明矩形域
RectangleF textArea = new RectangleF(rectX,rectY,rectWidth,rectHeight);
Font font = new Font( "宋体 ",fontSize);//定义字体
Brush whiteBrush = new SolidBrush(Color.White);
Brush blackBrush = new SolidBrush(Color.Black);
g.FillRectangle(blackBrush,rectX,rectY,rectWidth,rectHeight);
g.DrawString(text,font,whiteBrush,textArea);
MemoryStream ms = new MemoryStream();
//保存为Jpg类型
bitmap.Save(ms,ImageFormat.Jpeg);

//输出处理后的图像,这里为了演示方便,我将图片显示在页面中了
/* Response.Clear();
Response.ContentType = "image/jpeg ";
Response.BinaryWrite( ms.ToArray() );
*/
FileStream fs=new FileStream(fileName, FileMode.OpenOrCreate);//.CreateNew);
fs.Write(ms.ToArray(),0,ms.ToArray().Length);
fs.Close();

//Image1.ImageUrl = fileName; // 将图片显示在Image控件中
g.Dispose();
bitmap.Dispose();
image.Dispose();
}

public static ImageFormat GetImageType(object strContentType)
{
if ((strContentType.ToString().ToLower()) == "image/pjpeg ")
{
return ImageFormat.Jpeg;
}
else if ((strContentType.ToString().ToLower()) == "image/gif ")
{
return ImageFormat.Gif;
}
else if ((strContentType.ToString().ToLower()) == "image/bmp ")
{
return ImageFormat.Bmp;
}
else if ((strContentType.ToString().ToLower()) == "image/tiff ")
{
return ImageFormat.Tiff;
}
else if ((strContentType.ToString().ToLower()) == "image/x-icon ")
{
return ImageFormat.Icon;
}
else if ((strContentType.ToString().ToLower()) == "image/x-png ")
{
return ImageFormat.Png;
}
else if ((strContentType.ToString().ToLower()) == "image/x-emf ")
{
return ImageFormat.Emf;
}
else if ((strContentType.ToString().ToLower()) == "image/x-exif ")
{
return ImageFormat.Exif;
}
else if ((strContentType.ToString().ToLower()) == "image/x-wmf ")