简体   繁体   中英

Save bmp to dictionary c#

I'm trying to save bmp file to dictionary im using this code

Dictionary<string,MemoryStream> dict = new Dictionary<string,MemoryStream>();

dict.Add("mypicture.png",new MemoryStream());

image.Save(dict["mypicture.png"]);

but for some reason, i have an error in the last sentence i dont know why, is there a missing parameter that i should add in the last function?

Try this:

image.Save(dict["mypicture.png"], ImageFormat.Png);

Don't forget include the library

using System.Drawing.Imaging;

Here is how you can define your image format:

if (ImageFormat.Jpeg.Equals(image.RawFormat))
{
    // JPEG
}
else if (ImageFormat.Png.Equals(image.RawFormat))
{
    // PNG
}
else if (ImageFormat.Bmp.Equals(image.RawFormat))
{
    // BMP
}

So the best solution is

 image.Save(dict["mypicture.bmp"], image.RawFormat);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM