문제

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