首页 新闻 赞助 找找看

关于图片处理引起的内存不足

0
悬赏园豆:5 [已解决问题] 解决于 2014-09-10 14:38

错误1

错误2 

以前网站经常遇到这个错误,我在项目里处理图片时增加了是否是图片的判断,还是不行,只能在IIS里回收应用池来解决,现在网站基本是2天报一次内存不足。

2天前是这个的内存不足

错误3

 

现在网站我没有检测是否有其他内存泄露!只是从错误日志里查出来这些错误

胡晓凯的主页 胡晓凯 | 初学一级 | 园豆:2
提问于:2014-08-12 14:21
< >
分享
最佳答案
0

把 hu core 移除掉。

收获园豆:3
Launcher | 高人七级 |园豆:45045 | 2014-08-12 14:26

跟命名空间有关系吗?

胡晓凯 | 园豆:2 (初学一级) | 2014-08-12 14:29

@胡晓凯: 跟你使用的 Hu.Core 的功能有关系。

Launcher | 园豆:45045 (高人七级) | 2014-08-12 14:32

@Launcher: 这个里有个从网上找的增加水印的方法

/// <summary>
        /// 水印图片
        /// 【如果图片需要缩略,请使用skeletonize.Resizepic()方法对图片进行缩略】
        /// 返回图片虚拟路径,和一个警告信息,可根据此信息获取图片合成信息
        /// </summary>
        /// <param name="picpath">需要水印的图片路径</param>
        /// <param name="waterspath">水印图片路径</param>
        /// <param name="watermodel">水印模式</param>
        /// <param name="spath">文件保存路径</param>
        /// <param name="imgtype">保存文件类型</param>
        /// <param name="filecache">原文件处理方式</param>
        /// <param name="warning">处理警告信息</param>
        /// <returns>错误,返回错误信息;成功,返回图片路径</returns>
        public static string makewatermark(string picpath, string waterspath, Commonfun.WaterType watermodel, string spath, Commonfun.ImageType imgtype, Commonfun.FileCache filecache, out string warning)
        {
            var resetImage = picpath;
            #region
            //反馈信息
            System.Text.StringBuilder checkmessage = new System.Text.StringBuilder();

            //检测源文件
            string _sourceimg_common_mappath = "";
            bool checkfile = false;

            //检测水印源文件
            string _sourceimg_water_mappath = "";
            bool checkfilewater = false;

            checkfile = Commonfun.FileExistMapPath(picpath, Commonfun.FileCheckModel.C, out _sourceimg_common_mappath);
            checkfilewater = Commonfun.FileExistMapPath(waterspath, Commonfun.FileCheckModel.C, out _sourceimg_water_mappath);

            System.Drawing.Image _sourceimg_common = null;
            System.Drawing.Image _sourceimg_water = null;

            if (checkfile == true)
            {
                //从指定源文件,创建image对象
                _sourceimg_common = System.Drawing.Image.FromFile(_sourceimg_common_mappath);
            }
            else
            {
                checkmessage.Append("error:找不到需要的水印图片!" + picpath + ";");
            }
            if (checkfilewater == true)
            {
                //从指定源文件,创建image对象
                _sourceimg_water = System.Drawing.Image.FromFile(_sourceimg_water_mappath);
            }
            else
            {
                checkmessage.Append("error:找不到需要水印图片!" + waterspath + ";");
            }
            #endregion

            #region
            if (string.IsNullOrEmpty(checkmessage.ToString()))
            {
                //源图宽、高
                int _sourceimg_common_width = _sourceimg_common.Width;
                int _sourceimg_common_height = _sourceimg_common.Height;

                //水印图片宽、高
                int _sourceimg_water_width = _sourceimg_water.Width;
                int _sourceimg_water_height = _sourceimg_water.Height;

                #region 水印坐标
                //水印坐标
                int _sourceimg_water_point_x = 0;
                int _sourceimg_water_point_y = 0;

                switch (watermodel)
                {
                    case Commonfun.WaterType.Center:
                        _sourceimg_water_point_x = (_sourceimg_common_width - _sourceimg_water_width) / 2;
                        _sourceimg_water_point_y = (_sourceimg_common_height - _sourceimg_water_height) / 2;
                        ; break;
                    case Commonfun.WaterType.CenterDown:
                        _sourceimg_water_point_x = (_sourceimg_common_width - _sourceimg_water_width) / 2;
                        _sourceimg_water_point_y = _sourceimg_common_height - _sourceimg_water_height;
                        ; break;
                    case Commonfun.WaterType.CenterUp:
                        _sourceimg_water_point_x = (_sourceimg_common_width - _sourceimg_water_width) / 2;
                        _sourceimg_water_point_y = 0;
                        ; break;
                    case Commonfun.WaterType.LeftDown:
                        _sourceimg_water_point_x = 0;
                        _sourceimg_water_point_y = _sourceimg_common_height - _sourceimg_water_height;
                        ; break;
                    case Commonfun.WaterType.LeftUp:
                        ; break;
                    case Commonfun.WaterType.Random:
                        Random r = new Random();
                        int x_random = r.Next(0, _sourceimg_common_width);
                        int y_random = r.Next(0, _sourceimg_common_height);

                        _sourceimg_water_point_x = x_random > (_sourceimg_common_width - _sourceimg_water_width)
                            ? _sourceimg_common_width - _sourceimg_water_width : x_random;

                        _sourceimg_water_point_y = y_random > (_sourceimg_common_height - _sourceimg_water_height)
                            ? _sourceimg_common_height - _sourceimg_water_height : y_random;

                        ; break;
                    case Commonfun.WaterType.RightDown:
                        _sourceimg_water_point_x = _sourceimg_common_width - _sourceimg_water_width;
                        _sourceimg_water_point_y = _sourceimg_common_height - _sourceimg_water_height;
                        ; break;
                    case Commonfun.WaterType.RightUp:
                        _sourceimg_water_point_x = _sourceimg_common_width - _sourceimg_water_width;
                        _sourceimg_water_point_y = 0;
                        ; break;
                }
                #endregion

                //从源图创建画板
                System.Drawing.Graphics _g_common = Graphics.FromImage(_sourceimg_common);

                //设置画笔
                _g_common.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
                _g_common.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                _g_common.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;

                //绘制水印图片
                _g_common.DrawImage(_sourceimg_water, new Rectangle(_sourceimg_water_point_x, _sourceimg_water_point_y, _sourceimg_water_width, _sourceimg_water_height), new Rectangle(0, 0, _sourceimg_water_width, _sourceimg_water_height), GraphicsUnit.Pixel);

                //保存图片
                string _spath_common_mappath = "";
                //全局文件名

                //获取图片类型的hashcode值,生成图片后缀名
                int extro = imgtype.GetHashCode();
                string extend = extro == 0 ? ".jpg" : (extro == 1 ? ".gif" : (extro == 2 ? ".png" : ".jpg"));

                spath = spath + Guid.NewGuid().ToString() + extend;

                Commonfun.FileExistMapPath(spath, Commonfun.FileCheckModel.M, out _spath_common_mappath);

                switch (imgtype)
                {
                    case Commonfun.ImageType.JPEG: _sourceimg_common.Save(_spath_common_mappath, System.Drawing.Imaging.ImageFormat.Jpeg); break;
                    case Commonfun.ImageType.GIF: _sourceimg_common.Save(_spath_common_mappath, System.Drawing.Imaging.ImageFormat.Gif); break;
                    case Commonfun.ImageType.PNG: _sourceimg_common.Save(_spath_common_mappath, System.Drawing.Imaging.ImageFormat.Png); break;
                }
                //释放
                _sourceimg_common.Dispose();
                _sourceimg_water.Dispose();
                _g_common.Dispose();

                //处理原文件
                int filecachecode = filecache.GetHashCode();
                //删除原文件
                if (filecachecode == 1)
                {
                    System.IO.File.Delete(_sourceimg_common_mappath);
                }

                warning = "";
            }
            else
            {
                spath = String.Empty;
            }
            #endregion

            //释放
            if (_sourceimg_common != null)
            {
                _sourceimg_common.Dispose();
            }
            if (_sourceimg_water != null)
            {
                _sourceimg_water.Dispose();
            }

            warning = checkmessage.ToString().TrimEnd(';');
            return String.IsNullOrWhiteSpace(spath) ? resetImage : spath;
        }
