Always dispose of existing opened bitmap from file in SaveFile(), instead of simply dropping the reference if the existing file didn't contain a bitmap of the same size.

0.7.4.1
Justin Clark-Casey (justincc) 2012-04-20 03:57:22 +01:00
parent cba64ebc79
commit 75f117484b
1 changed files with 38 additions and 23 deletions

View File

@ -138,35 +138,50 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
// "Saving the image to the same file it was constructed from is not allowed and throws an exception." // "Saving the image to the same file it was constructed from is not allowed and throws an exception."
string tempName = Path.GetTempFileName(); string tempName = Path.GetTempFileName();
Bitmap entireBitmap = null; Bitmap existingBitmap = null;
Bitmap thisBitmap = null; Bitmap thisBitmap;
if (File.Exists(filename)) Bitmap newBitmap;
try
{ {
File.Copy(filename, tempName, true); if (File.Exists(filename))
entireBitmap = new Bitmap(tempName);
if (entireBitmap.Width != fileWidth * regionSizeX || entireBitmap.Height != fileHeight * regionSizeY)
{ {
// old file, let's overwrite it File.Copy(filename, tempName, true);
entireBitmap = new Bitmap(fileWidth * regionSizeX, fileHeight * regionSizeY); existingBitmap = new Bitmap(tempName);
if (existingBitmap.Width != fileWidth * regionSizeX || existingBitmap.Height != fileHeight * regionSizeY)
{
// old file, let's overwrite it
newBitmap = new Bitmap(fileWidth * regionSizeX, fileHeight * regionSizeY);
}
else
{
newBitmap = existingBitmap;
}
} }
else
{
newBitmap = new Bitmap(fileWidth * regionSizeX, fileHeight * regionSizeY);
}
thisBitmap = CreateGrayscaleBitmapFromMap(m_channel);
// Console.WriteLine("offsetX=" + offsetX + " offsetY=" + offsetY);
for (int x = 0; x < regionSizeX; x++)
for (int y = 0; y < regionSizeY; y++)
newBitmap.SetPixel(x + offsetX * regionSizeX, y + (fileHeight - 1 - offsetY) * regionSizeY, thisBitmap.GetPixel(x, y));
Save(newBitmap, filename);
} }
else finally
{ {
entireBitmap = new Bitmap(fileWidth * regionSizeX, fileHeight * regionSizeY); if (existingBitmap != null)
existingBitmap.Dispose();
thisBitmap.Dispose();
newBitmap.Dispose();
if (File.Exists(tempName))
File.Delete(tempName);
} }
thisBitmap = CreateGrayscaleBitmapFromMap(m_channel);
// Console.WriteLine("offsetX=" + offsetX + " offsetY=" + offsetY);
for (int x = 0; x < regionSizeX; x++)
for (int y = 0; y < regionSizeY; y++)
entireBitmap.SetPixel(x + offsetX * regionSizeX, y + (fileHeight - 1 - offsetY) * regionSizeY, thisBitmap.GetPixel(x, y));
Save(entireBitmap, filename);
thisBitmap.Dispose();
entireBitmap.Dispose();
if (File.Exists(tempName))
File.Delete(tempName);
} }
protected virtual void Save(Bitmap bmp, string filename) protected virtual void Save(Bitmap bmp, string filename)