I'm trying to download and save multiple bmps from a streetview panorama at a time. It's been working fine with Panorama() in a normal for loop but when I put it in a Parallel.For it throws an exception after about 20 images with an invalid parameters exception with the using (Bitmap result = new Bitmap(26 * 512, 13 * 512))
being higlighted. Memory usage is at 4GB when it crashes but that shouldn't be a problem as I have 16GB of RAM.
Here is my code:
public static void AllPanoramas((double Lat, double Lon)[] locData, string folderPath, ImageFormat format)
{
ServicePointManager.DefaultConnectionLimit = Environment.ProcessorCount * 12;
string[] panoIDs = new string[locData.Length];
Parallel.For(0, locData.Length, i =>
{
panoIDs[i] = Web.GetPanoID(locData[i], 50);
});
Parallel.For(0, panoIDs.Length, i =>
{
Panorama(panoIDs[i], folderPath + @"\image" + i + "." + format.ToString().ToLower(), format);
});
}
public static void Panorama(string panoID, string file, ImageFormat format)
{
Image[,] images = new Image[26, 13];
Parallel.For(0, 26, x => {
Parallel.For(0, 13, y =>{
using (WebClient client = new WebClient())
images[x, y] = Image.FromStream(new MemoryStream(client.DownloadData(Get.TileURL(panoID, x, y))));
});
});
using (Bitmap result = new Bitmap(26 * 512, 13 * 512))
{
for (int x = 0; x < 26; x++)
for (int y = 0; y < 13; y++)
using (Graphics g = Graphics.FromImage(result))
g.DrawImage(images[x, y], x * 512, y * 512);
result.Save(file, format);
}
}
I'm not sure what causes this problem so any help would be much appreciated.
The reason for this was that I was building in 32 bit, meaning the max memory I could use was 4GB. Changing it to 64 bit solved the problem.