【发布时间】:2013-10-02 19:21:26
【问题描述】:
我发出一个网络请求,以接收一个大的 jpeg 作为字节数组。这又可以转换为内存流。我需要将此数据转换为位图数据,以便我可以再次将其复制到字节数组中。我是否正确假设从内存流返回的字节数组与从位图数据的编组副本返回到字节数组的字节数组不同?
我不想将内存流写入图像,因为由于它的大小以及我使用的是紧凑型 cf C# 2 的事实,它会返回内存不足错误。
这是我对服务器的调用..
HttpWebRequest _request = (HttpWebRequest)WebRequest.Create("A url/00249.jpg");
_request.Method = "GET";
_request.Timeout = 5000;
_request.ReadWriteTimeout = 20000;
byte[] _buffer;
int _blockLength = 1024;
int _bytesRead = 0;
MemoryStream _ms = new MemoryStream();
using (Stream _response = ((HttpWebResponse)_request.GetResponse()).GetResponseStream())
{
do
{
_buffer = new byte[_blockLength];
_bytesRead = _response.Read(_buffer, 0, _blockLength);
_ms.Write(_buffer, 0, _bytesRead);
} while (_bytesRead > 0);
}
这是我从位图数据中读取字节数组的代码。
Bitmap Sprite = new Bitmap(_file);
Bitmapdata RawOriginal = Sprite.LockBits(new Rectangle(0, 0, Sprite.Width, Sprite.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
int origByteCount = RawOriginal.Stride * RawOriginal.Height;
SpriteBytes = new Byte[origByteCount];
System.Runtime.InteropServices.Marshal.Copy(RawOriginal.Scan0, SpriteBytes, 0, origByteCount);
Sprite.UnlockBits(RawOriginal);
注意: 我不想用这个:
Bitmap Sprite = new Bitmap(_file);
我想从:
MemoryStream _ms = new MemoryStream();
到
System.Runtime.InteropServices.Marshal.Copy(RawOriginal.Scan0, SpriteBytes, 0, origByteCount);
使用所需的任何转换无需写入位图。
【问题讨论】:
-
为什么不使用 FileStream?我猜你是说你没有内存空间,但文件系统上有空间?
-
@CrazyDart 嗨,感谢您的回复。最终的问题是将这个较大图像的部分提取为较小的图像(又名 Sprite)。我不知道这是否可以使用文件流。不过谢谢你的想法......
标签: c# bytearray bitmapdata compact-framework2.0