Fixed Bug #3482709, when using a time stamp on the page the print is cropped. (quick fix, as the problem is most likely in the GetAlignedRectangle)

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1617 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-02-01 15:01:22 +00:00
commit 6fb498f633

View file

@ -39,12 +39,14 @@ namespace Greenshot.Helpers {
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>(); private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private Image image; private Image image;
private ICaptureDetails captureDetails;
private PrintDocument printDocument = new PrintDocument(); private PrintDocument printDocument = new PrintDocument();
private PrintDialog printDialog = new PrintDialog(); private PrintDialog printDialog = new PrintDialog();
private PrintOptionsDialog printOptionsDialog = null; private PrintOptionsDialog printOptionsDialog = null;
public PrintHelper(Image image, ICaptureDetails captureDetails) { public PrintHelper(Image image, ICaptureDetails captureDetails) {
this.image = image; this.image = image;
this.captureDetails = captureDetails;
printDialog.UseEXDialog = true; printDialog.UseEXDialog = true;
printDocument.DocumentName = FilenameHelper.GetFilenameWithoutExtensionFromPattern(conf.OutputFileFilenamePattern, captureDetails); printDocument.DocumentName = FilenameHelper.GetFilenameWithoutExtensionFromPattern(conf.OutputFileFilenamePattern, captureDetails);
printDocument.PrintPage += DrawImageForPrint; printDocument.PrintPage += DrawImageForPrint;
@ -73,10 +75,18 @@ namespace Greenshot.Helpers {
*/ */
protected void Dispose(bool disposing) { protected void Dispose(bool disposing) {
if (disposing) { if (disposing) {
if(image != null) image.Dispose(); if (image != null) {
if(printDocument != null) printDocument.Dispose(); image.Dispose();
if(printDialog != null) printDialog.Dispose(); }
if(printOptionsDialog != null) printOptionsDialog.Dispose(); if (printDocument != null) {
printDocument.Dispose();
}
if (printDialog != null) {
printDialog.Dispose();
}
if (printOptionsDialog != null) {
printOptionsDialog.Dispose();
}
} }
image = null; image = null;
printDocument = null; printDocument = null;
@ -119,11 +129,21 @@ namespace Greenshot.Helpers {
void DrawImageForPrint(object sender, PrintPageEventArgs e) { void DrawImageForPrint(object sender, PrintPageEventArgs e) {
PrintOptionsDialog pod = printOptionsDialog; PrintOptionsDialog pod = printOptionsDialog;
ContentAlignment alignment = pod.AllowPrintCenter ? ContentAlignment.MiddleCenter : ContentAlignment.TopLeft; ContentAlignment alignment = pod.AllowPrintCenter ? ContentAlignment.MiddleCenter : ContentAlignment.TopLeft;
// prepare timestamp
float dateStringWidth = 0;
float dateStringHeight = 0;
string dateString = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
if (conf.OutputPrintTimestamp) {
using (Font f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular)) {
dateStringWidth = e.Graphics.MeasureString(dateString, f).Width;
dateStringHeight = e.Graphics.MeasureString(dateString, f).Height;
}
}
// Invert Bitmap if wanted
if (conf.OutputPrintInverted) { if (conf.OutputPrintInverted) {
// Invert Bitmap
using (BitmapBuffer bb = new BitmapBuffer((Bitmap)image, false)) { using (BitmapBuffer bb = new BitmapBuffer((Bitmap)image, false)) {
bb.Lock(); bb.Lock();
for (int y = 0; y < bb.Height; y++) { for (int y = 0; y < bb.Height; y++) {
@ -136,45 +156,44 @@ namespace Greenshot.Helpers {
} }
} }
// Get a rectangle representing the printable Area
RectangleF pageRect = e.PageSettings.PrintableArea; RectangleF pageRect = e.PageSettings.PrintableArea;
// Subtract the dateString height from the available area, this way the area stays free
pageRect.Height -= dateStringHeight;
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 (pod.AllowPrintRotate) { if (pod.AllowPrintRotate) {
if((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)) {
(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)) alignment = ContentAlignment.TopRight; if (alignment.Equals(ContentAlignment.TopLeft)) {
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 (pod.AllowPrintEnlarge || pod.AllowPrintShrink) { if (pod.AllowPrintEnlarge || pod.AllowPrintShrink) {
SizeF resizedRect = ScaleHelper.GetScaledSize(imageRect.Size, pageRect.Size, false); SizeF resizedRect = ScaleHelper.GetScaledSize(imageRect.Size, pageRect.Size, false);
if((pod.AllowPrintShrink && resizedRect.Width < printRect.Width) || if ((pod.AllowPrintShrink && resizedRect.Width < printRect.Width) || pod.AllowPrintEnlarge && resizedRect.Width > printRect.Width) {
pod.AllowPrintEnlarge && resizedRect.Width > printRect.Width) {
printRect.Size = resizedRect; printRect.Size = resizedRect;
} }
}
// prepare timestamp
float dateStringWidth = 0;
float dateStringHeight = 0;
if (conf.OutputPrintTimestamp) {
Font f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
string dateString = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
dateStringWidth = e.Graphics.MeasureString(dateString, f).Width;
dateStringHeight = e.Graphics.MeasureString(dateString, f).Height;
e.Graphics.DrawString(dateString, f, Brushes.Black, pageRect.Width / 2 - (dateStringWidth / 2), pageRect.Height - dateStringHeight);
} }
// align the image // align the image
printRect = ScaleHelper.GetAlignedRectangle(printRect, new RectangleF(0, 0, pageRect.Width, pageRect.Height - dateStringHeight * 2), alignment); printRect = ScaleHelper.GetAlignedRectangle(printRect, new RectangleF(0, 0, pageRect.Width, pageRect.Height), alignment);
if (conf.OutputPrintTimestamp) {
//printRect = new RectangleF(0, 0, printRect.Width, printRect.Height - (dateStringHeight * 2));
using (Font f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular)) {
e.Graphics.DrawString(dateString, f, Brushes.Black, pageRect.Width / 2 - (dateStringWidth / 2), pageRect.Height);
}
}
e.Graphics.DrawImage(image, printRect, imageRect, GraphicsUnit.Pixel); e.Graphics.DrawImage(image, printRect, imageRect, GraphicsUnit.Pixel);
} }
} }
} }