在网上找的一个批量处理图片缩放的方法。进行图片的批量处理。发现处理的张数最多只能一起处理2000多张,电脑就报内存不足了。下面是主要方法,求大神指导。
       private void button1_Click(object sender, EventArgs e)
        {
  DirectoryInfo cuurDir = new DirectoryInfo(currentDirPath);
            FileInfo[] files = cuurDir.GetFiles(); //返回当前目录的文件列表 
            StringBuilder builder = new StringBuilder("<ul>");
            Parallel.ForEach(files, file =>
            {
                string oldFileName = file.FullName;
                string newFileName = GetSaveDirPath("D:\\VSTS\\JuFine\\03_Code\\DataFeed" + "\\i801") + "\\" + file.Name;
                Bitmap srcBitmap = null;
                Bitmap destBitmap = null;
                Graphics graphic = null;
                try
                {
                    int width = Convert.ToInt32(TxtWidth.Text);
                    int height = Convert.ToInt32(TxtHeight.Text);
                    width = width == 0 ? 100 : width;
                    height = height == 0 ? 100 : height;                    GC.Collect(); GC.WaitForPendingFinalizers();
                    srcBitmap = new Bitmap(file.FullName); //加载原图 
                    destBitmap = new Bitmap(width, height);
                    RectangleF srcRec = new RectangleF(0, 0, srcBitmap.Width, srcBitmap.Height);
                    RectangleF destRec = new RectangleF(0, 0, width, height);
                    graphic = Graphics.FromImage(destBitmap);
                    graphic.DrawImage(srcBitmap, destRec, srcRec, GraphicsUnit.Pixel);                    destBitmap.Save(newFileName, System.Drawing.Imaging.ImageFormat.Jpeg);//保存较大Jpeg,如1280*1024图片时,比较消耗内存哦。
                    if (graphic != null) { graphic.Dispose(); graphic = null; }
                    if (destBitmap != null) { destBitmap.Dispose(); destBitmap = null; }
                    if (srcBitmap != null) { srcBitmap.Dispose(); srcBitmap = null; }
                    GC.Collect(); GC.WaitForPendingFinalizers();
                }
                catch (Exception ex)
                {
                    if (graphic != null) { graphic.Dispose(); graphic = null; }
                    if (destBitmap != null) { destBitmap.Dispose(); destBitmap = null; }
                    if (srcBitmap != null) { srcBitmap.Dispose(); srcBitmap = null; }
                    GC.Collect(); GC.WaitForPendingFinalizers();
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    FileInfo newFile = new FileInfo(newFileName);
                    builder.Append(string.Format(@"<li><img style='width:150px;height:200px;' alt='{0}' src='{1}' /><p>大小:{2}k</p></li>", newFile.Name, newFile.FullName, newFile.Length)).Append(Environment.NewLine);
                }            });
            builder.Append("</ul>");
            string template = TemplateHelper.ReadTemplate(TemplateHelper.GetTemplateDirPath(), "template.htm");
            List<MatchItem> itemList = new List<MatchItem>();
            itemList.Add(new MatchItem("$(imglist)", builder.ToString()));
            webBrowser1.DocumentText = TemplateHelper.AnalyzeTemplate(itemList, template);
            LblResult.Text = string.Format("共计{0}张图片", files.Length);
            MessageBox.Show("请在桌面images文件夹下查看!", "提示信息");
}c#图片处理

解决方案 »

  1.   

    每处理完一张图片即调用一次以下代码进行资源释放。private void ReduceMemory()
    {
    Process A = Process.GetCurrentProcess();
    A.MaxWorkingSet = Process.GetCurrentProcess().MaxWorkingSet;
    A.Dispose();
    }
      

  2.   

      if (graphic != null) { graphic.Dispose(); graphic = null; }
                        if (destBitmap != null) { destBitmap.Dispose(); destBitmap = null; }
                        if (srcBitmap != null) { srcBitmap.Dispose(); srcBitmap = null; }
                        GC.Collect(); GC.WaitForPendingFinalizers();
    这部分代码不也是释放资源的么?为什么还是会出现内存不足的那种情况
      

  3.   

    这个需求建议用控件在客户端来做。这样可以利用用户的电脑系统资源。如果在服务端来做太占资源了,而且用户多的话可能超出服务器的负荷。.NET的内存回收机制决定了他会占用许多内存资源。因为它的内存都不会即时回收,而是会经过一个周期后才会回收。它不像C++。
      

  4.   

    这个需求可以考虑用一些WEB图片上传控件来做。这样即可以减轻服务器的压力,又可以提高用户传图的效率。比如QQ相册里面的上传图片功能。它就是用的控件在客户端来处理的。这样不仅减轻了服务端的压力而且提高了效率,特别是在用户多的应用环境中,效率可以大幅度提高。
      

  5.   

    楼主试试这个控件:http://www.cnblogs.com/xproer/archive/2010/08/09/1796077.html
    支持浏览器:IE6,IE7,IE8,IE8(x64),IE9(x64),Firefox,Chrome,360安全浏览器,360极速浏览器,Maxthon1.x,Maxthon2.x,Maxthon3.x,QQ浏览器
    主界面图片列表添加图片编辑图片:提示信息:整合代码<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Web图片批量上传控件演示页面</title>
    <script type="text/javascript" src="ImageUploader/ImageUploader.js" charset="utf-8"></script>
    </head>
    <body>
    <div><a href="asp.net/images.aspx">查看上传的图片</a></div>
    <div id="msg"></div>
    <script language="javascript" type="text/javascript">
    var imgUploader = new ImageUploader();
    imgUploader.Config["PostUrl"] = "http://localhost/php/upload.php"; window.onload = function()
    {
    imgUploader.Init();
    }
    </script>
    </body>
    </html>
    IE(x86)平台示例下载:
    cab安装包
    cab安装包(x64)
    ASP示例
    ASP.NET示例
    JSP示例
    PHP示例IE(x86),IE(x64),Firefox,Chrome平台示例下载:
    xpi安装包
    crx安装包
    exe安装包
    ASP示例
    ASP.NET示例
    JSP示例
    PHP示例IE(x86),IE(x64),Firefox,Chrome企业版示例下载:
    cab安装包
    xpi安装包
    crx安装包
    exe安装包
    ASP示例
    ASP.NET示例
    JSP示例
    PHP示例