Try to dispose IDisposable objects more diligently

master
Nathan Crawford 2016-03-31 20:49:39 -04:00
rodzic 89c706588a
commit 37c0b98487
3 zmienionych plików z 97 dodań i 41 usunięć

Wyświetl plik

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace embroideryReader
{
class GuiResources
{
/// uiFlags: 0 - Count of GDI objects
/// uiFlags: 1 - Count of USER objects
/// - Win32 GDI objects (pens, brushes, fonts, palettes, regions, device contexts, bitmap headers)
/// - Win32 USER objects:
/// - WIN32 resources (accelerator tables, bitmap resources, dialog box templates, font resources, menu resources, raw data resources, string table entries, message table entries, cursors/icons)
/// - Other USER objects (windows, menus)
///
[DllImport("User32")]
extern public static int GetGuiResources(IntPtr hProcess, int uiFlags);
public static int GetGuiResourcesGDICount()
{
return GetGuiResources(Process.GetCurrentProcess().Handle, 0);
}
public static int GetGuiResourcesUserCount()
{
return GetGuiResources(Process.GetCurrentProcess().Handle, 1);
}
}
}

Wyświetl plik

@ -88,6 +88,7 @@
<Compile Include="frmTextbox.Designer.cs">
<DependentUpon>frmTextbox.cs</DependentUpon>
</Compile>
<Compile Include="GuiResources.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Translation.cs" />

Wyświetl plik

