[c#] A generic error occurred in GDI+, JPEG Image to MemoryStream

My console app got the same error message: "A generic error occurred in GDI+." The error happened in line newImage.Save as refer to the following code.

for (int i = 1; i <= 1000; i++)
{
   Image newImage = Image.FromFile(@"Sample.tif");
   //...some logic here
   newImage.Save(i + ".tif", , ImageFormat.Tiff);
}

The program returned error when the RAM usage is around 4GB, and managed to solve it by changed the Program Target to x64 in project properties.