/// <summary> /// Resize an image /// </summary> /// <param name="source"></param> /// <param name="parameters"></param> /// <returns></returns> public virtual Bitmap Resize(object source, ResizeSettings parameters) { using (var job = new ImageJob(source)) { var layout = _imageLayoutBuilder.BuildLayout(job.Bitmap.Size, parameters); var bmp = new Bitmap((int)layout.CanvasSize.Width, (int)layout.CanvasSize.Height, PixelFormat.Format24bppRgb); var gfx = Graphics.FromImage(bmp); gfx.InterpolationMode = InterpolationMode.HighQualityBicubic; gfx.SmoothingMode = SmoothingMode.HighQuality; gfx.PixelOffsetMode = PixelOffsetMode.HighQuality; gfx.CompositingQuality = CompositingQuality.HighQuality; gfx.CompositingMode = CompositingMode.SourceOver; // white background gfx.FillRectangle(new SolidBrush(Color.White), 0, 0, layout.CanvasSize.Width, layout.CanvasSize.Height); // draw the image gfx.DrawImage(job.Bitmap, layout.Image, new RectangleF(0, 0, job.Bitmap.Width, job.Bitmap.Height), GraphicsUnit.Pixel); // commit gfx.Flush(FlushIntention.Flush); gfx.Dispose(); return(bmp); } }
protected virtual void AssertLayout(string sourceSize, string resizeSettings, Action <RectangleF> assertImage, Action <SizeF> assertCanvas) { var sourceSizeSettings = new ResizeSettings(sourceSize); var result = _imageLayoutBuilder.BuildLayout(new Size(sourceSizeSettings.Width, sourceSizeSettings.Height), new ResizeSettings(resizeSettings)); if (assertCanvas != null) { assertCanvas(result.CanvasSize); } if (assertImage != null) { assertImage(result.Image); } var maxWidth = (int)(Math.Max(result.Image.Width, result.CanvasSize.Width)); var maxHeight = (int)(Math.Max(result.Image.Height, result.CanvasSize.Height)); var padding = (int)Math.Max(Math.Abs(result.Image.Y), Math.Abs(result.Image.X)) + 20; if ((maxWidth + padding) < 400) { padding = (400 - maxWidth) / 2; } // create a bitmap for visualizing var bitmapSize = new RectangleF(0, 0, maxWidth + padding * 2, maxHeight + (padding * 2)); using (var bmp = new Bitmap((int)bitmapSize.Width, (int)bitmapSize.Height)) { using (var gfx = Graphics.FromImage(bmp)) { // set the background gfx.FillRectangle(new SolidBrush(Color.White), 0, 0, bmp.Width, bmp.Height); // output the results gfx.DrawString("Source: " + sourceSize, new Font("Thaoma", 8), Brushes.Black, new RectangleF(0, 0, bmp.Width, bmp.Height), new StringFormat { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Near }); gfx.DrawString("Destination: " + resizeSettings, new Font("Thaoma", 8), Brushes.Black, new RectangleF(0, 0, bmp.Width, bmp.Height), new StringFormat { Alignment = StringAlignment.Far, LineAlignment = StringAlignment.Near }); gfx.DrawString("Canvas: " + result.CanvasSize.Width + "x" + result.CanvasSize.Height, new Font("Thaoma", 8), Brushes.Green, new RectangleF(0, 0, bmp.Width, bmp.Height), new StringFormat { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Far }); gfx.DrawString("Image: " + result.Image.Width + "x" + result.Image.Height, new Font("Thaoma", 8), Brushes.Red, new RectangleF(0, 0, bmp.Width, bmp.Height), new StringFormat { Alignment = StringAlignment.Far, LineAlignment = StringAlignment.Far }); //PolygonMath.AlignWith() var canvas = new RectangleF(padding, padding, result.CanvasSize.Width, result.CanvasSize.Height); var image = new RectangleF(padding + result.Image.X, padding + result.Image.Y, result.Image.Width, result.Image.Height); var points = new List <PointF>(); points.AddRange(PolygonMath.ToPoly(canvas)); points.AddRange(PolygonMath.ToPoly(image)); points = PolygonMath.AlignWith(points.ToArray(), PolygonMath.ToPoly(bitmapSize), ContentAlignment.MiddleCenter).ToList(); canvas = PolygonMath.GetBoundingBox(points.Take(4).ToArray()); image = PolygonMath.GetBoundingBox(points.Skip(4).Take(4).ToArray()); gfx.FillRectangle(new SolidBrush(Color.Green), canvas); gfx.DrawRectangle(new Pen(Color.Red, 2), image.X, image.Y, image.Width, image.Height); } var fileName = sourceSize + "--" + resizeSettings + ".bmp"; var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName); if (File.Exists(filePath)) { File.Delete(filePath); } bmp.Save(filePath, ImageFormat.Bmp); Trace.WriteLine("Source: " + sourceSize); Trace.WriteLine("Destination: " + resizeSettings); Trace.WriteLine(" Result: " + filePath); } }