----------------------------------------------
我刚从asp转到c#上,找了些代码做了一个图片加水印的程序,可以中间老报错,将兄弟们指点一下好吗?谢谢。错误地方在代码中我已标出来了。。简单的说就是我上传了一个大图,我将它先缩小到指定尺寸后,再加水印现在出在缩小后,加水印时,老说文件还在使用。。为什么啊?我在缩小后,Dispose()了的啊,可不要报错郁闷..
---------------------------------------------
private int AddPicWater(string sourcePath,string save_Path,string server_path,string add_fileName,string add_extension,int add_Postion,string source_WaterPic)
{
//sourcePath:原图路径;add_fileName:原文件名;add_extension:原扩展名;
//add_Postiong:水印位置;source_WaterPic源水印路径;save_Path;保存路径;server_path服务器上传路径;//加图片水印
....
                  ....
string newPath = save_Path + add_fileName + add_extension;
                          
save_Small_Pic(sourcePath,news_pic_width3,1000,newPath);try
{
System.Drawing.Image image = System.Drawing.Image.FromFile(newPath);
//*******------------------这个语句就报错,说newPath文件正在使用,可是我生成缩略图后,释放了资源的啊?为什么呢???--------------**********//
System.Drawing.Image copyImage = System.Drawing.Image.FromFile( source_WaterPic);
Graphics gg = Graphics.FromImage(image);
switch(add_Postion)
//1为左上,2为右下,3为中间
{
case 1:
gg.DrawImage(copyImage, new Rectangle(10, 10, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
break;
case 2:
gg.DrawImage(copyImage, new Rectangle(image.Width-copyImage.Width, image.Height-copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
break;
case 3:
gg.DrawImage(copyImage, new Rectangle((image.Width-copyImage.Width)/2, (image.Height-copyImage.Height)/2, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
break;
}
gg.Dispose();//保存加水印过后的图片,删除原始图片image.Save(newPath);
image.Dispose();
}
catch(Exception)
{
ff.close();
return 1;
}
ff.close();
return 0;
}
private void save_Small_Pic(string s_Pic,int s_Width,int s_Height,string s_Save_Name)
{
//生成缩略图
System.Drawing.Image image2 = System.Drawing.Image.FromFile(s_Pic);
int smallWidth=image2.Width;
int smallHeight=image2.Height;if  (smallWidth>s_Width)
{float bbb=float.Parse(smallWidth.ToString())/float.Parse(s_Width.ToString());
float ccc=float.Parse(smallHeight.ToString())/bbb;
smallHeight=Convert.ToInt32(ccc);
smallWidth=s_Width;
}
if (smallHeight>s_Height )
{
float bbb=float.Parse(smallHeight.ToString())/float.Parse(s_Height.ToString());
float ccc=float.Parse(smallWidth.ToString())/bbb;
smallWidth=Convert.ToInt32(ccc);
smallHeight=s_Height;
}
if ((smallHeight!=image2.Height) || (smallWidth!=image2.Width))
{System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
System.Drawing.Image smallimage;
smallimage = image2.GetThumbnailImage(smallWidth,smallHeight,callb,new System.IntPtr());
smallimage.Save(s_Save_Name,System.Drawing.Imaging.ImageFormat.Jpeg);
smallimage.Dispose();
}
image2.Dispose();
image2=null;
}

解决方案 »

  1.   

    就是这句
    save_Small_Pic(sourcePath,news_pic_width3,1000,newPath);try
    {
    System.Drawing.Image image = System.Drawing.Image.FromFile(newPath);
    //*******------------------这个语句就报错,说newPath文件正在使用,可是我生成缩略图后,释放了资源的啊?为什么呢???--------------**********//可我在save_Small_Pic中将图片Dispose();的啊应该是文件关闭了的啊。。
    我对C#还很菜菜请兄弟们指教一下嘛!