When I put the image to MemoryStream, does it take the same size in RAM with the size of the image?

For example, if image is 4MB, will MemoryStream be 4MB in size?

有帮助吗?

解决方案

It depends on how you get the image into the memory stream. If you read all the bytes from a file into byte array and load that into a stream, then yes the size will be the same. If you load it into a bitmap, then it will depend on the ImageFormat you use when saving to the stream. Take the following example:

using (MemoryStream ms = new MemoryStream(System.IO.File.ReadAllBytes("C:\\temp\\test.jpg")))
{
    Console.WriteLine(ms.Length);   //59922
}

Bitmap b = new Bitmap("C:\\temp\\test.jpg");
using (MemoryStream ms = new MemoryStream())
{
    b.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
    Console.WriteLine(ms.Length);  //6220854
}

using (MemoryStream ms = new MemoryStream())
{
    b.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    Console.WriteLine(ms.Length); //59922
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top