none
C#处理图片时的内存管理

    问题

  • 我写了一个小软件,关于多图处理的(图片合并,裁切,色调)。在64位系统上运行不会出什么问题,但是如果在32位系统会提示内存不足,特别是图片比较多的时候,我一次性处理的图片比较多,最少都有2 3十张,而且图片比较大(一般都是3000 * 3000以上的图片)。我在每张图片处理完成后都会释放内存,而且从任务管理器中查看程序占用的内存确实是被释放过了。但还是会出现内存不足。

    贴上一点图片处理的代码,麻烦那位大侠给指点一下迷津。

    System.Drawing.SolidBrush solidBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
    System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(pictureTemplate.OriginalWidth, pictureTemplate.OriginalHeight);
    bitmap.SetResolution(300f, 300f);
    System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);
    graphics.Clear(System.Drawing.Color.White);
    for (int j = 0; j < pictureTemplate.Layer.Count; j++) {
        if (!(pictureTemplate.Layer[j].OrderNoLayer == "true")) {
            if (!(pictureTemplate.Layer[j].BleedingLayer == "true")) {
                if (pictureTemplate.Layer[j].IsDrawing) {
                    if (!(pictureTemplate.Layer[j].LayerName == "素材图层")) {
                        if (pictureTemplate.Layer[j].ExteriorLayer) {
                            System.GC.Collect();
                            using (System.IO.FileStream fileStream = new System.IO.FileStream(string.Concat(new string[]
                                    {
                                        Swap.ProductInfo.TemplatePath,
                                        "\\",
                                        text,
                                        "\\",
                                        j.ToString(),
                                        "_high.png"
                                    }), System.IO.FileMode.Open, System.IO.FileAccess.Read)) {
                                System.Drawing.Image image = System.Drawing.Image.FromStream(fileStream, true, false);
                                if (pictureTemplate.Layer[j].AdLayer) {
                                    using (System.Drawing.Bitmap bitmap2 = new System.Drawing.Bitmap(image)) {
                                        int x = pictureTemplate.Layer[j].HighOriginalLayerValidAreaX,
                                            y = pictureTemplate.Layer[j].HighOriginalLayerValidAreaY,
                                            w = pictureTemplate.Layer[j].HighOriginalLayerValidAreaWidth + pictureTemplate.Layer[j].HighOriginalLayerValidAreaX,
                                            h = pictureTemplate.Layer[j].HighOriginalLayerValidAreaHeight + pictureTemplate.Layer[j].HighOriginalLayerValidAreaY;
                                        //var data = bitmap2.LockBits(new Rectangle(x, y, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
                                        for (int k = x; k <= x + w; k++) {
                                            for (int l = y; l <= y + h; l++) {
                                                System.Drawing.Color color = bitmap2.GetPixel(k, l);
                                                if (color.A != 0 || color.B != 0 || color.R != 0 || color.G != 0) {
                                                    byte a = color.A;
                                                    byte r = color.R;
                                                    byte g = color.G;
                                                    byte b = color.B;
                                                    color = System.Drawing.Color.FromArgb(128, (int)r, (int)g, (int)b);
                                                    bitmap2.SetPixel(k, l, color);
                                                }
                                            }
                                        }
                                        image = (System.Drawing.Bitmap)bitmap2.Clone();
                                    }
                                }
                                graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, pictureTemplate.OriginalWidth, pictureTemplate.OriginalHeight));
                                image.Dispose();
                            }
                            //System.Drawing.Image image = System.Drawing.Image.FromFile();
                            // 释放内存
                            CommonHelper.ClearMemory();
                        } else if (pictureTemplate.Layer[j].TextLayer == "true" && !pictureTemplate.Layer[j].FormattingTextLayer) {
                            if (!string.IsNullOrEmpty(pictureTemplate.Layer[j].content) && pictureTemplate.Layer[j].content != "请点击此输入文字" && pictureTemplate.Layer[j].content != "双击此处编辑文本") {
                                System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(pictureTemplate.Layer[j].fontcolor);
                                System.Drawing.Font font2 = null;
                                if (pictureTemplate.Layer[j].fontstyle == "") {
                                    font2 = new System.Drawing.Font(pictureTemplate.Layer[j].fontname, pictureTemplate.Layer[j].fontsize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
                                } else if (pictureTemplate.Layer[j].fontstyle == "加粗") {
                                    font2 = new System.Drawing.Font(pictureTemplate.Layer[j].fontname, pictureTemplate.Layer[j].fontsize, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
                                } else if (pictureTemplate.Layer[j].fontstyle == "倾斜") {
                                    font2 = new System.Drawing.Font(pictureTemplate.Layer[j].fontname, pictureTemplate.Layer[j].fontsize, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Pixel);
                                } else if (pictureTemplate.Layer[j].fontstyle == "下划线") {
                                    font2 = new System.Drawing.Font(pictureTemplate.Layer[j].fontname, pictureTemplate.Layer[j].fontsize, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Pixel);
                                } else if (pictureTemplate.Layer[j].fontstyle == "加粗|倾斜") {
                                    font2 = new System.Drawing.Font(pictureTemplate.Layer[j].fontname, pictureTemplate.Layer[j].fontsize, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Pixel);
                                } else if (pictureTemplate.Layer[j].fontstyle == "加粗|倾斜|下划线") {
                                    font2 = new System.Drawing.Font(pictureTemplate.Layer[j].fontname, pictureTemplate.Layer[j].fontsize, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Pixel);
                                } else if (pictureTemplate.Layer[j].fontstyle == "倾斜|下划线") {
                                    font2 = new System.Drawing.Font(pictureTemplate.Layer[j].fontname, pictureTemplate.Layer[j].fontsize, System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Pixel);
                                } else if (pictureTemplate.Layer[j].fontstyle == "加粗|下划线") {
                                    font2 = new System.Drawing.Font(pictureTemplate.Layer[j].fontname, pictureTemplate.Layer[j].fontsize, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Pixel);
                                } else {
                                    font2 = new System.Drawing.Font(pictureTemplate.Layer[j].fontname, pictureTemplate.Layer[j].fontsize, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
                                }
                                graphics.DrawString(pictureTemplate.Layer[j].content, font2, brush, new System.Drawing.Rectangle(pictureTemplate.Layer[j].HighOriginalLayerValidAreaX, pictureTemplate.Layer[j].HighOriginalLayerValidAreaY, pictureTemplate.Layer[j].HighOriginalLayerValidAreaWidth, (pictureTemplate.Layer[j].HighOriginalLayerValidAreaHeight + pictureTemplate.Layer[j].HighOriginalLayerValidAreaY < pictureTemplate.OriginalHeight) ? pictureTemplate.Layer[j].HighOriginalLayerValidAreaHeight : (pictureTemplate.OriginalHeight - pictureTemplate.Layer[j].HighOriginalLayerValidAreaY)));
                            }
                        } else if (pictureTemplate.Layer[j].Filled) {
                            System.Drawing.Bitmap bitmap3 = this.TemporaryFillLayer(pictureTemplate.Layer[j], pictureTemplate);
                            if (bitmap3 == null) {
                                return null;
                            }
                            Layer layer = pictureTemplate.Layer[j];
                            System.Drawing.Bitmap bitmap4 = null;
                            //bitmap4 = (System.Drawing.Bitmap)System.Drawing.Image.FromFile(layer.画布路径);
                            using (System.IO.FileStream fileStream = new System.IO.FileStream(layer.CanvasPath, System.IO.FileMode.Open, System.IO.FileAccess.Read)) {
                                bitmap4 = (System.Drawing.Bitmap)System.Drawing.Image.FromStream(fileStream, true, false);
                                // 旋转图片
                                if (layer.IsRotate) {
                                    bitmap4 = PictureHandleTools.ImageRotate(bitmap4, (float)layer.RotationAngle);
                                }
                            }
    
                            int num2 = (int)((float)layer.GetCloneArea().x * ((float)bitmap3.Width / (float)bitmap4.Width));
                            int num3 = (int)((float)layer.GetCloneArea().width * ((float)bitmap3.Width / (float)bitmap4.Width));
                            int num4 = (int)((float)layer.GetCloneArea().y * ((float)bitmap3.Height / (float)bitmap4.Height));
                            int num5 = (int)((float)layer.GetCloneArea().height * ((float)bitmap3.Height / (float)bitmap4.Height));
                            if (num4 + num5 > bitmap3.Height) {
                                num5 = bitmap3.Height - num4;
                            }
                            if (num2 + num3 > bitmap3.Width) {
                                num3 = bitmap3.Width - num2;
                            }
                            System.Drawing.Bitmap bitmap5 = null;
                            try {
                                //bitmap3.Save("E://image3.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                                bitmap5 = bitmap3.Clone(new System.Drawing.Rectangle(num2, num4, num3, num5), System.Drawing.Imaging.PixelFormat.Undefined);
                                //bitmap5.Save("E://image5.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
                                if (layer.Brightness != 0 || layer.Contrast != 0 || layer.Saturability != 0)
                                    bitmap5 = ImageHelper.KiLighten(bitmap5, layer.Brightness, layer.Contrast, layer.Saturability);
                                // 旋转图片
                                //if (layer.RotationAngle != 0)
                                //    bitmap5 = 图片处理工具类.图片旋转新版本(bitmap5, (float)layer.RotationAngle);
                            } catch (System.Exception ex) {
                                this.ShowMessage(string.Concat(new object[]
                                        {
                                            ex.Message,
                                            "\r\ntemp image width : ",
                                            bitmap3.Width,
                                            " height:",
                                            bitmap3.Height,
                                            "\r\nclone location : ",
                                            num2,
                                            ":",
                                            num4,
                                            ":",
                                            num3,
                                            ":",
                                            num5
                                        }));
                                continue;
                            }
                            try {
                                graphics.DrawImage(bitmap5, new System.Drawing.Rectangle(layer.HighOriginalLayerValidAreaX, layer.HighOriginalLayerValidAreaY, layer.HighOriginalLayerValidAreaWidth, layer.HighOriginalLayerValidAreaHeight));
                            } catch (System.Exception ex) {
                                this.ShowMessage(string.Concat(new object[]
                                        {
                                            ex.Message,
                                            "temp image width : ",
                                            bitmap5.Width,
                                            " height:",
                                            bitmap5.Height,
                                            "\r\n",
                                            layer.HighOriginalLayerValidAreaX,
                                            "\r\n",
                                            layer.HighOriginalLayerValidAreaY,
                                            "\r\n",
                                            layer.HighOriginalLayerValidAreaWidth,
                                            "\r\n",
                                            layer.HighOriginalLayerValidAreaHeight
                                        }));
                                continue;
                            } finally {
                                bitmap3.Dispose();
                                if (bitmap5 != null) {
                                    bitmap5.Dispose();
                                }
                            }
                            if (!layer.IsRotate) {
                                bitmap4.Dispose();
                            }
                            // 释放内存
                            CommonHelper.ClearMemory();
                        }
                    }
                }
            }
        }
    }


    2016年1月12日 12:52

答案