Why does C# memory stream reserve so much memory?

Because this is the algorithm for how it expands its capacity. public override void Write(byte[] buffer, int offset, int count) { //… Removed Error checking for example int i = _position + count; // Check for overflow if (i < 0) throw new IOException(Environment.GetResourceString(“IO.IO_StreamTooLong”)); if (i > _length) { bool mustZero = _position > _length; … Read more

Creating WPF BitmapImage from MemoryStream png, gif

Add bi.CacheOption = BitmapCacheOption.OnLoad directly after your .BeginInit(): BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.CacheOption = BitmapCacheOption.OnLoad; … Without this, BitmapImage uses lazy initialization by default and stream will be closed by then. In first example you try to read image from possibly garbage-collected closed or even disposed MemoryStream. Second example uses file, which is … Read more

With FileStreamResult, how is the MemoryStream closed?

The FileStreamResult will do that for you. When in doubt always check the code, because the code never lies and since ASP.NET MVC is open source it’s even more easy to view the code. A quick search on Google for FileStreamResult.cs lets you verify that in the WriteFile method the stream is correctly disposed using … Read more

Converting TMemoryStream to ‘String’ in Delphi 2009

The code you have is unnecessarily complex, even for older Delphi versions. Why should fetching the string version of a stream force the stream’s memory to be reallocated, after all? function MemoryStreamToString(M: TMemoryStream): string; begin SetString(Result, PChar(M.Memory), M.Size div SizeOf(Char)); end; That works in all Delphi versions, not just Delphi 2009. It works when the … Read more

Does a Stream get Disposed when returning a File from an Action? [duplicate]

According to source code here aspnet/AspNetWebStack/blob/master/src/System.Web.Mvc/FileStreamResult.cs Yes protected override void WriteFile(HttpResponseBase response) { // grab chunks of data and write to the output stream Stream outputStream = response.OutputStream; using (FileStream) { byte[] buffer = new byte[BufferSize]; while (true) { int bytesRead = FileStream.Read(buffer, 0, BufferSize); if (bytesRead == 0) { // no more data break; … Read more

Simpler way to create a C++ memorystream from (char*, size_t), without copying the data?

I’m assuming that your input data is binary (not text), and that you want to extract chunks of binary data from it. All without making a copy of your input data. You can combine boost::iostreams::basic_array_source and boost::iostreams::stream_buffer (from Boost.Iostreams) with boost::archive::binary_iarchive (from Boost.Serialization) to be able to use convenient extraction >> operators to read chunks … Read more

Saving a bitmap into a 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 … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)