Skip to content

Commit 1caceb7

Browse files
committed
refactor screenshot/bitmap handling to immediately dispose when done
1 parent 6733605 commit 1caceb7

File tree

4 files changed

+15
-65
lines changed

4 files changed

+15
-65
lines changed

src/ExceptionReporter/Core/ScreenshotTaker.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ namespace ExceptionReporting.Core
77
{
88
internal interface IScreenshotTaker
99
{
10-
Bitmap TakeScreenShot();
11-
string GetImageAsFile(Bitmap bitmap);
10+
string TakeScreenShot();
1211
}
1312

1413
/// <summary>
@@ -19,8 +18,8 @@ internal class ScreenshotTaker : IScreenshotTaker
1918
private const string ScreenshotFileName = "exceptionreport-screenshot.jpg";
2019

2120
/// <summary> Take a screenshot (supports multiple monitors) </summary>
22-
/// <returns>Bitmap of the screen, as at the time called</returns>
23-
public Bitmap TakeScreenShot()
21+
/// <returns>temp file name of JPEG image</returns>
22+
public string TakeScreenShot()
2423
{
2524
if (ExceptionReporter.IsRunningMono()) return null;
2625

@@ -31,24 +30,25 @@ public Bitmap TakeScreenShot()
3130
rectangle = Rectangle.Union(rectangle, screen.Bounds);
3231
}
3332

34-
var bitmap = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format32bppArgb);
35-
36-
using (var graphics = Graphics.FromImage(bitmap))
33+
using (var bitmap = new Bitmap(rectangle.Width, rectangle.Height, PixelFormat.Format32bppArgb))
3734
{
38-
graphics.CopyFromScreen(rectangle.X, rectangle.Y, 0, 0, rectangle.Size, CopyPixelOperation.SourceCopy);
39-
}
4035

41-
return bitmap;
36+
using (var graphics = Graphics.FromImage(bitmap))
37+
{
38+
graphics.CopyFromScreen(rectangle.X, rectangle.Y, 0, 0, rectangle.Size, CopyPixelOperation.SourceCopy);
39+
}
40+
41+
return GetImageAsFile(bitmap);
42+
}
4243
}
4344

4445
/// <summary>
4546
/// Return the supplied Bitmap, as a file on the system, in JPEG format
4647
/// </summary>
4748
/// <param name="bitmap">The Bitmap to save</param>
48-
/// <returns></returns>
49-
public string GetImageAsFile(Bitmap bitmap)
49+
private static string GetImageAsFile(Image bitmap)
5050
{
51-
var tempFileName = Path.GetTempPath() + ScreenshotFileName; // the image is not deleted but the same file is reused every time
51+
var tempFileName = Path.GetTempPath() + ScreenshotFileName; // image is not deleted but same file is reused
5252
bitmap.Save(tempFileName, ImageFormat.Jpeg);
5353
return tempFileName;
5454
}

src/ExceptionReporter/ExceptionReportInfo.cs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -216,24 +216,11 @@ public bool ShowEmailButton
216216
/// </summary>
217217
public bool TakeScreenshot { get; set; } = false;
218218

219-
/// <summary>
220-
/// The Screenshot Bitmap, used internally but exposed for flexibility
221-
/// </summary>
222-
public Bitmap ScreenshotImage { get; set; }
223-
224219
/// <summary>
225220
/// The method used to send the report
226221
/// </summary>
227222
public ReportSendMethod SendMethod { get; set; } = ReportSendMethod.None;
228223

229-
/// <summary>
230-
/// Whether a screenshot is configured to be taken and that it has been taken - used internally
231-
/// </summary>
232-
public bool ScreenshotAvailable
233-
{
234-
get { return TakeScreenshot && ScreenshotImage != null; }
235-
}
236-
237224
/// <summary>
238225
/// Show the Exception Reporter as a "TopMost" window (ie TopMost property on a WinForm)
239226
/// This can be quite important in some environments (eg Office Addins) where it might get covered by other UI
@@ -314,12 +301,6 @@ public enum EmailMethod
314301

315302
[Obsolete("use 'SendMethod' property instead")]
316303
public EmailMethod MailMethod { get; set; }
317-
318-
protected override void DisposeManagedResources()
319-
{
320-
ScreenshotImage?.Dispose();
321-
base.DisposeManagedResources();
322-
}
323304
}
324305

325306
/// <summary>

src/ExceptionReporter/Mail/Attacher.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,8 @@ public void AttachFiles(IAttach attacher)
2929

3030
try
3131
{
32-
if (_config.TakeScreenshot && !_config.ScreenshotAvailable)
33-
_config.ScreenshotImage = ScreenshotTaker.TakeScreenShot();
34-
}
35-
catch { /* ignored */ }
36-
37-
if (_config.ScreenshotAvailable)
38-
{
39-
files.Add(ScreenshotTaker.GetImageAsFile(_config.ScreenshotImage));
40-
}
32+
if (_config.TakeScreenshot) files.Add(ScreenshotTaker.TakeScreenShot());
33+
} catch { /* ignored */ }
4134

4235
var filesThatExist = files.Where(f => File.Exists(f)).ToList();
4336

src/Test.ExceptionReporter/Attacher_Tests.cs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Drawing;
21
using ExceptionReporting.Core;
32
using ExceptionReporting.Mail;
43
using Moq;
@@ -144,28 +143,5 @@ public void Should_Not_Take_Screenshot_If_Not_Configured()
144143

145144
screenshot.Verify(s => s.TakeScreenShot(), Times.Never);
146145
}
147-
148-
/// <summary>
149-
/// There's not actually a pathway in the code to have a screenshot "already taken" - but I think this is a worthwhile guard
150-
/// </summary>
151-
[Test]
152-
public void Should_Not_Take_Screenshot_If_Already_Taken()
153-
{
154-
var screenshot = new Mock<IScreenshotTaker>();
155-
156-
using (var bm = new Bitmap(1, 1))
157-
{
158-
var attacher =
159-
new Attacher(new ExceptionReportInfo
160-
{
161-
TakeScreenshot = true,
162-
ScreenshotImage = bm // this makes the screenshot "already taken" ie exists
163-
}) { ScreenshotTaker = screenshot.Object };
164-
165-
attacher.AttachFiles(_iattach.Object);
166-
}
167-
168-
screenshot.Verify(s => s.TakeScreenShot(), Times.Never);
169-
}
170146
}
171147
}

0 commit comments

Comments
 (0)