/// <summary> /// Resize canvas with pixel to the left, right, top and bottom /// </summary> /// <param name="sourceImage">Image</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">int</param> /// <param name="right">int</param> /// <param name="top">int</param> /// <param name="bottom">int</param> /// <param name="matrix">Matrix</param> /// <returns>a new bitmap with the source copied on it</returns> public static Bitmap ResizeCanvas(this Image sourceImage, Color backgroundColor, int left, int right, int top, int bottom, Matrix matrix) { matrix.Translate(left, top, MatrixOrder.Append); var newBitmap = BitmapFactory.CreateEmpty(sourceImage.Width + left + right, sourceImage.Height + top + bottom, sourceImage.PixelFormat, backgroundColor, sourceImage.HorizontalResolution, sourceImage.VerticalResolution); using (var graphics = Graphics.FromImage(newBitmap)) { graphics.DrawImageUnscaled(sourceImage, left, top); } return(newBitmap); }
/// <summary> /// Create a new bitmap where the sourceBitmap has a shadow /// </summary> /// <param name="sourceBitmap">Bitmap to make a shadow on</param> /// <param name="darkness">How dark is the shadow</param> /// <param name="shadowSize">Size of the shadow</param> /// <param name="targetPixelformat">What pixel format must the returning bitmap have</param> /// <param name="shadowOffset"></param> /// <param name="matrix"> /// The transform matrix which describes how the elements need to be transformed to stay at the same /// location /// </param> /// <returns>Bitmap with the shadow, is bigger than the sourceBitmap!!</returns> public static Bitmap CreateShadow(this Image sourceBitmap, float darkness, int shadowSize, NativePoint shadowOffset, Matrix matrix, PixelFormat targetPixelformat) { var offset = shadowOffset.Offset(shadowSize - 1, shadowSize - 1); matrix.Translate(offset.X, offset.Y, MatrixOrder.Append); // Create a new "clean" image var returnImage = BitmapFactory.CreateEmpty(sourceBitmap.Width + shadowSize * 2, sourceBitmap.Height + shadowSize * 2, targetPixelformat, Color.Empty, sourceBitmap.HorizontalResolution, sourceBitmap.VerticalResolution); // Make sure the shadow is odd, there is no reason for an even blur! if ((shadowSize & 1) == 0) { shadowSize++; } // Create "mask" for the shadow var maskMatrix = new ColorMatrix { Matrix00 = 0, Matrix11 = 0, Matrix22 = 0, Matrix33 = darkness }; var shadowRectangle = new NativeRect(new NativePoint(shadowSize, shadowSize), sourceBitmap.Size); ApplyColorMatrix((Bitmap)sourceBitmap, NativeRect.Empty, returnImage, shadowRectangle, maskMatrix); // blur "shadow", apply to whole new image // try normal software blur returnImage.ApplyBoxBlur(shadowSize); // Draw the original image over the shadow using (var graphics = Graphics.FromImage(returnImage)) { // Make sure we draw with the best quality! graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; // draw original with a TextureBrush so we have nice antialiasing! using (Brush textureBrush = new TextureBrush(sourceBitmap, WrapMode.Clamp)) { // We need to do a translate-transform otherwise the image is wrapped graphics.TranslateTransform(offset.X, offset.Y); graphics.FillRectangle(textureBrush, 0, 0, sourceBitmap.Width, sourceBitmap.Height); } } return(returnImage); }
/// <summary> /// Get a scaled version of the sourceBitmap /// </summary> /// <param name="sourceImage">Image</param> /// <param name="percent">1-99 to make smaller, use 101 and more to make the picture bigger</param> /// <returns>Bitmap</returns> public static Bitmap ScaleByPercent(this Image sourceImage, int percent) { var nPercent = (float)percent / 100; var sourceWidth = sourceImage.Width; var sourceHeight = sourceImage.Height; var destWidth = (int)(sourceWidth * nPercent); var destHeight = (int)(sourceHeight * nPercent); var scaledBitmap = BitmapFactory.CreateEmpty(destWidth, destHeight, sourceImage.PixelFormat, Color.Empty, sourceImage.HorizontalResolution, sourceImage.VerticalResolution); using (var graphics = Graphics.FromImage(scaledBitmap)) { graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.DrawImage(sourceImage, new NativeRect(0, 0, destWidth, destHeight), new NativeRect(0, 0, sourceWidth, sourceHeight), GraphicsUnit.Pixel); } return(scaledBitmap); }
/// <summary> /// Scale the bitmap, keeping aspect ratio, but the canvas will always have the specified size. /// </summary> /// <param name="sourceImage">Image to scale</param> /// <param name="maintainAspectRatio">true to maintain the aspect ratio</param> /// <param name="canvasUseNewSize">Makes the image maintain aspect ratio, but the canvas get's the specified size</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> /// <param name="matrix">Matrix</param> /// <param name="interpolationMode">InterpolationMode</param> /// <returns>a new bitmap with the specified size, the source-Image scaled to fit with aspect ratio locked</returns> public static Bitmap Resize(this Image sourceImage, bool maintainAspectRatio, bool canvasUseNewSize, Color backgroundColor, int newWidth, int newHeight, Matrix matrix, InterpolationMode interpolationMode = InterpolationMode.HighQualityBicubic) { var destX = 0; var destY = 0; var nPercentW = newWidth / (float)sourceImage.Width; var nPercentH = newHeight / (float)sourceImage.Height; if (maintainAspectRatio) { if ((int)nPercentW == 1 || (int)nPercentH != 0 && nPercentH < nPercentW) { nPercentW = nPercentH; if (canvasUseNewSize) { destX = Math.Max(0, Convert.ToInt32((newWidth - sourceImage.Width * nPercentW) / 2)); } } else { nPercentH = nPercentW; if (canvasUseNewSize) { destY = Math.Max(0, Convert.ToInt32((newHeight - sourceImage.Height * nPercentH) / 2)); } } } var destWidth = (int)(sourceImage.Width * nPercentW); var destHeight = (int)(sourceImage.Height * nPercentH); if (newWidth == 0) { newWidth = destWidth; } if (newHeight == 0) { newHeight = destHeight; } Bitmap newBitmap; if (maintainAspectRatio && canvasUseNewSize) { newBitmap = BitmapFactory.CreateEmpty(newWidth, newHeight, sourceImage.PixelFormat, backgroundColor, sourceImage.HorizontalResolution, sourceImage.VerticalResolution); matrix?.Scale((float)newWidth / sourceImage.Width, (float)newHeight / sourceImage.Height, MatrixOrder.Append); } else { newBitmap = BitmapFactory.CreateEmpty(destWidth, destHeight, sourceImage.PixelFormat, backgroundColor, sourceImage.HorizontalResolution, sourceImage.VerticalResolution); matrix?.Scale((float)destWidth / sourceImage.Width, (float)destHeight / sourceImage.Height, MatrixOrder.Append); } using (var graphics = Graphics.FromImage(newBitmap)) { graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.InterpolationMode = interpolationMode; using (var wrapMode = new ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); graphics.DrawImage(sourceImage, new NativeRect(destX, destY, destWidth, destHeight), 0, 0, sourceImage.Width, sourceImage.Height, GraphicsUnit.Pixel, wrapMode); } } return(newBitmap); }