I am trying to convert all of the pngs in a folder to 8bpp pngs using nQuant. I tried using the following code:
foreach (string file in Directory.GetFiles(tempFolderPath, "*.png", SearchOption.TopDirectoryOnly))
{
using (MemoryStream memory = new MemoryStream())
{
using (FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (Bitmap image = new Bitmap(fs))
{
using (Image quantized = quantizer.QuantizeImage(image))
{
quantized.Save(memory, ImageFormat.Png);
byte[] bytes = memory.ToArray();
fs.Write(bytes, 0, bytes.Length);
}
}
}
}
}
This however, doesn't work. No exceptions. Just simply doesn't write to the file. I've replaced it with this working code.
Bitmap image;
foreach (string file in Directory.GetFiles(tempFolderPath, "*.png", SearchOption.TopDirectoryOnly))
{
using (FileStream fso = new FileStream(file, FileMode.Open, FileAccess.ReadWrite))
{
image = new Bitmap(fso);
}
using (MemoryStream memory = new MemoryStream())
{
using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.ReadWrite))
{
using (Image quantized = quantizer.QuantizeImage(image))
{
quantized.Save(memory, ImageFormat.Png);
byte[] bytes = memory.ToArray();
fs.Write(bytes, 0, bytes.Length);
}
}
}
}
Seems like FileMode.OpenOrCreate
can do one OR the other, but not both.
Is there anyway to read and write on the same FileStream
?