Changes for Thomas & fixed undo/redo for background changes

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1647 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-02-09 15:30:27 +00:00
commit 46758d238c
8 changed files with 164 additions and 31 deletions

View file

@ -132,6 +132,16 @@ namespace Greenshot.Destinations {
effectSubItem.Click += delegate { effectSubItem.Click += delegate {
surface.ApplyBitmapEffect(Effects.TornEdge); surface.ApplyBitmapEffect(Effects.TornEdge);
}; };
effectSubItem = new ToolStripMenuItem("Border");
effectItem.DropDownItems.Add(effectSubItem);
effectSubItem.Click += delegate {
surface.ApplyBitmapEffect(Effects.Border);
};
effectSubItem = new ToolStripMenuItem("Grayscale");
effectItem.DropDownItems.Add(effectSubItem);
effectSubItem.Click += delegate {
surface.ApplyBitmapEffect(Effects.Grayscale);
};
}; };
} }
// Close // Close

View file

@ -552,20 +552,28 @@ namespace Greenshot.Drawing {
Point offset = Point.Empty; Point offset = Point.Empty;
switch (effect) { switch (effect) {
case Effects.Shadow: case Effects.Shadow:
newImage = ImageHelper.CreateShadow((Bitmap)Image, 1f, 10, Image.PixelFormat, out offset); offset = new Point(6, 6);
newImage = ImageHelper.CreateShadow((Bitmap)Image, 1f, 7, offset, PixelFormat.Format24bppRgb); //Image.PixelFormat);
break; break;
case Effects.TornEdge: case Effects.TornEdge:
offset = new Point(5, 5);
using (Bitmap tmpImage = ImageHelper.CreateTornEdge((Bitmap)Image)) { using (Bitmap tmpImage = ImageHelper.CreateTornEdge((Bitmap)Image)) {
newImage = ImageHelper.CreateShadow(tmpImage, 1f, 8, Image.PixelFormat, out offset); newImage = ImageHelper.CreateShadow(tmpImage, 1f, 6, offset, PixelFormat.Format24bppRgb); //Image.PixelFormat);
} }
break; break;
case Effects.Border:
newImage = ImageHelper.CreateBorder((Bitmap)Image, 2, Color.Black, Image.PixelFormat, out offset);
break;
case Effects.Grayscale:
newImage = ImageHelper.CreateGrayscale((Bitmap)Image);
break;
} }
if (newImage != null) { if (newImage != null) {
// Make sure the elements move according to the offset the effect made the bitmap move // Make sure the elements move according to the offset the effect made the bitmap move
elements.MoveBy(offset.X, offset.Y); elements.MoveBy(offset.X, offset.Y);
// Make undoable // Make undoable
MakeUndoable(new SurfaceCropMemento(this, imageRectangle), false); MakeUndoable(new SurfaceBackgroundChangeMemento(this, offset), false);
SetImage(newImage, false); SetImage(newImage, false);
Invalidate(); Invalidate();
} }
@ -600,11 +608,12 @@ namespace Greenshot.Drawing {
Bitmap tmpImage = ((Bitmap)Image).Clone(cropRectangle, Image.PixelFormat); Bitmap tmpImage = ((Bitmap)Image).Clone(cropRectangle, Image.PixelFormat);
tmpImage.SetResolution(Image.HorizontalResolution, Image.VerticalResolution); tmpImage.SetResolution(Image.HorizontalResolution, Image.VerticalResolution);
Point offset = new Point(-cropRectangle.Left, -cropRectangle.Top);
// Make undoable // Make undoable
MakeUndoable(new SurfaceCropMemento(this, cropRectangle), false); MakeUndoable(new SurfaceBackgroundChangeMemento(this, offset), false);
SetImage(tmpImage, false); SetImage(tmpImage, false);
elements.MoveBy(-cropRectangle.Left, -cropRectangle.Top); elements.MoveBy(offset.X, offset.Y);
if (SurfaceSizeChanged != null) { if (SurfaceSizeChanged != null) {
SurfaceSizeChanged(this); SurfaceSizeChanged(this);
} }
@ -613,10 +622,10 @@ namespace Greenshot.Drawing {
} }
return false; return false;
} }
public void UndoCrop(Image previous, Rectangle cropRectangle) { public void UndoBackgroundChange(Image previous, Point offset) {
SetImage(previous, false); SetImage(previous, false);
elements.MoveBy(cropRectangle.Left, cropRectangle.Top); elements.MoveBy(offset.X, offset.Y);
if (SurfaceSizeChanged != null) { if (SurfaceSizeChanged != null) {
SurfaceSizeChanged(this); SurfaceSizeChanged(this);
} }

View file

@ -197,7 +197,7 @@
<Compile Include="Memento\TextChangeMemento.cs" /> <Compile Include="Memento\TextChangeMemento.cs" />
<Compile Include="Memento\IMemento.cs" /> <Compile Include="Memento\IMemento.cs" />
<Compile Include="Memento\DrawableContainerBoundsChangeMemento.cs" /> <Compile Include="Memento\DrawableContainerBoundsChangeMemento.cs" />
<Compile Include="Memento\SurfaceCropMemento.cs" /> <Compile Include="Memento\SurfaceBackgroundChangeMemento.cs" />
<Compile Include="Helpers\OfficeInterop\OfficeWrappers.cs" /> <Compile Include="Helpers\OfficeInterop\OfficeWrappers.cs" />
<Compile Include="Helpers\OfficeInterop\OutlookWrapper.cs" /> <Compile Include="Helpers\OfficeInterop\OutlookWrapper.cs" />
<Compile Include="Helpers\OfficeInterop\PowerpointWrapper.cs" /> <Compile Include="Helpers\OfficeInterop\PowerpointWrapper.cs" />

View file

@ -28,15 +28,15 @@ namespace Greenshot.Memento {
/// <summary> /// <summary>
/// The SurfaceCropMemento makes it possible to undo-redo an surface crop /// The SurfaceCropMemento makes it possible to undo-redo an surface crop
/// </summary> /// </summary>
public class SurfaceCropMemento : IMemento { public class SurfaceBackgroundChangeMemento : IMemento {
private Image image; private Image image;
private Surface surface; private Surface surface;
private Rectangle cropRectangle; private Point offset;
public SurfaceCropMemento(Surface surface, Rectangle cropRectangle) { public SurfaceBackgroundChangeMemento(Surface surface, Point offset) {
this.surface = surface; this.surface = surface;
this.image = surface.Image; this.image = surface.Image;
this.cropRectangle = cropRectangle; this.offset = new Point(-offset.X, -offset.Y);
} }
public void Dispose() { public void Dispose() {
@ -58,8 +58,8 @@ namespace Greenshot.Memento {
} }
public IMemento Restore() { public IMemento Restore() {
SurfaceCropMemento oldState = new SurfaceCropMemento( surface, cropRectangle); SurfaceBackgroundChangeMemento oldState = new SurfaceBackgroundChangeMemento(surface, offset);
surface.UndoCrop(image, cropRectangle); surface.UndoBackgroundChange(image, offset);
surface.Invalidate(); surface.Invalidate();
return oldState; return oldState;
} }

View file

@ -306,11 +306,7 @@ namespace GreenshotPlugin.Core {
/// <returns>Changed bitmap</returns> /// <returns>Changed bitmap</returns>
public static Bitmap CreateTornEdge(Bitmap sourceBitmap) { public static Bitmap CreateTornEdge(Bitmap sourceBitmap) {
Bitmap returnImage = new Bitmap(sourceBitmap.Width, sourceBitmap.Height, PixelFormat.Format32bppArgb); Bitmap returnImage = new Bitmap(sourceBitmap.Width, sourceBitmap.Height, PixelFormat.Format32bppArgb);
try { returnImage.SetResolution(sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
returnImage.SetResolution(sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
} catch (Exception ex) {
LOG.Warn("An exception occured while setting the resolution.", ex);
}
using (GraphicsPath path = new GraphicsPath()) { using (GraphicsPath path = new GraphicsPath()) {
Random random = new Random(); Random random = new Random();
int regionWidth = 20; int regionWidth = 20;
@ -612,12 +608,10 @@ namespace GreenshotPlugin.Core {
/// <param name="targetPixelformat">What pixel format must the returning bitmap have</param> /// <param name="targetPixelformat">What pixel format must the returning bitmap have</param>
/// <param name="offset">How many pixels is the original image moved?</param> /// <param name="offset">How many pixels is the original image moved?</param>
/// <returns>Bitmap with the shadow, is bigger than the sourceBitmap!!</returns> /// <returns>Bitmap with the shadow, is bigger than the sourceBitmap!!</returns>
public static Bitmap CreateShadow(Bitmap sourceBitmap, float darkness, int shadowSize, PixelFormat targetPixelformat, out Point offset) { public static Bitmap CreateShadow(Bitmap sourceBitmap, float darkness, int shadowSize, Point offset, PixelFormat targetPixelformat) {
// "return" the shifted offset, so the caller can e.g. move elements
offset = new Point(shadowSize - 2, shadowSize - 2);
// Create a new "clean" image // Create a new "clean" image
Bitmap newImage = new Bitmap(sourceBitmap.Width + (shadowSize * 2), sourceBitmap.Height + (shadowSize * 2), targetPixelformat); Bitmap newImage = new Bitmap(sourceBitmap.Width + (shadowSize * 2), sourceBitmap.Height + (shadowSize * 2), targetPixelformat);
newImage.SetResolution(sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
using (Graphics graphics = Graphics.FromImage(newImage)) { using (Graphics graphics = Graphics.FromImage(newImage)) {
// Make sure the background color is what we want (transparent or white, depending on the pixel format) // Make sure the background color is what we want (transparent or white, depending on the pixel format)
@ -650,11 +644,92 @@ namespace GreenshotPlugin.Core {
// draw original with a TextureBrush so we have nice antialiasing! // draw original with a TextureBrush so we have nice antialiasing!
using (Brush textureBrush = new TextureBrush(sourceBitmap, WrapMode.Clamp)) { using (Brush textureBrush = new TextureBrush(sourceBitmap, WrapMode.Clamp)) {
// We need to do a translate-tranform otherwise the image is wrapped // We need to do a translate-tranform otherwise the image is wrapped
graphics.TranslateTransform(shadowSize - 2, shadowSize - 2); graphics.TranslateTransform(offset.X, offset.Y);
graphics.FillRectangle(textureBrush, 0, 0, sourceBitmap.Width, sourceBitmap.Height); graphics.FillRectangle(textureBrush, 0, 0, sourceBitmap.Width, sourceBitmap.Height);
} }
} }
return newImage; return newImage;
} }
/// <summary>
/// Create a new bitmap where the sourceBitmap has a Simple border around it
/// </summary>
/// <param name="sourceBitmap">Bitmap to make a border on</param>
/// <param name="borderSize">Size of the border</param>
/// <param name="borderColor">Color of the border</param>
/// <param name="targetPixelformat">What pixel format must the returning bitmap have</param>
/// <param name="offset">How many pixels is the original image moved?</param>
/// <returns>Bitmap with the shadow, is bigger than the sourceBitmap!!</returns>
public static Bitmap CreateBorder(Bitmap sourceBitmap, int borderSize, Color borderColor, PixelFormat targetPixelformat, out Point offset) {
// "return" the shifted offset, so the caller can e.g. move elements
offset = new Point(borderSize, borderSize);
// Create a new "clean" image
Bitmap newImage = new Bitmap(sourceBitmap.Width + (borderSize * 2), sourceBitmap.Height + (borderSize * 2), targetPixelformat);
newImage.SetResolution(sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
using (Graphics graphics = Graphics.FromImage(newImage)) {
// Make sure the background color is what we want (transparent or white, depending on the pixel format)
if (Image.IsAlphaPixelFormat(targetPixelformat)) {
graphics.Clear(Color.Transparent);
} else {
graphics.Clear(Color.White);
}
// Make sure we draw with the best quality!
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
using (GraphicsPath path = new GraphicsPath()) {
path.AddRectangle(new Rectangle(borderSize >> 1, borderSize >> 1, newImage.Width - (borderSize), newImage.Height - (borderSize)));
using (Pen pen = new Pen(borderColor, borderSize)) {
pen.LineJoin = LineJoin.Round;
pen.StartCap = LineCap.Round;
pen.EndCap = LineCap.Round;
graphics.DrawPath(pen, path);
}
}
// draw original with a TextureBrush so we have nice antialiasing!
using (Brush textureBrush = new TextureBrush(sourceBitmap, WrapMode.Clamp)) {
// We need to do a translate-tranform otherwise the image is wrapped
graphics.TranslateTransform(offset.X, offset.Y);
graphics.FillRectangle(textureBrush, 0, 0, sourceBitmap.Width, sourceBitmap.Height);
}
}
return newImage;
}
/// <summary>
/// Create a new bitmap where the sourceBitmap is in grayscale
/// </summary>
/// <param name="sourceBitmap">Original bitmap</param>
/// <returns>Bitmap with grayscale</returns>
public static Bitmap CreateGrayscale(Bitmap sourceBitmap) {
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height);
//get a graphics object from the new image
using (Graphics graphics = Graphics.FromImage(newBitmap)) {
// create the grayscale ColorMatrix
ColorMatrix colorMatrix = new ColorMatrix(
new float[][] {
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
//create some image attributes
ImageAttributes attributes = new ImageAttributes();
//set the color matrix attribute
attributes.SetColorMatrix(colorMatrix);
//draw the original image on the new image using the grayscale color matrix
graphics.DrawImage(sourceBitmap, new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), 0, 0, sourceBitmap.Width, sourceBitmap.Height, GraphicsUnit.Pixel, attributes);
}
return newBitmap;
}
} }
} }

View file

@ -715,9 +715,11 @@ namespace GreenshotPlugin.Core {
/// <summary> /// <summary>
/// Capture DWM Window /// Capture DWM Window
/// </summary> /// </summary>
/// <param name="capture"></param> /// <param name="capture">Capture to fill</param>
/// <returns></returns> /// <param name="windowCaptureMode">Wanted WindowCaptureMode</param>
public ICapture CaptureDWMWindow(ICapture capture, WindowCaptureMode windowCaptureMode, bool failIfNotFits) { /// <param name="autoMode">True if auto modus is used</param>
/// <returns>ICapture with the capture</returns>
public ICapture CaptureDWMWindow(ICapture capture, WindowCaptureMode windowCaptureMode, bool autoMode) {
IntPtr thumbnailHandle = IntPtr.Zero; IntPtr thumbnailHandle = IntPtr.Zero;
Form tempForm = null; Form tempForm = null;
bool tempFormShown = false; bool tempFormShown = false;
@ -789,7 +791,7 @@ namespace GreenshotPlugin.Core {
// and subtrackting the border from the size (2 times, as we move right/down for the capture without resizing) // and subtrackting the border from the size (2 times, as we move right/down for the capture without resizing)
captureRectangle.Width -= 2 * borderSize.Width; captureRectangle.Width -= 2 * borderSize.Width;
captureRectangle.Height -= 2 * borderSize.Height; captureRectangle.Height -= 2 * borderSize.Height;
} else if (failIfNotFits) { } else if (autoMode) {
// check if the capture fits // check if the capture fits
if (!capture.ScreenBounds.Contains(captureRectangle)) { if (!capture.ScreenBounds.Contains(captureRectangle)) {
// if GDI is allowed.. // if GDI is allowed..
@ -856,7 +858,14 @@ namespace GreenshotPlugin.Core {
// If no capture up till now, create a normal capture. // If no capture up till now, create a normal capture.
if (capturedBitmap == null) { if (capturedBitmap == null) {
// Remove transparency, this will break the capturing // Remove transparency, this will break the capturing
tempForm.BackColor = Color.FromArgb(255, conf.DWMBackgroundColor.R, conf.DWMBackgroundColor.G, conf.DWMBackgroundColor.B); if (!autoMode) {
tempForm.BackColor = Color.FromArgb(255, conf.DWMBackgroundColor.R, conf.DWMBackgroundColor.G, conf.DWMBackgroundColor.B);
} else {
Color colorizationColor = DWM.ColorizationColor;
// Modify by losing the transparency and increasing the intensity (as if the background color is white)
colorizationColor = Color.FromArgb(255, (colorizationColor.R + 255) >> 1, (colorizationColor.G + 255) >> 1, (colorizationColor.B + 255) >> 1);
tempForm.BackColor = colorizationColor;
}
// Make sure everything is visible // Make sure everything is visible
tempForm.Refresh(); tempForm.Refresh();
Application.DoEvents(); Application.DoEvents();
@ -865,6 +874,12 @@ namespace GreenshotPlugin.Core {
} }
if (capturedBitmap != null && redMask != null) { if (capturedBitmap != null && redMask != null) {
// Remove corners // Remove corners
if (!Image.IsAlphaPixelFormat(capturedBitmap.PixelFormat)) {
LOG.Debug("Changing pixelformat to Alpha for the RemoveCorners");
Bitmap tmpBitmap = capturedBitmap.Clone(new Rectangle(Point.Empty, capturedBitmap.Size), PixelFormat.Format32bppArgb);
capturedBitmap.Dispose();
capturedBitmap = tmpBitmap;
}
RemoveCorners(capturedBitmap, redMask, windowCaptureMode, conf.DWMBackgroundColor); RemoveCorners(capturedBitmap, redMask, windowCaptureMode, conf.DWMBackgroundColor);
} }
} }
@ -1208,6 +1223,11 @@ namespace GreenshotPlugin.Core {
LOG.DebugFormat("Not freezing ourselves, process was: {0}", proc.ProcessName); LOG.DebugFormat("Not freezing ourselves, process was: {0}", proc.ProcessName);
return; return;
} }
// TODO: Check Outlook, Office etc?
if (proc.ProcessName.ToLower().Contains("outlook")) {
LOG.DebugFormat("Not freezing outlook due to Destinations, process was: {0}", proc.ProcessName);
return;
}
LOG.DebugFormat("Freezing process: {0}", proc.ProcessName); LOG.DebugFormat("Freezing process: {0}", proc.ProcessName);
foreach (ProcessThread pT in proc.Threads) { foreach (ProcessThread pT in proc.Threads) {

View file

@ -31,7 +31,7 @@ namespace Greenshot.Plugin {
/// </summary> /// </summary>
//public enum HorizontalAlignment {LEFT, CENTER, RIGHT}; //public enum HorizontalAlignment {LEFT, CENTER, RIGHT};
public enum VerticalAlignment {TOP, CENTER, BOTTOM}; public enum VerticalAlignment {TOP, CENTER, BOTTOM};
public enum Effects { Shadow, TornEdge }; public enum Effects { Shadow, TornEdge, Border, Grayscale };
public enum SurfaceMessageTyp { public enum SurfaceMessageTyp {
FileSaved, FileSaved,

View file

@ -20,6 +20,8 @@
*/ */
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Drawing;
namespace GreenshotPlugin.UnmanagedHelpers { namespace GreenshotPlugin.UnmanagedHelpers {
@ -107,6 +109,9 @@ namespace GreenshotPlugin.UnmanagedHelpers {
[DllImport("dwmapi", SetLastError = true)] [DllImport("dwmapi", SetLastError = true)]
public static extern int DwmEnableBlurBehindWindow(IntPtr hwnd, ref DWM_BLURBEHIND blurBehind); public static extern int DwmEnableBlurBehindWindow(IntPtr hwnd, ref DWM_BLURBEHIND blurBehind);
// Key to ColorizationColor for DWM
private const string COLORIZATION_COLOR_KEY = @"SOFTWARE\Microsoft\Windows\DWM";
/// <summary> /// <summary>
/// Helper method for an easy DWM check /// Helper method for an easy DWM check
/// </summary> /// </summary>
@ -119,5 +124,19 @@ namespace GreenshotPlugin.UnmanagedHelpers {
} }
return false; return false;
} }
public static Color ColorizationColor {
get {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(COLORIZATION_COLOR_KEY, false)) {
if (key != null) {
object dwordValue = key.GetValue("ColorizationColor");
if (dwordValue != null) {
return Color.FromArgb((Int32)dwordValue);
}
}
}
return Color.White;
}
}
} }
} }