public void TestBitmapResize() { Color green = Color.FromArgb(0, 200, 0); Color red = Color.FromArgb(200, 0, 0); using (var bitmap = new Bitmap(20, 20)) { using (var graphics = Graphics.FromImage(bitmap)) using (var gBrush = new SolidBrush(green)) using (var rBrush = new SolidBrush(red)) { graphics.FillRectangle(gBrush, 0, 0, 10, 20); graphics.FillRectangle(rBrush, 10, 0, 10, 20); } using (var sourceRepresentation = new BitmapRepresentation(bitmap)) using (var sourceBitmapEx = sourceRepresentation.CreateBitmap()) using (var destinationBitmapEx = new BitmapEx(10, 10)) { BitmapHelpers.ResizeBitmap(sourceBitmapEx, destinationBitmapEx); var bmp = destinationBitmapEx.GetInternal(); { var green2 = bmp.GetPixel(3, 5); var red2 = bmp.GetPixel(7, 5); Assert.AreEqual(true, CompareColors(green, green2, 5)); Assert.AreEqual(true, CompareColors(red, red2, 5)); } } } }
public static IEnumerable <LayoutDrawElement> ExtractLayoutDrawElements(BitmapEx source, DeviceSize deviceSize, byte left, byte top, LayoutContext layoutContext, TransitionInfo transitionInfo = default(TransitionInfo)) { var itemWidth = source.Width / deviceSize.Width; var itemHeight = source.Height / deviceSize.Height; for (byte x = 0; x < deviceSize.Width; x++) { for (byte y = 0; y < deviceSize.Height; y++) { var part = layoutContext.CreateBitmap(); using (Graphics grD = part.CreateGraphics()) { grD.DrawImage(source.GetInternal(), new Rectangle(0, 0, itemWidth, itemHeight), new Rectangle(x * itemWidth, y * itemHeight, itemWidth, itemHeight), GraphicsUnit.Pixel); } yield return(new LayoutDrawElement(new Location(left + x, top + y), part, transitionInfo)); } } }
internal void SetImage(Location location, BitmapEx bitmap) { Action action = () => { var picBox = Controls.OfType <PictureBox>().FirstOrDefault(p => (Location)p.Tag == location); if (picBox != null) { picBox.Image?.Dispose(); picBox.Image = (Image)bitmap.GetInternal().Clone(); } }; if (InvokeRequired) { Invoke(action); } else { action(); } }
public static void ResizeBitmap(BitmapEx source, BitmapEx destination, Rectangle destRectangle, ColorMatrix colorMatrix = null) { using (var graphics = destination.CreateGraphics()) { graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; using (var wrapMode = new ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); if (colorMatrix != null) { wrapMode.SetColorMatrix(colorMatrix); } graphics.DrawImage(source.GetInternal(), destRectangle, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, wrapMode); } } }