mirror of
https://github.com/greenshot/greenshot
synced 2025-08-20 13:33:27 -07:00
Added a few helper methods to resize bitmaps.
git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1761 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
parent
abad0a2166
commit
18e0d88b0c
1 changed files with 115 additions and 23 deletions
|
@ -325,8 +325,7 @@ namespace GreenshotPlugin.Core {
|
||||||
/// <param name="sourceBitmap">Bitmap to make torn edge off</param>
|
/// <param name="sourceBitmap">Bitmap to make torn edge off</param>
|
||||||
/// <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 = CreateEmptyLike(sourceBitmap);
|
||||||
returnImage.SetResolution(sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
|
|
||||||
using (GraphicsPath path = new GraphicsPath()) {
|
using (GraphicsPath path = new GraphicsPath()) {
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
int regionWidth = 20;
|
int regionWidth = 20;
|
||||||
|
@ -687,8 +686,7 @@ namespace GreenshotPlugin.Core {
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy back to image
|
// copy back to image
|
||||||
Bitmap newImage = new Bitmap(sourceBitmap.Width, sourceBitmap.Height, sourceBitmap.PixelFormat);
|
Bitmap newImage = CreateEmptyLike(sourceBitmap);
|
||||||
newImage.SetResolution(sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
|
|
||||||
var bits2 = newImage.LockBits(rct, ImageLockMode.ReadWrite, newImage.PixelFormat);
|
var bits2 = newImage.LockBits(rct, ImageLockMode.ReadWrite, newImage.PixelFormat);
|
||||||
Marshal.Copy(dest, 0, bits2.Scan0, dest.Length);
|
Marshal.Copy(dest, 0, bits2.Scan0, dest.Length);
|
||||||
newImage.UnlockBits(bits);
|
newImage.UnlockBits(bits);
|
||||||
|
@ -727,16 +725,9 @@ namespace GreenshotPlugin.Core {
|
||||||
/// <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(Image sourceBitmap, float darkness, int shadowSize, Point offset, PixelFormat targetPixelformat) {
|
public static Bitmap CreateShadow(Image sourceBitmap, float darkness, int shadowSize, Point offset, PixelFormat targetPixelformat) {
|
||||||
// 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 = CreateEmpty(sourceBitmap.Width + (shadowSize * 2), sourceBitmap.Height + (shadowSize * 2), targetPixelformat, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
|
||||||
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)
|
|
||||||
if (Image.IsAlphaPixelFormat(targetPixelformat)) {
|
|
||||||
graphics.Clear(Color.Transparent);
|
|
||||||
} else {
|
|
||||||
graphics.Clear(Color.White);
|
|
||||||
}
|
|
||||||
// Make sure we draw with the best quality!
|
// Make sure we draw with the best quality!
|
||||||
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
||||||
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||||
|
@ -815,15 +806,8 @@ namespace GreenshotPlugin.Core {
|
||||||
offset = new Point(borderSize, borderSize);
|
offset = new Point(borderSize, borderSize);
|
||||||
|
|
||||||
// Create a new "clean" image
|
// Create a new "clean" image
|
||||||
Bitmap newImage = new Bitmap(sourceBitmap.Width + (borderSize * 2), sourceBitmap.Height + (borderSize * 2), targetPixelformat);
|
Bitmap newImage = CreateEmpty(sourceBitmap.Width + (borderSize * 2), sourceBitmap.Height + (borderSize * 2), targetPixelformat, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
|
||||||
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)
|
|
||||||
if (Image.IsAlphaPixelFormat(targetPixelformat)) {
|
|
||||||
graphics.Clear(Color.Transparent);
|
|
||||||
} else {
|
|
||||||
graphics.Clear(Color.White);
|
|
||||||
}
|
|
||||||
// Make sure we draw with the best quality!
|
// Make sure we draw with the best quality!
|
||||||
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
graphics.SmoothingMode = SmoothingMode.HighQuality;
|
||||||
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
||||||
|
@ -855,9 +839,7 @@ namespace GreenshotPlugin.Core {
|
||||||
/// <returns>Bitmap with grayscale</returns>
|
/// <returns>Bitmap with grayscale</returns>
|
||||||
public static Bitmap CreateGrayscale(Bitmap sourceBitmap) {
|
public static Bitmap CreateGrayscale(Bitmap sourceBitmap) {
|
||||||
//create a blank bitmap the same size as original
|
//create a blank bitmap the same size as original
|
||||||
Bitmap newBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height);
|
Bitmap newBitmap = CreateEmptyLike(sourceBitmap);
|
||||||
// Make sure both images have the same resolution
|
|
||||||
newBitmap.SetResolution(sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
|
|
||||||
|
|
||||||
//get a graphics object from the new image
|
//get a graphics object from the new image
|
||||||
using (Graphics graphics = Graphics.FromImage(newBitmap)) {
|
using (Graphics graphics = Graphics.FromImage(newBitmap)) {
|
||||||
|
@ -1005,5 +987,115 @@ namespace GreenshotPlugin.Core {
|
||||||
returnBitmap.RotateFlip(rotateFlipType);
|
returnBitmap.RotateFlip(rotateFlipType);
|
||||||
return returnBitmap;
|
return returnBitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A generic way to create an empty image
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sourceBitmap">the source bitmap as the specifications for the new bitmap</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static Bitmap CreateEmptyLike(Bitmap sourceBitmap) {
|
||||||
|
return CreateEmpty(sourceBitmap.Width, sourceBitmap.Height, sourceBitmap.PixelFormat, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A generic way to create an empty image
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="width"></param>
|
||||||
|
/// <param name="height"></param>
|
||||||
|
/// <param name="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) {
|
||||||
|
// 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)) {
|
||||||
|
graphics.Clear(Color.Transparent);
|
||||||
|
} else {
|
||||||
|
graphics.Clear(Color.White);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newImage;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get a scaled version of the sourceBitmap
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sourceBitmap"></param>
|
||||||
|
/// <param name="percent">1-99 to make smaller, use 101 and more to make the picture bigger</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static Bitmap ScaleByPercent(Bitmap sourceBitmap, int percent) {
|
||||||
|
float nPercent = ((float)percent/100);
|
||||||
|
|
||||||
|
int sourceWidth = sourceBitmap.Width;
|
||||||
|
int sourceHeight = sourceBitmap.Height;
|
||||||
|
int destWidth = (int)(sourceWidth * nPercent);
|
||||||
|
int destHeight = (int)(sourceHeight * nPercent);
|
||||||
|
|
||||||
|
Bitmap scaledBitmap = CreateEmpty(destWidth, destHeight, sourceBitmap.PixelFormat, 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);
|
||||||
|
}
|
||||||
|
return scaledBitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Grow canvas with pixel to the left, right, top and bottom
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sourceBitmap"></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);
|
||||||
|
using (Graphics graphics = Graphics.FromImage(newBitmap)) {
|
||||||
|
graphics.DrawImageUnscaled(sourceBitmap, left, top);
|
||||||
|
}
|
||||||
|
return newBitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 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="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) {
|
||||||
|
int destX = 0;
|
||||||
|
int destY = 0;
|
||||||
|
|
||||||
|
float nPercent = 0;
|
||||||
|
float nPercentW = 0;
|
||||||
|
float nPercentH = 0;
|
||||||
|
|
||||||
|
nPercentW = ((float)newWidth / (float)sourceBitmap.Width);
|
||||||
|
nPercentH = ((float)newHeight / (float)sourceBitmap.Height);
|
||||||
|
if (nPercentH < nPercentW) {
|
||||||
|
nPercent = nPercentH;
|
||||||
|
destX = System.Convert.ToInt16((newWidth - (sourceBitmap.Width * nPercent)) / 2);
|
||||||
|
} else {
|
||||||
|
nPercent = nPercentW;
|
||||||
|
destY = System.Convert.ToInt16((newHeight - (sourceBitmap.Height * nPercent)) / 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
int destWidth = (int)(sourceBitmap.Width * nPercent);
|
||||||
|
int destHeight = (int)(sourceBitmap.Height * nPercent);
|
||||||
|
|
||||||
|
Bitmap newBitmap = CreateEmpty(newWidth, newHeight, sourceBitmap.PixelFormat, 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);
|
||||||
|
|
||||||
|
}
|
||||||
|
return newBitmap;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue