none
OutOfMemory,是什么原因哈? RRS feed

  • 问题

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace FontWatermarkDemo {
        public partial class Form1: Form {
            #region 字体成员及其图片元素.
            //默认字体.
            private float fontSize = 8;
            private FontStyle fontStyle = FontStyle.Regular;
            private Color fontColor = Color.Black;
            //需要指定.
            private FontFamily fontFamily = null;
            private Font font = null;
            private Bitmap bMapPreview; //预览图片.
            private Bitmap bMapSave;    //保存的图片.
            #endregion
            public Form1() {
                InitializeComponent();
                //退出.
                btnExit.Click += (sender, e) => { this.Dispose(); };
            }
            //加载图片.
            private void btnLoadImg_Click(object sender, EventArgs e) {
                openFD.Filter = "图片格式|*.jpeg;*.jpg;*.png;*.bmp;*.gif";  //过滤文件.
                if(openFD.ShowDialog() != DialogResult.OK) return;
                foreach(var file in openFD.FileNames) { //将FileInfo对象加到ListBox.
                    OwnFileInfo oFile = new OwnFileInfo(file);
                    lboxImgList.Items.Add(oFile);
                }
            }
            //检查是否已加载图片.
            private void txtWatermarkFontInput_TextChanged(object sender, EventArgs e) {
                if(lboxImgList.Items.Count <= 0) {
                    MessageBox.Show("请先加载图片", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                OwnFileInfo file = lboxImgList.Items[0] as OwnFileInfo;
                if(file != null)
                    FontWatermark(txtWatermarkFontInput.Text.Trim(), file, 0);
            }
            //设置字体.
            private void btnSetFont_Click(object sender, EventArgs e) {
                fontD.ShowApply = true; //显示"应用"按钮.
                fontD.ShowHelp = false; //不显示"帮助"按钮.
                if(fontD.ShowDialog() != DialogResult.No) {  //响应"应用"或"确定"按钮.
                    Font fontNew = fontD.Font;
                    fontSize = fontNew.Size;
                    fontStyle = fontNew.Style;
                    fontFamily = fontNew.FontFamily;
                    fontColor = fontD.Color;
                }
                OwnFileInfo file = lboxImgList.Items[0] as OwnFileInfo;
                if(file != null)
                    FontWatermark(txtWatermarkFontInput.Text.Trim(), file, 0);
            }
            //水印文字的显示方法. 
            /// <summary>
            /// 水印文字.
            /// </summary>
            /// <param name="strInput">输入的文字.</param>
            /// <param name="imgFullPath">水印图片的完整路径.</param>
            /// <param name="tag">标识,如"0"表示预览不保存,"1"表示预览保存.</param>
            void FontWatermark(string strInput, OwnFileInfo file, int tag) {
                SolidBrush sBrush = new SolidBrush(fontColor);  //画文字的刷子.
                switch(tag) {
                    //指定预览.
                    case 0:
                        bMapPreview = new Bitmap(picBoxWatermarkPreview.Width, picBoxWatermarkPreview.Height);
                        FontWatermarkDealSaveOrNot(bMapPreview, sBrush, strInput, file, 0);
                        break;
                    //指定保存.
                    case 1:
                        bMapSave = new Bitmap(Image.FromFile(file.FileName));
                        FontWatermarkDealSaveOrNot(bMapSave, sBrush, strInput, file, 1);
                        break;
                    default:
                        throw new ArgumentException("unknow tag");
                }
            }
            //水印文字的显示,处理是否保存的内部方法.
            void FontWatermarkDealSaveOrNot(Image bMap, Brush brush, string strInput, OwnFileInfo file, int tag) {
                using(Graphics g = Graphics.FromImage(bMap)) {
                    SizeF sizeFont; //字体宽高.
                    int fWidth;   //字体宽度.
                    int fHeight;  //字体的高度.
                    if(fontFamily == null) {    //未设定字体.
                        font = new Font(strInput, fontSize);
                        sizeFont = g.MeasureString(strInput, font);
                        fWidth = (int)sizeFont.Width;
                        fHeight = (int)sizeFont.Height;
                    }
                    else {
                        font = new Font(fontFamily, fontSize, fontStyle);
                        sizeFont = g.MeasureString(strInput, font);
                        fWidth = (int)sizeFont.Width;
                        fHeight = (int)sizeFont.Height;
                    }
                    //画字体的位置.
                    Point fontPosition = new Point((bMap.Width - fWidth) / 2, (bMap.Height - fHeight) / 2);
                    if(tag == 0) {  //指定预览.
                        g.Clear(Color.LightBlue);   //画布背景色.
                        g.DrawString(strInput, font, brush, fontPosition);
                        picBoxWatermarkPreview.Image = bMapPreview; //用PictureBox显示临时图片.
                    }
                    else if(tag == 1) { //指定保存.
                        g.DrawString(strInput, font, brush, fontPosition);
                        bMap.Save(txtSavePath.Text.Trim() + "\\" + file);
                    }
                }
            }
            //存储要保存图片的路径.
            private void btnSavePathBrowse_Click(object sender, EventArgs e) {
                if(folderBD.ShowDialog() != DialogResult.OK) return;
                txtSavePath.Text = folderBD.SelectedPath;
            }
            //确定保存.
            private void btnConfirm_Click(object sender, EventArgs e) {
                foreach(var f in lboxImgList.Items) {
                    OwnFileInfo file = f as OwnFileInfo;
                    if(file != null)
                        FontWatermark(txtWatermarkFontInput.Text.Trim(), file, 1);
                }
            }
        }
    }
    
    //上面是我用BitMap,给图片加文字...当做到第 41 个的时候,就报OutOfMemory异常了...是代码的问题么?
    2013年7月13日 8:48

答案

全部回复

  • 有个问题想问大家.
    我用 BitMap加水印后,就加了 个个字"明日科技是".
    是"华为行楷","粗体","初号".
    就字体而已,我输出的 图片变大了很多,
    从 
    2.30 MB -> 5.86 MB
    2.62 MB -> 8.37 MB
    2.33 MB -> 7.38 MB
    2.74 MB -> 8.86 MB
    这是我列出的一部分,为什么变化那么大?不就几个字而已么?我觉得图片本身应该更大...何解?


    2013年7月13日 9:10
  • 建议每次使用Graphics绘制图形之后,调用Dispose函数释放内存。
    2013年7月13日 14:11