From 75f117484b8e45d0db4273f37cc993bf02f00fcb Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Fri, 20 Apr 2012 03:57:22 +0100 Subject: 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. --- .../Terrain/FileLoaders/GenericSystemDrawing.cs | 59 ++++++++++++++-------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs index 3921bf9..039c3fa 100644 --- a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs +++ b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs @@ -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." string tempName = Path.GetTempFileName(); - Bitmap entireBitmap = null; - Bitmap thisBitmap = null; - if (File.Exists(filename)) + Bitmap existingBitmap = null; + Bitmap thisBitmap; + Bitmap newBitmap; + + try { - File.Copy(filename, tempName, true); - entireBitmap = new Bitmap(tempName); - if (entireBitmap.Width != fileWidth * regionSizeX || entireBitmap.Height != fileHeight * regionSizeY) + if (File.Exists(filename)) { - // old file, let's overwrite it - entireBitmap = new Bitmap(fileWidth * regionSizeX, fileHeight * regionSizeY); + File.Copy(filename, tempName, true); + 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); - } - - 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)); + if (existingBitmap != null) + existingBitmap.Dispose(); - Save(entireBitmap, filename); - thisBitmap.Dispose(); - entireBitmap.Dispose(); + thisBitmap.Dispose(); + newBitmap.Dispose(); - if (File.Exists(tempName)) - File.Delete(tempName); + if (File.Exists(tempName)) + File.Delete(tempName); + } } protected virtual void Save(Bitmap bmp, string filename) -- cgit v1.1