把别人dll文件的内容,发编译出来后。代码就复制过来弄图片压缩。发现文件老是无法删除。大家帮看看还有哪个没释放资源。晕倒        public static void AddWaterAndFontPic(string strSourcePicPath, string SavePicPath, string strText, string WaterMarkPic)
        {
            if (System.IO.File.Exists(strSourcePicPath) && System.IO.File.Exists(WaterMarkPic))
            {
                         
              
                System.Drawing.Image image = System.Drawing.Image.FromFile(strSourcePicPath);                int width = image.Width;
                int height = image.Height;
                Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
                bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                Graphics graphics = Graphics.FromImage(bitmap);
                System.Drawing.Image image2 = new Bitmap(WaterMarkPic);
                int srcWidth = image2.Width;
                int srcHeight = image2.Height;
                graphics.SmoothingMode = SmoothingMode.AntiAlias;
                graphics.DrawImage(image, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel);
                int[] numArray = new int[] { 0x10, 14, 12, 10, 8, 6, 4 };
                Font font = null;
                SizeF ef = new SizeF();
                for (int i = 0; i < 7; i++)
                {
                    font = new Font("arial", (float)numArray[i], FontStyle.Bold);
                    ef = graphics.MeasureString(strText, font);
                    if (((ushort)ef.Width) < ((ushort)width))
                    {
                        break;
                    }
                }
                int num6 = (int)(height * 0.05);
                float y = (height - num6) - (ef.Height / 2f);
                float x = width / 2;
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                SolidBrush brush = new SolidBrush(Color.FromArgb(0x99, 0, 0, 0));
                graphics.DrawString(strText, font, brush, new PointF(x + 1f, y + 1f), format);
                SolidBrush brush2 = new SolidBrush(Color.FromArgb(0x99, 0xff, 0xff, 0xff));
                graphics.DrawString(strText, font, brush2, new PointF(x, y), format);
                Bitmap bitmap2 = new Bitmap(bitmap);
                bitmap2.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                Graphics graphics2 = Graphics.FromImage(bitmap2);
                ImageAttributes imageAttr = new ImageAttributes();
                ColorMap map = new ColorMap();
                map.OldColor = Color.FromArgb(0xff, 0, 0xff, 0);
                map.NewColor = Color.FromArgb(0, 0, 0, 0);
                ColorMap[] mapArray = new ColorMap[] { map };
                imageAttr.SetRemapTable(mapArray, ColorAdjustType.Bitmap);
                float[][] numArray3 = new float[5][];
                float[] numArray4 = new float[5];
                numArray4[0] = 1f;
                numArray3[0] = numArray4;
                numArray4 = new float[5];
                numArray4[1] = 1f;
                numArray3[1] = numArray4;
                numArray4 = new float[5];
                numArray4[2] = 1f;
                numArray3[2] = numArray4;
                numArray4 = new float[5];
                numArray4[3] = 0.3f;
                numArray3[3] = numArray4;
                numArray4 = new float[5];
                numArray4[4] = 1f;
                numArray3[4] = numArray4;
                float[][] newColorMatrix = numArray3;
                ColorMatrix matrix = new ColorMatrix(newColorMatrix);
                imageAttr.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                int num9 = (width - srcWidth) - 10;
                int num10 = 10;
                graphics2.DrawImage(image2, new Rectangle(num9, num10, srcWidth, srcHeight), 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel, imageAttr);
                image = bitmap2;
                try
                {
                    image.Save(SavePicPath, ImageFormat.Jpeg);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                   
                    graphics.Dispose();
                    graphics2.Dispose();
                    font.Dispose();
                  
                    bitmap.Dispose();
                    bitmap2.Dispose();                    brush.Dispose();
                    brush2.Dispose();                                       image.Dispose();
                    image2.Dispose();                    //删除不了,不知道还有什么东西没释放,老是提示文件被占用。已经排除各项原因,应该在这段函数里面出问题
                    File.Delete(strSourcePicPath);
                }            }
            else
            {
                HttpContext.Current.Response.Write(@"<script>alert('源水印图片丢失或源欲处理图片丢失');history.go(-1);return false;</script>");
            }        }
问题是老是无法删除图片

解决方案 »

  1.   


     public static void AddWaterAndFontPic(string strSourcePicPath, string SavePicPath, string strText, string WaterMarkPic)
            {
    //本次可以正常删除
              //File.Delete(strSourcePicPath);              if (System.IO.File.Exists(strSourcePicPath) && System.IO.File.Exists(WaterMarkPic))
                {                  ..................
                   finally
                    {
                       
                        graphics.Dispose();
                        graphics2.Dispose();
                        font.Dispose();
                      
                        bitmap.Dispose();
                        bitmap2.Dispose();                    brush.Dispose();
                        brush2.Dispose();                                       image.Dispose();
                        image2.Dispose();                    //删除不了,不知道还有什么东西没释放,老是提示文件被占用。已经排除各项原因,应该在这段函数里面出问题
                        File.Delete(strSourcePicPath);
                    }            }
      

  2.   

     System.Drawing.Image image = System.Drawing.Image.FromFile(strSourcePicPath);
    这行代码的问题.Image在被释放之前strSourcePic会一直被锁定.
      

  3.   

    那怎么办,我都image.dispose()了?
      

  4.   

    Dispose 
    try
    {
         image.Save(SavePicPath, ImageFormat.Jpeg);
         image.Dispose();//试一下吧.
    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
      

  5.   

    Dispose 
    try 

        image.Save(SavePicPath, ImageFormat.Jpeg); 
        image.Dispose();//试一下吧. 

                    catch (Exception ex) 
                    { 
                        throw ex; 
                    } 
      

  6.   

    公布下解决答案:
    在释放后,采用 GC.collect();
    强制进行垃圾清除就可以了。非常感谢suyiming  以及各位大侠们。谢谢