Removed surface disposing, and making sure the PrintHelper itself is disposed correctly. Disposing the surface is not allowed in a destination, e.g. this would rip the surface away right from under the editor :)

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2503 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2013-02-20 09:16:28 +00:00
commit 0e86fa409c
2 changed files with 90 additions and 92 deletions

View file

@ -106,15 +106,21 @@ namespace Greenshot.Destinations {
/// <param name="captureDetails"></param> /// <param name="captureDetails"></param>
/// <returns>ExportInformation</returns> /// <returns>ExportInformation</returns>
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) { public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description); ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description);
PrinterSettings printerSettings = null; PrinterSettings printerSettings = null;
if (!string.IsNullOrEmpty(printerName)) { if (!string.IsNullOrEmpty(printerName)) {
printerSettings = new PrintHelper(surface, captureDetails).PrintTo(printerName); using (PrintHelper printHelper = new PrintHelper(surface, captureDetails)) {
printerSettings = printHelper.PrintTo(printerName);
}
} else if (!manuallyInitiated) { } else if (!manuallyInitiated) {
PrinterSettings settings = new PrinterSettings(); PrinterSettings settings = new PrinterSettings();
printerSettings = new PrintHelper(surface, captureDetails).PrintTo(settings.PrinterName); using (PrintHelper printHelper = new PrintHelper(surface, captureDetails)) {
printerSettings = printHelper.PrintTo(settings.PrinterName);
}
} else { } else {
printerSettings = new PrintHelper(surface, captureDetails).PrintWithDialog(); using (PrintHelper printHelper = new PrintHelper(surface, captureDetails)) {
printerSettings = printHelper.PrintWithDialog();
}
} }
if (printerSettings != null) { if (printerSettings != null) {
exportInformation.ExportMade = true; exportInformation.ExportMade = true;

View file

@ -39,13 +39,13 @@ namespace Greenshot.Helpers {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PrintHelper)); private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PrintHelper));
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>(); private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private ISurface surface; private ISurface surface;
private ICaptureDetails captureDetails; private ICaptureDetails captureDetails;
private PrintDocument printDocument = new PrintDocument(); private PrintDocument printDocument = new PrintDocument();
private PrintDialog printDialog = new PrintDialog(); private PrintDialog printDialog = new PrintDialog();
public PrintHelper(ISurface surface, ICaptureDetails captureDetails) { public PrintHelper(ISurface surface, ICaptureDetails captureDetails) {
this.surface = surface; this.surface = surface;
this.captureDetails = captureDetails; this.captureDetails = captureDetails;
printDialog.UseEXDialog = true; printDialog.UseEXDialog = true;
printDocument.DocumentName = FilenameHelper.GetFilenameWithoutExtensionFromPattern(conf.OutputFileFilenamePattern, captureDetails); printDocument.DocumentName = FilenameHelper.GetFilenameWithoutExtensionFromPattern(conf.OutputFileFilenamePattern, captureDetails);
@ -75,9 +75,6 @@ namespace Greenshot.Helpers {
*/ */
protected virtual void Dispose(bool disposing) { protected virtual void Dispose(bool disposing) {
if (disposing) { if (disposing) {
if (surface != null) {
surface.Dispose();
}
if (printDocument != null) { if (printDocument != null) {
printDocument.Dispose(); printDocument.Dispose();
} }
@ -85,7 +82,7 @@ namespace Greenshot.Helpers {
printDialog.Dispose(); printDialog.Dispose();
} }
} }
surface = null; surface = null;
printDocument = null; printDocument = null;
printDialog = null; printDialog = null;
} }
@ -111,8 +108,6 @@ namespace Greenshot.Helpers {
LOG.Error("An error ocurred while trying to print", e); LOG.Error("An error ocurred while trying to print", e);
MessageBox.Show(Language.GetString(LangKey.print_error), Language.GetString(LangKey.error)); MessageBox.Show(Language.GetString(LangKey.print_error), Language.GetString(LangKey.error));
} }
surface.Dispose();
surface = null;
return returnPrinterSettings; return returnPrinterSettings;
} }
@ -127,7 +122,7 @@ namespace Greenshot.Helpers {
DialogResult? printOptionsResult = ShowPrintOptionsDialog(); DialogResult? printOptionsResult = ShowPrintOptionsDialog();
try { try {
if (printOptionsResult == null || printOptionsResult == DialogResult.OK) { if (printOptionsResult == null || printOptionsResult == DialogResult.OK) {
if (IsColorPrint()) { if (IsColorPrint()) {
printDocument.DefaultPageSettings.Color = false; printDocument.DefaultPageSettings.Color = false;
} }
printDocument.Print(); printDocument.Print();
@ -139,14 +134,12 @@ namespace Greenshot.Helpers {
} }
} }
surface.Dispose();
surface = null;
return returnPrinterSettings; return returnPrinterSettings;
} }
private bool IsColorPrint() { private bool IsColorPrint() {
return !conf.OutputPrintGrayscale && !conf.OutputPrintMonochrome; return !conf.OutputPrintGrayscale && !conf.OutputPrintMonochrome;
} }
/// <summary> /// <summary>
/// display print options dialog (if the user has not configured Greenshot not to) /// display print options dialog (if the user has not configured Greenshot not to)
@ -165,93 +158,92 @@ namespace Greenshot.Helpers {
void DrawImageForPrint(object sender, PrintPageEventArgs e) { void DrawImageForPrint(object sender, PrintPageEventArgs e) {
// Create the output settins // Create the output settins
SurfaceOutputSettings printOutputSettings = new SurfaceOutputSettings(OutputFormat.png, 100, false); SurfaceOutputSettings printOutputSettings = new SurfaceOutputSettings(OutputFormat.png, 100, false);
ApplyEffects(printOutputSettings); ApplyEffects(printOutputSettings);
Image image; Image image;
Boolean disposeImage = ImageOutput.CreateImageFromSurface(surface, printOutputSettings, out image); bool disposeImage = ImageOutput.CreateImageFromSurface(surface, printOutputSettings, out image);
try { try {
ContentAlignment alignment = conf.OutputPrintCenter ? ContentAlignment.MiddleCenter : ContentAlignment.TopLeft; ContentAlignment alignment = conf.OutputPrintCenter ? ContentAlignment.MiddleCenter : ContentAlignment.TopLeft;
// prepare timestamp // prepare timestamp
float footerStringWidth = 0; float footerStringWidth = 0;
float footerStringHeight = 0; float footerStringHeight = 0;
string footerString = null; //DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString(); string footerString = null; //DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
if (conf.OutputPrintFooter) { if (conf.OutputPrintFooter) {
footerString = FilenameHelper.FillPattern(conf.OutputPrintFooterPattern, captureDetails, false); footerString = FilenameHelper.FillPattern(conf.OutputPrintFooterPattern, captureDetails, false);
using (Font f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular)) { using (Font f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular)) {
footerStringWidth = e.Graphics.MeasureString(footerString, f).Width; footerStringWidth = e.Graphics.MeasureString(footerString, f).Width;
footerStringHeight = e.Graphics.MeasureString(footerString, f).Height; footerStringHeight = e.Graphics.MeasureString(footerString, f).Height;
} }
} }
// Get a rectangle representing the printable Area // Get a rectangle representing the printable Area
RectangleF pageRect = e.PageSettings.PrintableArea; RectangleF pageRect = e.PageSettings.PrintableArea;
if(e.PageSettings.Landscape) { if(e.PageSettings.Landscape) {
float origWidth = pageRect.Width; float origWidth = pageRect.Width;
pageRect.Width = pageRect.Height; pageRect.Width = pageRect.Height;
pageRect.Height = origWidth; pageRect.Height = origWidth;
} }
// Subtract the dateString height from the available area, this way the area stays free // Subtract the dateString height from the available area, this way the area stays free
pageRect.Height -= footerStringHeight; pageRect.Height -= footerStringHeight;
GraphicsUnit gu = GraphicsUnit.Pixel; GraphicsUnit gu = GraphicsUnit.Pixel;
RectangleF imageRect = image.GetBounds(ref gu); RectangleF imageRect = image.GetBounds(ref gu);
// rotate the image if it fits the page better // rotate the image if it fits the page better
if (conf.OutputPrintAllowRotate) { if (conf.OutputPrintAllowRotate) {
if ((pageRect.Width > pageRect.Height && imageRect.Width < imageRect.Height) || (pageRect.Width < pageRect.Height && imageRect.Width > imageRect.Height)) { if ((pageRect.Width > pageRect.Height && imageRect.Width < imageRect.Height) || (pageRect.Width < pageRect.Height && imageRect.Width > imageRect.Height)) {
image.RotateFlip(RotateFlipType.Rotate90FlipNone); image.RotateFlip(RotateFlipType.Rotate90FlipNone);
imageRect = image.GetBounds(ref gu); imageRect = image.GetBounds(ref gu);
if (alignment.Equals(ContentAlignment.TopLeft)) { if (alignment.Equals(ContentAlignment.TopLeft)) {
alignment = ContentAlignment.TopRight; alignment = ContentAlignment.TopRight;
} }
} }
} }
RectangleF printRect = new RectangleF(0, 0, imageRect.Width, imageRect.Height); RectangleF printRect = new RectangleF(0, 0, imageRect.Width, imageRect.Height);
// scale the image to fit the page better // scale the image to fit the page better
if (conf.OutputPrintAllowEnlarge || conf.OutputPrintAllowShrink) { if (conf.OutputPrintAllowEnlarge || conf.OutputPrintAllowShrink) {
SizeF resizedRect = ScaleHelper.GetScaledSize(imageRect.Size, pageRect.Size, false); SizeF resizedRect = ScaleHelper.GetScaledSize(imageRect.Size, pageRect.Size, false);
if ((conf.OutputPrintAllowShrink && resizedRect.Width < printRect.Width) || conf.OutputPrintAllowEnlarge && resizedRect.Width > printRect.Width) { if ((conf.OutputPrintAllowShrink && resizedRect.Width < printRect.Width) || conf.OutputPrintAllowEnlarge && resizedRect.Width > printRect.Width) {
printRect.Size = resizedRect; printRect.Size = resizedRect;
} }
} }
// align the image // align the image
printRect = ScaleHelper.GetAlignedRectangle(printRect, new RectangleF(0, 0, pageRect.Width, pageRect.Height), alignment); printRect = ScaleHelper.GetAlignedRectangle(printRect, new RectangleF(0, 0, pageRect.Width, pageRect.Height), alignment);
if (conf.OutputPrintFooter) { if (conf.OutputPrintFooter) {
//printRect = new RectangleF(0, 0, printRect.Width, printRect.Height - (dateStringHeight * 2)); //printRect = new RectangleF(0, 0, printRect.Width, printRect.Height - (dateStringHeight * 2));
using (Font f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular)) { using (Font f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular)) {
e.Graphics.DrawString(footerString, f, Brushes.Black, pageRect.Width / 2 - (footerStringWidth / 2), pageRect.Height); e.Graphics.DrawString(footerString, f, Brushes.Black, pageRect.Width / 2 - (footerStringWidth / 2), pageRect.Height);
} }
} }
e.Graphics.DrawImage(image, printRect, imageRect, GraphicsUnit.Pixel); e.Graphics.DrawImage(image, printRect, imageRect, GraphicsUnit.Pixel);
} finally { } finally {
if (disposeImage && image != null) { if (disposeImage && image != null) {
image.Dispose(); image.Dispose();
image = null; image = null;
} }
} }
} }
private void ApplyEffects(SurfaceOutputSettings printOutputSettings) private void ApplyEffects(SurfaceOutputSettings printOutputSettings) {
{ // TODO:
// TODO: // add effects here
// add effects here if (conf.OutputPrintMonochrome) {
if (conf.OutputPrintMonochrome) { byte threshold = conf.OutputPrintMonochromeThreshold;
byte threshold = conf.OutputPrintMonochromeThreshold; printOutputSettings.Effects.Add(new MonochromeEffect(threshold));
printOutputSettings.Effects.Add(new MonochromeEffect(threshold)); printOutputSettings.ReduceColors = true;
printOutputSettings.ReduceColors = true; }
}
// the invert effect should probably be the last // the invert effect should probably be the last
if (conf.OutputPrintInverted) { if (conf.OutputPrintInverted) {
printOutputSettings.Effects.Add(new InvertEffect()); printOutputSettings.Effects.Add(new InvertEffect());
} }
} }
} }
} }