c# - using(Bitmap) 偶尔并行抛出异常
问题描述
我正在尝试一次从街景全景图中下载并保存多个 bmp。在正常的 for 循环中使用 Panorama() 可以正常工作,但是当我将它放在 Parallel.For 中时,它会在大约 20 张带有无效参数异常的图像后抛出异常,using (Bitmap result = new Bitmap(26 * 512, 13 * 512))
并被高亮显示。崩溃时内存使用量为 4GB,但这应该不是问题,因为我有 16GB 的 RAM。
这是我的代码:
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);
}
}
我不确定是什么导致了这个问题,所以任何帮助都将不胜感激。
解决方案
这样做的原因是我正在构建 32 位,这意味着我可以使用的最大内存是 4GB。将其更改为 64 位解决了问题。
推荐阅读
- node.js - 为什么即使设置了 mimeType,下载的驱动器电子表格也不包含任何数据?
- css - div 是否可以使用基于像素的边距填充整个视口,而不使用 CSS3 calc 属性?
- python - 遍历python中的排序嵌套字典
- r - Purrr 和映射向量输出函数
- python - 将 c 模块的 stderr 输出重定向到自定义 python 编写器
- go - 通过 SSH golang 连接到 mongoDB
- javascript - 使用 Javascript 创建的 SVG 路径没有宽度或高度
- php - 我正在尝试将批处理嵌入到 php 中,但它没有执行
- angular - 角料台性能
- tensorflow - 当使用全部 4 个 GPU 时,Tensorflow 的 Multi_Gpu_Model 会导致 Ubuntu 18.04 操作系统崩溃