@ -244,6 +244,13 @@ namespace embroideryReader
tempImage = destImage;
}
// About to abandon the current DrawArea object, dispose it now
if(DrawArea != null)
{
DrawArea.Dispose();
DrawArea = null;
}
// Add transparency grid
if (settings.transparencyGridEnabled)
{
@ -268,10 +275,15 @@ namespace embroideryReader
}
g.DrawImage(tempImage, 0, 0);
// Done with tempImage
tempImage.Dispose();
tempImage = null;
}
}
else
{
// Keeping the object tempImage was pointing at, so don't dispose it
DrawArea = tempImage;
}
@ -395,7 +407,11 @@ namespace embroideryReader
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show(String.Format(translation.GetTranslatedString(Translation.StringID.ABOUT_MESSAGE), currentVersion())); // "EmbroideryReader version " + currentVersion() + ". This program reads and displays embroidery designs from .PES files."
string message = String.Format(translation.GetTranslatedString(Translation.StringID.ABOUT_MESSAGE), currentVersion()); // "EmbroideryReader version " + currentVersion() + ". This program reads and displays embroidery designs from .PES files."
message += Environment.NewLine + Environment.NewLine + "GUI GDI count: " + GuiResources.GetGuiResourcesGDICount();
message += Environment.NewLine + "GUI USER count: " + GuiResources.GetGuiResourcesUserCount();
message += Environment.NewLine + ".Net framework version: " + Environment.Version;
MessageBox.Show(message);
}
private void checkForUpdateToolStripMenuItem_Click(object sender, EventArgs e)
@ -511,8 +527,10 @@ namespace embroideryReader
{
float inchesPerMM = 0.03937007874015748031496062992126f;
e.Graphics.ScaleTransform((float)(e.PageSettings.PrinterResolution.X * inchesPerMM * 0.01f), (float)(e.PageSettings.PrinterResolution.Y * inchesPerMM * 0.01f));
Bitmap tempDrawArea = design.designToBitmap((float)settings.threadThickness, settings.filterStiches, settings.filterStitchesThreshold, e.PageSettings.PrinterResolution.X * inchesPerMM * 0.2f);
e.Graphics.DrawImage(tempDrawArea, 30, 30);
using (Bitmap tempDrawArea = design.designToBitmap((float)settings.threadThickness, settings.filterStiches, settings.filterStitchesThreshold, e.PageSettings.PrinterResolution.X * inchesPerMM * 0.2f))
{
e.Graphics.DrawImage(tempDrawArea, 30, 30);
}
}
}
@ -527,12 +545,15 @@ namespace embroideryReader
if (DrawArea != null)
{
Clipboard.Clear();
Bitmap temp = new Bitmap(DrawArea.Width, DrawArea.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics tempGraph = Graphics.FromImage(temp);
tempGraph.FillRectangle(Brushes.White, 0, 0, temp.Width, temp.Height);
tempGraph.DrawImageUnscaled(DrawArea, 0, 0);
tempGraph.Dispose();
Clipboard.SetImage(temp);
using (Bitmap temp = new Bitmap(DrawArea.Width, DrawArea.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb))
{
using (Graphics tempGraph = Graphics.FromImage(temp))
{
tempGraph.FillRectangle(Brushes.White, 0, 0, temp.Width, temp.Height);
tempGraph.DrawImageUnscaled(DrawArea, 0, 0);
}
Clipboard.SetImage(temp);
}
}
}
@ -587,41 +608,43 @@ namespace embroideryReader
{
if (DrawArea != null)
{
Bitmap temp = new Bitmap(DrawArea.Width, DrawArea.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics tempGraph = Graphics.FromImage(temp);
tempGraph.FillRectangle(Brushes.White, 0, 0, temp.Width, temp.Height);
tempGraph.DrawImageUnscaled(DrawArea, 0, 0);
tempGraph.Dispose();
saveFileDialog1.FileName = "";
// "Bitmap (*.bmp)|*.bmp|PNG (*.png)|*.png|JPEG (*.jpg)|*.jpg|GIF (*.gif)|*.gif|TIFF (*.tif)|*.tif|All Files (*.*)|*.*"
saveFileDialog1.Filter = translation.GetTranslatedString(Translation.StringID.FILE_TYPE_BMP) + " (*.bmp)|*.bmp|" +
translation.GetTranslatedString(Translation.StringID.FILE_TYPE_PNG) + " (*.png)|*.png|" +
translation.GetTranslatedString(Translation.StringID.FILE_TYPE_JPG) + " (*.jpg)|*.jpg|" +
translation.GetTranslatedString(Translation.StringID.FILE_TYPE_GIF) + " (*.gif)|*.gif|" +
translation.GetTranslatedString(Translation.StringID.FILE_TYPE_TIFF) + " (*.tif)|*.tif|" +
translation.GetTranslatedString(Translation.StringID.FILE_TYPE_ALL) + " (*.*)|*.*";
if (settings.lastSaveImageLocation != null)
using (Bitmap temp = new Bitmap(DrawArea.Width, DrawArea.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb))
{
saveFileDialog1.InitialDirectory = settings.lastSaveImageLocation;
}
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string filename = "";
filename = saveFileDialog1.FileName;
System.Drawing.Imaging.ImageFormat format;
switch (System.IO.Path.GetExtension(filename).ToLower())
using (Graphics tempGraph = Graphics.FromImage(temp))
{
case ".bmp": format = System.Drawing.Imaging.ImageFormat.Bmp; break;
case ".png": format = System.Drawing.Imaging.ImageFormat.Png; break;
case ".jpg": format = System.Drawing.Imaging.ImageFormat.Jpeg; break;
case ".gif": format = System.Drawing.Imaging.ImageFormat.Gif; break;
case ".tif": format = System.Drawing.Imaging.ImageFormat.Tiff; break;
default: format = System.Drawing.Imaging.ImageFormat.Bmp; break;
tempGraph.FillRectangle(Brushes.White, 0, 0, temp.Width, temp.Height);
tempGraph.DrawImageUnscaled(DrawArea, 0, 0);
}
saveFileDialog1.FileName = "";
// "Bitmap (*.bmp)|*.bmp|PNG (*.png)|*.png|JPEG (*.jpg)|*.jpg|GIF (*.gif)|*.gif|TIFF (*.tif)|*.tif|All Files (*.*)|*.*"
saveFileDialog1.Filter = translation.GetTranslatedString(Translation.StringID.FILE_TYPE_BMP) + " (*.bmp)|*.bmp|" +
translation.GetTranslatedString(Translation.StringID.FILE_TYPE_PNG) + " (*.png)|*.png|" +
translation.GetTranslatedString(Translation.StringID.FILE_TYPE_JPG) + " (*.jpg)|*.jpg|" +
translation.GetTranslatedString(Translation.StringID.FILE_TYPE_GIF) + " (*.gif)|*.gif|" +
translation.GetTranslatedString(Translation.StringID.FILE_TYPE_TIFF) + " (*.tif)|*.tif|" +
translation.GetTranslatedString(Translation.StringID.FILE_TYPE_ALL) + " (*.*)|*.*";
if (settings.lastSaveImageLocation != null)
{
saveFileDialog1.InitialDirectory = settings.lastSaveImageLocation;
}
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string filename = "";
filename = saveFileDialog1.FileName;
System.Drawing.Imaging.ImageFormat format;
switch (System.IO.Path.GetExtension(filename).ToLower())
{
case ".bmp": format = System.Drawing.Imaging.ImageFormat.Bmp; break;
case ".png": format = System.Drawing.Imaging.ImageFormat.Png; break;
case ".jpg": format = System.Drawing.Imaging.ImageFormat.Jpeg; break;
case ".gif": format = System.Drawing.Imaging.ImageFormat.Gif; break;
case ".tif": format = System.Drawing.Imaging.ImageFormat.Tiff; break;
default: format = System.Drawing.Imaging.ImageFormat.Bmp; break;
}
temp.Save(filename, format);
showStatus(translation.GetTranslatedString(Translation.StringID.IMAGE_SAVED), 5000); // "Image saved"
settings.lastSaveImageLocation = System.IO.Path.GetDirectoryName(filename);
}
temp.Save(filename, format);
showStatus(translation.GetTranslatedString(Translation.StringID.IMAGE_SAVED), 5000); // "Image saved"
settings.lastSaveImageLocation = System.IO.Path.GetDirectoryName(filename);
}
}
}