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

Copy MemoryStream to FileStream and save the file?

You need to reset the position of the stream before copying. outStream.Position = 0; outStream.CopyTo(fileStream); You used the outStream when saving the file using the imageFactory. That function populated the outStream. While populating the outStream the position is set to the end of the populated area. That is so that when you keep on writing … Read more

Image.FromStream() method returns Invalid Argument exception

Image.FromStream() expects a stream that contains ONLY one image! It resets the stream.Position to 0. I’ve you have a stream that contains multiple images or other stuff, you have to read your image data into a byte array and to initialize a MemoryStream with that: Image.FromStream(new MemoryStream(myImageByteArray)); The MemoryStream has to remain open as long … Read more

Serializing/deserializing with memory stream

This code works for me: public void Run() { Dog myDog = new Dog(); myDog.Name= “Foo”; myDog.Color = DogColor.Brown; System.Console.WriteLine(“{0}”, myDog.ToString()); MemoryStream stream = SerializeToStream(myDog); Dog newDog = (Dog)DeserializeFromStream(stream); System.Console.WriteLine(“{0}”, newDog.ToString()); } Where the types are like this: [Serializable] public enum DogColor { Brown, Black, Mottled } [Serializable] public class Dog { public String Name … Read more

MemoryStream – Cannot access a closed Stream

This is because the StreamReader closes the underlying stream automatically when being disposed of. The using statement does this automatically. However, the StreamWriter you’re using is still trying to work on the stream (also, the using statement for the writer is now trying to dispose of the StreamWriter, which is then trying to close the … 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

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

How to bind a MemoryStream to asp:image control?

Best bet is to create an HttpHandler that would return the image. Then bind the ImageUrl property on the asp:Image to the url of the HttpHandler. Here is some code. First create the HttpHandler: <%@ WebHandler Language=”C#” Class=”ImageHandler” %> using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Web; public class ImageHandler : IHttpHandler { public void … Read more

tech