MemoryStream读取Bitmap后爆内存卡读取不了了是怎么回事

MemoryStream类读写内存 - AngelLee2009 - 博客园
  和FileStream一样,MemoryStream和BufferedStream都派生自基类Stream,因此它们有很多共同的属性和方法,但是每一个类都有自己独特的用法。这两个类都是实现对内存进行数据读写的功能,而不是对持久性存储器进行读写。  读写内存-MemoryStream类  MemoryStream类用于向内存而不是磁盘读写数据。MemoryStream封装以无符号字节数组形式存储的数据,该数组在创建MemoryStream对象时被初始化,或者该数组可创建为空数组。可在内存中直接访问这些封装的数据。内存流可降低应用程序中对临时缓冲区和临时文件的需要。下表列出了MemoryStream类的重要方法:  1、Read(byte[] buffer, int offset, int count):从当前流(MemoryStream对象)中读取字节块并将数据写入缓冲区中。
  参数:
    buffer,当此方法返回时,包含指定的字节数组,该数组中从 offset 到 (offset + count -1) 之间的值由从当前流中读取的字符替换。
    offset, buffer 中的从零开始的字节偏移量,从此处开始存储从当前流中的数据;
    count,最多读取的字节数;
  返回值:
  类型:
  写入缓冲区中的总字节数。 如果当前可用字节数不到所请求的字节数,则这一总字节数可能小于所请求的字节数,或者如果在读取任何字节前已到达流的末尾,则为零。   2、ReadByte():从当前流(MemoryStream流)中读取一个字节。
  返回值 :
  类型:
   的字节;或者如果已到达流的末尾,则为 -1。  3、Write(byte[] buffer, int offset, int count):使用从某个缓冲区读取的数据将字节块写入当前流。
  参数:
    buffer:从中写入数据的缓冲区。
    offset:buffer 中的从零开始的字节偏移量,从此处开始将字节复制到当前流。
    count:最多写入的字节数。  4、WriteByte(byte value):将一个字节写入当前流中的当前位置。
  参数:
    value:写入的字节。  Read方法使用的语法如下:
mmstream.Read(byte[] buffer,offset,count)
  其中mmstream为MemoryStream类的一个流对象,3个参数中,buffer包含指定的字节数组,该数组中,从offset到(offset +count-1)之间的值由当前流中读取的字符替换。Offset是指Buffer中的字节偏移量,从此处开始读取。Count是指最多读取的字节数。Write()方法和Read()方法具有相同的参数类型。
  MemoryStream类的使用实例:
using System.IO;
using System.T
class program
static void Main()
byte[] byteA
char[] charA
UnicodeEncoding uniEncoding = new UnicodeEncoding();
byte[] firstString = uniEncoding.GetBytes("努力学习");
byte[] secondString = uniEncoding.GetBytes("不做C#中的菜鸟");
using (MemoryStream memStream = new MemoryStream(100))
memStream.Write(firstString, 0, firstString.Length);
count = 0;
while (count & secondString.Length)
memStream.WriteByte(secondString[count++]);
Console.WriteLine("Capacity={0},Length={1},Position={2}\n", memStream.Capacity.ToString(), memStream.Length.ToString(), memStream.Position.ToString());
memStream.Seek(0, SeekOrigin.Begin);
byteArray = new byte[memStream.Length];
count = memStream.Read(byteArray, 0, 20);
while (count & memStream.Length)
byteArray[count++] = Convert.ToByte(memStream.ReadByte());
charArray = new char[uniEncoding.GetCharCount(byteArray, 0, count)];
uniEncoding.GetDecoder().GetChars(byteArray, 0, count, charArray, 0);
Console.WriteLine(charArray);
Console.ReadKey();
  MemoryStream.Capacity 属性 取得或设定配置给这个资料流的位元组数目。  MemoryStream.Position 属性 指定当前流的位置。  MemoryStream.Length 属性获取用字节表示的流长度。  SeekOrigin()是一个枚举类,作用设定流的一个参数。  SeekOrigin.Begin我得理解就是文件的最开始,&0&是偏移,表示跳过0个字节。写2就是跳过2个字节。  MemoryStream类通过字节读写数据。本例中定义了写入的字节数组,为了更好的说明Write和WriteByte的异同,在代码中声明了两个byte数组,其中一个数组写入时调用Write方法,通过指定该方法的三个参数实现如何写入。  另一个数组调用了WriteByte方法,每次写入一个字节,所以采用while循环来完成全部字节的写入。写入MemoryStream后,可以检索该流的容量,实际长度,当前流的位置,将这些值输出到控制台。通过观察结果,可以确定写入MemoryStream流是否成功。  调用Read和ReadByte两种方法读取MemoryStream流中的数据,并将其进行Unicode编码后输出到控制台。&&&
  来源:http://developer.51cto.com/art/356.htm
