From 72d3de218b76536682b236bfd4087ff7742e7d96 Mon Sep 17 00:00:00 2001 From: RKrom Date: Sun, 8 Apr 2012 18:35:27 +0000 Subject: [PATCH] Added a background color for some of the ImageHelper methods, this makes it more flexible. git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1762 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- Greenshot/Drawing/Surface.cs | 23 +++++++++++++++++++ GreenshotPlugin/Core/ImageHelper.cs | 34 +++++++++++++++++------------ 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/Greenshot/Drawing/Surface.cs b/Greenshot/Drawing/Surface.cs index ba7b979c3..8b23380cb 100644 --- a/Greenshot/Drawing/Surface.cs +++ b/Greenshot/Drawing/Surface.cs @@ -545,7 +545,30 @@ namespace Greenshot.Drawing { } return false; } + + /// + /// "Grow" the canvas with the specified pixels on the left, right, top and bottom. Using the backgroundColor. + /// + /// + /// + /// + /// + /// + public void GrowCanvas(Color backgroundColor, int left, int right, int top, int bottom) { + Bitmap newImage = ImageHelper.GrowCanvas((Bitmap)Image, backgroundColor, left, right, top, bottom); + // Make sure the elements move according to the offset the effect made the bitmap move + elements.MoveBy(left, top); + // Make undoable + MakeUndoable(new SurfaceBackgroundChangeMemento(this, new Point(left, top)), false); + SetImage(newImage, false); + Invalidate(); + SurfaceSizeChanged(this); + } + /// + /// Apply a bitmap effect to the surface + /// + /// public void ApplyBitmapEffect(Effects effect) { BackgroundForm backgroundForm = new BackgroundForm("Effect", "Please wait"); backgroundForm.Show(); diff --git a/GreenshotPlugin/Core/ImageHelper.cs b/GreenshotPlugin/Core/ImageHelper.cs index c018c9628..07045a2ca 100644 --- a/GreenshotPlugin/Core/ImageHelper.cs +++ b/GreenshotPlugin/Core/ImageHelper.cs @@ -325,7 +325,7 @@ namespace GreenshotPlugin.Core { /// Bitmap to make torn edge off /// Changed bitmap public static Bitmap CreateTornEdge(Bitmap sourceBitmap) { - Bitmap returnImage = CreateEmptyLike(sourceBitmap); + Bitmap returnImage = CreateEmptyLike(sourceBitmap, Color.Empty); using (GraphicsPath path = new GraphicsPath()) { Random random = new Random(); int regionWidth = 20; @@ -686,7 +686,7 @@ namespace GreenshotPlugin.Core { } // copy back to image - Bitmap newImage = CreateEmptyLike(sourceBitmap); + Bitmap newImage = CreateEmptyLike(sourceBitmap, Color.Empty); var bits2 = newImage.LockBits(rct, ImageLockMode.ReadWrite, newImage.PixelFormat); Marshal.Copy(dest, 0, bits2.Scan0, dest.Length); newImage.UnlockBits(bits); @@ -725,7 +725,7 @@ namespace GreenshotPlugin.Core { /// Bitmap with the shadow, is bigger than the sourceBitmap!! public static Bitmap CreateShadow(Image sourceBitmap, float darkness, int shadowSize, Point offset, PixelFormat targetPixelformat) { // Create a new "clean" image - Bitmap newImage = CreateEmpty(sourceBitmap.Width + (shadowSize * 2), sourceBitmap.Height + (shadowSize * 2), targetPixelformat, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); + Bitmap newImage = CreateEmpty(sourceBitmap.Width + (shadowSize * 2), sourceBitmap.Height + (shadowSize * 2), targetPixelformat, Color.Empty, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); using (Graphics graphics = Graphics.FromImage(newImage)) { // Make sure we draw with the best quality! @@ -806,7 +806,7 @@ namespace GreenshotPlugin.Core { offset = new Point(borderSize, borderSize); // Create a new "clean" image - Bitmap newImage = CreateEmpty(sourceBitmap.Width + (borderSize * 2), sourceBitmap.Height + (borderSize * 2), targetPixelformat, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); + Bitmap newImage = CreateEmpty(sourceBitmap.Width + (borderSize * 2), sourceBitmap.Height + (borderSize * 2), targetPixelformat, Color.Empty, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); using (Graphics graphics = Graphics.FromImage(newImage)) { // Make sure we draw with the best quality! graphics.SmoothingMode = SmoothingMode.HighQuality; @@ -839,7 +839,7 @@ namespace GreenshotPlugin.Core { /// Bitmap with grayscale public static Bitmap CreateGrayscale(Bitmap sourceBitmap) { //create a blank bitmap the same size as original - Bitmap newBitmap = CreateEmptyLike(sourceBitmap); + Bitmap newBitmap = CreateEmptyLike(sourceBitmap, Color.Empty); //get a graphics object from the new image using (Graphics graphics = Graphics.FromImage(newBitmap)) { @@ -992,9 +992,10 @@ namespace GreenshotPlugin.Core { /// A generic way to create an empty image /// /// the source bitmap as the specifications for the new bitmap + /// The color to fill with, or Color.Empty to take the default depending on the pixel format /// - public static Bitmap CreateEmptyLike(Bitmap sourceBitmap) { - return CreateEmpty(sourceBitmap.Width, sourceBitmap.Height, sourceBitmap.PixelFormat, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); + public static Bitmap CreateEmptyLike(Bitmap sourceBitmap, Color backgroundColor) { + return CreateEmpty(sourceBitmap.Width, sourceBitmap.Height, sourceBitmap.PixelFormat, backgroundColor, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); } /// @@ -1003,17 +1004,20 @@ namespace GreenshotPlugin.Core { /// /// /// + /// The color to fill with, or Color.Empty to take the default depending on the pixel format /// /// /// - public static Bitmap CreateEmpty(int width, int height, PixelFormat format, float horizontalResolution, float verticalResolution) { + public static Bitmap CreateEmpty(int width, int height, PixelFormat format, Color backgroundColor, float horizontalResolution, float verticalResolution) { // Create a new "clean" image Bitmap newImage = new Bitmap(width, height, format); newImage.SetResolution(horizontalResolution, 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(format)) { + if (!Color.Empty.Equals(backgroundColor)) { + graphics.Clear(backgroundColor); + } else if (Image.IsAlphaPixelFormat(format)) { graphics.Clear(Color.Transparent); } else { graphics.Clear(Color.White); @@ -1036,7 +1040,7 @@ namespace GreenshotPlugin.Core { int destWidth = (int)(sourceWidth * nPercent); int destHeight = (int)(sourceHeight * nPercent); - Bitmap scaledBitmap = CreateEmpty(destWidth, destHeight, sourceBitmap.PixelFormat, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); + Bitmap scaledBitmap = CreateEmpty(destWidth, destHeight, sourceBitmap.PixelFormat, Color.Empty, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); using (Graphics graphics = Graphics.FromImage(scaledBitmap)) { graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.DrawImage(sourceBitmap, new Rectangle(0, 0, destWidth, destHeight), new Rectangle(0, 0, sourceWidth, sourceHeight), GraphicsUnit.Pixel); @@ -1048,13 +1052,14 @@ namespace GreenshotPlugin.Core { /// Grow canvas with pixel to the left, right, top and bottom /// /// + /// The color to fill with, or Color.Empty to take the default depending on the pixel format /// /// /// /// /// a new bitmap with the source copied on it - public static Bitmap GrowCanvas(Bitmap sourceBitmap, int left, int right, int top, int bottom) { - Bitmap newBitmap = CreateEmpty(sourceBitmap.Width + left + right, sourceBitmap.Height + top + bottom, sourceBitmap.PixelFormat, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); + public static Bitmap GrowCanvas(Bitmap sourceBitmap, Color backgroundColor, int left, int right, int top, int bottom) { + Bitmap newBitmap = CreateEmpty(sourceBitmap.Width + left + right, sourceBitmap.Height + top + bottom, sourceBitmap.PixelFormat, backgroundColor, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); using (Graphics graphics = Graphics.FromImage(newBitmap)) { graphics.DrawImageUnscaled(sourceBitmap, left, top); } @@ -1065,10 +1070,11 @@ namespace GreenshotPlugin.Core { /// Scale the bitmap, keeping aspect ratio, but the canvas will always have the specified size. /// /// Bitmap to scale + /// The color to fill with, or Color.Empty to take the default depending on the pixel format /// new width /// new height /// a new bitmap with the specified size, the source-bitmap scaled to fit with aspect ratio locked - public static Bitmap AspectratioLockedBitmapResize(Bitmap sourceBitmap, int newWidth, int newHeight) { + public static Bitmap AspectratioLockedBitmapResize(Bitmap sourceBitmap, Color backgroundColor, int newWidth, int newHeight) { int destX = 0; int destY = 0; @@ -1089,7 +1095,7 @@ namespace GreenshotPlugin.Core { int destWidth = (int)(sourceBitmap.Width * nPercent); int destHeight = (int)(sourceBitmap.Height * nPercent); - Bitmap newBitmap = CreateEmpty(newWidth, newHeight, sourceBitmap.PixelFormat, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); + Bitmap newBitmap = CreateEmpty(newWidth, newHeight, sourceBitmap.PixelFormat, backgroundColor, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); using (Graphics graphics = Graphics.FromImage(newBitmap)) { graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.DrawImage(sourceBitmap, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), GraphicsUnit.Pixel);