将位图保存到 MemoryStream 中
我应该分配内存还是只分配内存流的对象: 这样可以吗?
MemoryStream memoryStream = new MemoryStream();
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
如果我需要定义MemoryStream大小,如何从Bitmap中获取它?
Should I allocate the memory or just the object of the memory stream:
Is this OK?
MemoryStream memoryStream = new MemoryStream();
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
If I need to define the MemoryStream size, how can I get it from Bitmap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.NET 是一个托管环境:具体来说,内存分配通常由 .NET 运行时代表您进行管理。您通常不需要自己分配内存。
但有时,您确实需要使用
Close()
或Dispose()
来通知运行时您已完成内存使用。using
语句可用于包装诸如MemoryStream
之类的资源,以告诉运行时可以回收内存。.NET is a managed environment: specifically, memory allocation is usually managed on your behalf by the .NET runtime. You don't typically need to allocate the memory yourself.
Sometimes, however, you do need to inform the runtime when you've finished with memory by using
Close()
orDispose()
. Theusing
statement can be used to wrap a resource such asMemoryStream
to tell the runtime that the memory can be reclaimed.您不需要预先分配内存。
之后您可以使用 MemoryStream.Length 获取大小。
使用
memoryStream
完成所需的操作后,请务必将其处置(或将其全部包装在using
语句中)。You don't need to preallocate memory.
You can get the size afterwards with
memoryStream.Length
.After you've done what you need to with your
memoryStream
, be sure to dispose it (or wrap it all in ausing
statement).