随笔 - 158memorystream写下内存提示内存不足 - C#当前位置:& &&&memorystream写下内存提示内存不足memorystream写下内存提示内存不足www.MyException.Cn&&网友分享于:&&浏览:202次memorystream写入内存提示内存不足private Bitmap CreateBitmap(byte[] originalImageData, int originalWidth, int originalHeight) &
//指定8位格式,即256色.old &
//指定32位格式 &
Bitmap resultBitmap = new Bitmap(originalWidth, originalHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb); &
//将该位图存入内存中 &
MemoryStream curImageStream = new MemoryStream(); &
resultBitmap.Save(curImageStream, System.Drawing.Imaging.ImageFormat.Bmp); &
curImageStream.Flush(); &
//由于位图数据需要DWORD对齐(4byte倍数),计算需要补位的个数 &
//int curPadNum = ((originalWidth * 8 + 31) / 32 * 4) - originalW &
int curPadNum = ((originalWidth * 24 + 31) / 32 * 4) - originalW &
//最终生成的位图数据大小 &
//int bitmapDataSize = ((originalWidth * 8 + 31) / 32 * 4) * originalH &
int bitmapDataSize = ((originalWidth * 24 + 31) / 32 * 4) * originalH &
//bitmapDataSize = originalWidth * originalH &
//数据部分相对文件开始偏移,具体可以参考位图文件格式 &
int dataOffset = ReadData(curImageStream, 10, 4); &
//改变调色板,因为默认的调色板是32位彩色的,需要修改为256色的调色板 &
int paletteStart = 54; &
int paletteEnd = dataO &
//int color = 0; &
//RPG24,不需要调色板 &
//for (int i = paletteS i & paletteE i += 4) &
byte[] tempColor = new byte[4]; &
tempColor[0] = (byte) &
tempColor[1] = (byte) &
tempColor[2] = (byte) &
tempColor[3] = (byte)0; &
color++; &
curImageStream.Position = &
curImageStream.Write(tempColor, 0, 4); &
curImageStream.Position = paletteS &
//最终生成的位图数据,以及大小,高度没有变,宽度需要调整 &
byte[] destImageData = new byte[bitmapDataSize]; &
int destWidth = originalWidth + curPadN &
//生成最终的位图数据,注意的是,位图数据 从左到右,从下到上,所以需要颠倒 &
for (int originalRowIndex = originalHeight - 1; originalRowIndex &= 0; originalRowIndex--) &
int destRowIndex = originalHeight - originalRowIndex - 1; &
for (int dataIndex = 0; dataIndex & originalWidth * 3; dataIndex += 3) &
//同时还要注意,新的位图数据的宽度已经变化destWidth,否则会产生错位 &
destImageData[destRowIndex * destWidth + dataIndex] = originalImageData[originalRowIndex * originalWidth * 3 + dataIndex]; &
destImageData[destRowIndex * destWidth + dataIndex + 1] = originalImageData[originalRowIndex * originalWidth * 3 + dataIndex + 1]; &
destImageData[destRowIndex * destWidth + dataIndex + 2] = originalImageData[originalRowIndex * originalWidth * 3 + dataIndex + 2]; &
//将流的Position移到数据段
curImageStream.Position = dataO &
//将新位图数据写入内存中 &
curImageStream.Write(destImageData, 0, bitmapDataSize); &
curImageStream.Flush(); &
//将内存中的位图写入Bitmap对象 &
resultBitmap = new Bitmap(curImageStream); 共&3&页:
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 12345678910 Copyright & &&版权所有基于.NET BitmapImage 内存释放问题的解决方法详解
转载 & & 作者:
本篇文章是对.NET BitmapImage 内存释放问题的解决方法进行了详细的分析介绍,需要的朋友参考下
网上查到的代码,多数的写法使用MemoryStream来实现: 代码如下:new Thread(new ThreadStart(() =& {&&& var bitmap = new BitmapImage();&&& bitmap.BeginInit();
&&& using (var stream = new MemoryStream(File.ReadAllBytes(...))) {&&&&&&& bitmap.StreamSource =&&&&&&& bitmap.CacheOption = BitmapCacheOption.OnL&&&&&&& bitmap.EndInit();&&&&&&& bitmap.Freeze();
&&& }&&& this.Dispatcher.Invoke((Action)delegate {&&&&&&& Image1.Source =
})).Start();今天问题来了,当我设置了DecodeWidth为100时加载1000张图片,照理说内存应该维持100×100的1000张图片,但事实上他保留了所以原始图片的内存直到BitmapImage被回收时才释放,这让我很尴尬,换句话说using(MemoryStream)并没有真正按我们预期释放MemoryStream中的Buffer,那如何才能释放呢?其实最简单就是直接弃用MemoryStream转投FileStream,如下: 代码如下:using (var stream = new FileStream(path, FileMode.Open)) {&&& image.BeginInit();&&& image.StreamSource =
&&& image.DecodePixelWidth = 100;
&&& image.CacheOption = BitmapCacheOption.OnL&&& image.EndInit();&&& image.Freeze();}
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)匿名用户不能发表回复!|

我要回帖

更多关于 ddr3的内存读取速度 的文章

 

随机推荐