在运行程序事发生:System.OutofMemoryException;内存不足,改怎么解决啊?我是打算在Picturebox里显示一个6000*6000的位图!求各位帮帮忙呀!

解决方案 »

  1.   

    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.Drawing.Imaging;
    using System.IO;namespace test
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private byte[] photo;
            private void Form1_Load(object sender, EventArgs e)
            {
                //Image img = new Bitmap(@"C:\Documents and Settings\All Users\Documents\My Pictures\示例图片\Sunset.jpg");
                Image img = new Bitmap(@"D:\065_045.jpg");
                MemoryStream stream = new MemoryStream();
                img.Save(stream, ImageFormat.Bmp);
                BinaryReader br = new BinaryReader(stream);
                photo = stream.ToArray();
                stream.Close();
            }        private void button1_Click(object sender, EventArgs e)
            {
                byte[] bytes = photo;
                MemoryStream ms = new MemoryStream(bytes);
                ms.Position = 0;
                Image img = Image.FromStream(ms);
                ms.Close();
                this.pictureBox1.Image = img;
                 img.Dispose();              
            }        private void button2_Click(object sender, EventArgs e)
            {
                byte[] bytes = photo;
                FileStream fs = new FileStream(@"D:\tupian.bmp", FileMode.Create, FileAccess.Write);
                fs.Write(bytes, 0, bytes.Length);
                fs.Flush();
                fs.Close();
        
            }       
        }
    }
    这是我的程序
      

  2.   

    调试代码即可知道另,尽量使用 .Dispose() ,不用.Close() 释放资源才足
      

  3.   

    错误 2 “GenerateResource”任务意外失败。
    System.OutOfMemoryException: 内存不足。
       在 System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
       在 System.Drawing.ImageConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
       在 Microsoft.VisualStudio.Windows.Forms.ResourceConverter`1.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
       在 System.ComponentModel.TypeConverter.ConvertFrom(Object value)
       在 System.Resources.ResXDataNode.GenerateObjectFromDataNodeInfo(DataNodeInfo dataNodeInfo, ITypeResolutionService typeResolver)
       在 System.Resources.ResXDataNode.GetValue(ITypeResolutionService typeResolver)
       在 System.Resources.ResXResourceReader.ParseDataNode(XmlTextReader reader, Boolean isMetaData)
       在 System.Resources.ResXResourceReader.ParseXml(XmlTextReader reader)
       在 System.Resources.ResXResourceReader.EnsureResData()
       在 System.Resources.ResXResourceReader.GetEnumerator()
       在 Microsoft.Build.Tasks.ProcessResourceFiles.ReadResources(IResourceReader reader, String fileName)
       在 Microsoft.Build.Tasks.ProcessResourceFiles.ReadResources(String filename, Boolean shouldUseSourcePath)
       在 Microsoft.Build.Tasks.ProcessResourceFiles.ProcessFile(String inFile, String outFile)
       在 Microsoft.Build.Tasks.ProcessResourceFiles.Run(TaskLoggingHelper log, ITaskItem[] assemblyFilesList, ArrayList inputs, ArrayList outputs, Boolean sourcePath, String language, String namespacename, String resourcesNamespace, String filename, String classname, Boolean publicClass)
       在 Microsoft.Build.Tasks.GenerateResource.Execute()
       在 Microsoft.Build.BuildEngine.TaskEngine.ExecuteInstantiatedTask(EngineProxy engineProxy, ItemBucket bucket, TaskExecutionMode howToExecuteTask, ITask task, Boolean& taskResult) test
    这个是提示的错误!各位大侠帮忙看看吧!
      

  4.   

    RGB 8:8:8 未压缩 
    6000*6000*256*256*256/1024/1024/1024/8=70312.5G
      

  5.   

    fs.Write(bytes, 0, bytes.Length); 看看 Length是多少。
      

  6.   

    注释掉2行,就没问题了。
    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;
    using System.Drawing.Imaging;namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
            
            private byte[] photo;         private void Form2_Load(object sender, EventArgs e)
            {
                Image img = new Bitmap(@"E:\pic\Cool\001.jpg") as Image;
                MemoryStream stream = new MemoryStream();
                img.Save(stream, ImageFormat.Bmp);
                //BinaryReader br = new BinaryReader(stream);
                photo = stream.ToArray();
                stream.Close(); 
            }        private void button1_Click(object sender, EventArgs e)
            {
                byte[] bytes = photo;
                MemoryStream ms = new MemoryStream(bytes);
                ms.Position = 0;
                Image img = Image.FromStream(ms);
                ms.Close();
                this.pictureBox1.Image = img;
                //img.Dispose();  
            }        private void button2_Click(object sender, EventArgs e)
            {
                byte[] bytes = photo;
                FileStream fs = new FileStream(@"D:\tupian.bmp", FileMode.Create, FileAccess.Write);
                fs.Write(bytes, 0, bytes.Length);
                fs.Flush();
                fs.Close(); 
            }
        }
    }
      

  7.   


    晕倒,8000X6000X24/1024/1024/8=137.3291015625MByte