将位图保存到 MemoryStream 中

发布于 2024-12-23 05:13:29 字数 220 浏览 4 评论 0原文

我应该分配内存还是只分配内存流的对象: 这样可以吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

入画浅相思 2024-12-30 05:13:29

.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() or Dispose(). The using statement can be used to wrap a resource such as MemoryStream to tell the runtime that the memory can be reclaimed.

森末i 2024-12-30 05:13:29

您不需要预先分配内存。

之后您可以使用 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 a using statement).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文