View Code
胡晓凯 | 园豆:2 (初学一级) | 2014-08-12 14:37

@胡晓凯: 看不懂。这是啥:Commonfun.FileCache filecache?

Launcher | 园豆:45045 (高人七级) | 2014-08-12 14:47

@Launcher: 是另外的类:

public class Commonfun

{

  /// <summary>
        /// 原图文件是否保存
        /// Delete:保存
        /// Save:不保存,删除
        /// </summary>
        public enum FileCache
        {
            Save,
            Delete
        }

}

胡晓凯 | 园豆:2 (初学一级) | 2014-08-12 15:16

@胡晓凯: 报内存不足错误时,用 Clr Profiler 分析下内存占用情况。

Launcher | 园豆:45045 (高人七级) | 2014-08-12 15:36

@Launcher: 昨天看了 一天网站的内存变化,没有占用太大的(服务器16G的内存),其中我在Image.FromFile 上下增加了try catch,到现在没有出现内存不足的错误!

胡晓凯 | 园豆:2 (初学一级) | 2014-08-14 08:32
其他回答(1)
0

直接用.NET自带的功能处理图片是非常占内存的。。。

可以的话请用unsafe代码处理,具体看以下文章:

http://blog.csdn.net/liehuo123/article/details/6110708

收获园豆:2
XiaoFaye | 园豆:3087 (老鸟四级) | 2014-08-12 14:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册