More fixes for the resize code.

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1763 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-04-08 19:11:55 +00:00
commit afc9c37ce5
2 changed files with 50 additions and 12 deletions

View file

@ -564,6 +564,26 @@ namespace Greenshot.Drawing {
Invalidate(); Invalidate();
SurfaceSizeChanged(this); SurfaceSizeChanged(this);
} }
/// <summary>
/// Resize bitmap
/// </summary>
/// <param name="backgroundColor"></param>
/// <param name="left"></param>
/// <param name="right"></param>
/// <param name="top"></param>
/// <param name="bottom"></param>
public void ResizeBitmap(bool lockAspectRatio, bool canvasUsedNewSize, Color backgroundColor, int newWidth, int newHeight) {
Point offset;
Bitmap newImage = ImageHelper.ResizeBitmap((Bitmap)Image, lockAspectRatio, canvasUsedNewSize, backgroundColor, newWidth, newHeight, out offset);
// Make sure the elements move according to the offset the effect made the bitmap move
elements.MoveBy(offset.X, offset.Y);
// Make undoable
MakeUndoable(new SurfaceBackgroundChangeMemento(this, offset), false);
SetImage(newImage, false);
Invalidate();
SurfaceSizeChanged(this);
}
/// <summary> /// <summary>
/// Apply a bitmap effect to the surface /// Apply a bitmap effect to the surface

View file

@ -1070,36 +1070,54 @@ namespace GreenshotPlugin.Core {
/// Scale the bitmap, keeping aspect ratio, but the canvas will always have the specified size. /// Scale the bitmap, keeping aspect ratio, but the canvas will always have the specified size.
/// </summary> /// </summary>
/// <param name="sourceBitmap">Bitmap to scale</param> /// <param name="sourceBitmap">Bitmap to scale</param>
/// <param name="lockAspectRatio">true to lock aspect ratio</param>
/// <param name="backgroundColor">The color to fill with, or Color.Empty to take the default depending on the pixel 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="newWidth">new width</param> /// <param name="newWidth">new width</param>
/// <param name="newHeight">new height</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> /// <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, Color backgroundColor, int newWidth, int newHeight) { public static Bitmap ResizeBitmap(Bitmap sourceBitmap, bool lockAspectRatio, bool canvasUseNewSize, Color backgroundColor, int newWidth, int newHeight, out Point offset) {
int destX = 0; int destX = 0;
int destY = 0; int destY = 0;
float nPercent = 0;
float nPercentW = 0; float nPercentW = 0;
float nPercentH = 0; float nPercentH = 0;
nPercentW = ((float)newWidth / (float)sourceBitmap.Width); nPercentW = ((float)newWidth / (float)sourceBitmap.Width);
nPercentH = ((float)newHeight / (float)sourceBitmap.Height); nPercentH = ((float)newHeight / (float)sourceBitmap.Height);
if (nPercentH < nPercentW) { if (lockAspectRatio) {
nPercent = nPercentH; if (nPercentH != 0 && nPercentH < nPercentW) {
destX = System.Convert.ToInt16((newWidth - (sourceBitmap.Width * nPercent)) / 2); nPercentW = nPercentH;
} else { if (canvasUseNewSize) {
nPercent = nPercentW; destX = Math.Max(0, System.Convert.ToInt32((newWidth - (sourceBitmap.Width * nPercentW)) / 2));
destY = System.Convert.ToInt16((newHeight - (sourceBitmap.Height * nPercent)) / 2); }
} else {
nPercentH = nPercentW;
if (canvasUseNewSize) {
destY = Math.Max(0, System.Convert.ToInt32((newHeight - (sourceBitmap.Height * nPercentH)) / 2));
}
}
} }
int destWidth = (int)(sourceBitmap.Width * nPercent); offset = new Point(destX, destY);
int destHeight = (int)(sourceBitmap.Height * nPercent);
int destWidth = (int)(sourceBitmap.Width * nPercentW);
int destHeight = (int)(sourceBitmap.Height * nPercentH);
if (newWidth == 0) {
newWidth = destWidth;
}
if (newHeight == 0) {
newHeight = destHeight;
}
Bitmap newBitmap = null;
if (lockAspectRatio && canvasUseNewSize) {
newBitmap = CreateEmpty(newWidth, newHeight, sourceBitmap.PixelFormat, backgroundColor, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
} else {
newBitmap = CreateEmpty(destWidth, destHeight, sourceBitmap.PixelFormat, backgroundColor, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
}
Bitmap newBitmap = CreateEmpty(newWidth, newHeight, sourceBitmap.PixelFormat, backgroundColor, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution);
using (Graphics graphics = Graphics.FromImage(newBitmap)) { using (Graphics graphics = Graphics.FromImage(newBitmap)) {
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.DrawImage(sourceBitmap, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), GraphicsUnit.Pixel); graphics.DrawImage(sourceBitmap, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(0, 0, sourceBitmap.Width, sourceBitmap.Height), GraphicsUnit.Pixel);
} }
return newBitmap; return newBitmap;
} }