Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a bitmap into a MemoryStream

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?

like image 425
Joseph Avatar asked Dec 29 '11 10:12

Joseph


2 Answers

.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.

like image 200
Jeremy McGee Avatar answered Sep 26 '22 14:09

Jeremy McGee


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).

like image 29
George Duckett Avatar answered Sep 25 '22 14:09

George Duckett