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
This commit is contained in:
RKrom 2012-04-08 18:35:27 +00:00
parent 18e0d88b0c
commit 72d3de218b
2 changed files with 43 additions and 14 deletions

View file

@ -325,7 +325,7 @@ namespace GreenshotPlugin.Core {
/// <param name="sourceBitmap">Bitmap to make torn edge off</param>
/// <returns>Changed bitmap</returns>
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 {
/// <returns>Bitmap with the shadow, is bigger than the sourceBitmap!!</returns>
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 {
/// <returns>Bitmap with grayscale</returns>
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
/// </summary>
/// <param name="sourceBitmap">the source bitmap as the specifications for the new bitmap</param>
/// <param name="backgroundColor">The color to fill with, or Color.Empty to take the default depending on the pixel format</param>
/// <returns></returns>
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);
}
/// <summary>
@ -1003,17 +1004,20 @@ namespace GreenshotPlugin.Core {
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="format"></param>
/// <param name="backgroundColor">The color to fill with, or Color.Empty to take the default depending on the pixel format</param>
/// <param name="horizontalResolution"></param>
/// <param name="verticalResolution"></param>
/// <returns></returns>
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
/// </summary>
/// <param name="sourceBitmap"></param>
/// <param name="backgroundColor">The color to fill with, or Color.Empty to take the default depending on the pixel format</param>
/// <param name="left"></param>
/// <param name="right"></param>
/// <param name="top"></param>
/// <param name="bottom"></param>
/// <returns>a new bitmap with the source copied on it</returns>
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.
/// </summary>
/// <param name="sourceBitmap">Bitmap to scale</param>
/// <param name="backgroundColor">The color to fill with, or Color.Empty to take the default depending on the pixel format</param>
/// <param name="newWidth">new width</param>
/// <param name="newHeight">new height</param>
/// <returns>a new bitmap with the specified size, the source-bitmap scaled to fit with aspect ratio locked</returns>
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);