예제 #1
0
        public override BitmapBase Draw(Tank tank)
        {
            var rnd = new Random(tank.TankId.GetHashCode());
            int rectWidth = _RectWidth, rectHeight = _RectHeight;
            int x = _X, y = _Y;
            int layerWidth = _LayerWidth, layerHeight = _LayerHeight;

            if (SmallWithBorders && rnd.NextDouble() < 0.33)
            {
                rectWidth   = rnd.Next(1, 70);
                rectHeight  = rnd.Next(1, 20);
                x           = rnd.Next(0, ParentStyle.IconWidth - rectWidth);
                y           = rnd.Next(0, ParentStyle.IconHeight - rectHeight);
                layerWidth  = x + rectWidth + rnd.Next(0, 10);
                layerHeight = y + rectHeight + rnd.Next(0, 10);
            }
            else if (WithoutBorders && rnd.NextDouble() < 0.5)
            {
                rectWidth  = layerWidth = rnd.Next(1, 70);
                rectHeight = layerHeight = rnd.Next(1, 20);
                x          = y = 0;
            }
            else if (LargeWithBorders)
            {
                rectWidth   = rnd.Next(70, 200);
                rectHeight  = rnd.Next(20, 70);
                x           = rnd.Next(0, 10);
                y           = rnd.Next(0, 10);
                layerWidth  = x + rectWidth + rnd.Next(0, 10);
                layerHeight = y + rectHeight + rnd.Next(0, 10);
            }

            var result = new BitmapGdi(layerWidth, layerHeight);

            using (var brush = new SolidBrush(Color))
                using (var g = Graphics.FromImage(result.Bitmap))
                    g.FillRectangle(brush, x, y, rectWidth, rectHeight);
            return(result);
        }
예제 #2
0
        public override BitmapBase Draw(Tank tank)
        {
            var rnd = new Random(tank.TankId.GetHashCode());
            int rectWidth = _RectWidth, rectHeight = _RectHeight;
            int x = _X, y = _Y;
            int layerWidth = _LayerWidth, layerHeight = _LayerHeight;
            if (SmallWithBorders && rnd.NextDouble() < 0.33)
            {
                rectWidth = rnd.Next(1, 70);
                rectHeight = rnd.Next(1, 20);
                x = rnd.Next(0, ParentStyle.IconWidth - rectWidth);
                y = rnd.Next(0, ParentStyle.IconHeight - rectHeight);
                layerWidth = x + rectWidth + rnd.Next(0, 10);
                layerHeight = y + rectHeight + rnd.Next(0, 10);
            }
            else if (WithoutBorders && rnd.NextDouble() < 0.5)
            {
                rectWidth = layerWidth = rnd.Next(1, 70);
                rectHeight = layerHeight = rnd.Next(1, 20);
                x = y = 0;
            }
            else if (LargeWithBorders)
            {
                rectWidth = rnd.Next(70, 200);
                rectHeight = rnd.Next(20, 70);
                x = rnd.Next(0, 10);
                y = rnd.Next(0, 10);
                layerWidth = x + rectWidth + rnd.Next(0, 10);
                layerHeight = y + rectHeight + rnd.Next(0, 10);
            }

            var result = new BitmapGdi(layerWidth, layerHeight);
            using (var brush = new SolidBrush(Color))
            using (var g = Graphics.FromImage(result.Bitmap))
                g.FillRectangle(brush, x, y, rectWidth, rectHeight);
            return result;
        }
예제 #3
0
        public override BitmapBase Apply(Tank tank, BitmapBase layer)
        {
            var pixels = PixelRect.FromMixed(0, 0, layer.Width, layer.Height);
            if (ShowPixelBorders || PositionByPixels || (SizeByPixels && SizeMode2 != SizeMode2.NoChange && SizeMode2 != SizeMode2.ByPercentage))
                pixels = layer.PreciseSize(PixelAlphaThreshold);
            bool emptyPixels = pixels.Width <= 0 || pixels.Height <= 0;
            if (emptyPixels)
                pixels = PixelRect.FromMixed(0, 0, layer.Width, layer.Height);

            double scaleWidth, scaleHeight;
            int sourceWidth = SizeByPixels ? pixels.Width : layer.Width;
            int sourceHeight = SizeByPixels ? pixels.Height : layer.Height;
            switch (SizeMode2)
            {
                case SizeMode2.NoChange:
                    scaleWidth = scaleHeight = 1;
                    break;
                case SizeMode2.ByPercentage:
                    scaleWidth = scaleHeight = Percentage / 100.0;
                    break;
                case SizeMode2.BySizeWidthOnly:
                    scaleWidth = scaleHeight = Width / (double) sourceWidth;
                    break;
                case SizeMode2.BySizeHeightOnly:
                    scaleWidth = scaleHeight = Height / (double) sourceHeight;
                    break;
                case SizeMode2.BySizeFit:
                    scaleWidth = scaleHeight = Math.Min(Width / (double) sourceWidth, Height / (double) sourceHeight);
                    break;
                case SizeMode2.BySizeStretch:
                    scaleWidth = Width / (double) sourceWidth;
                    scaleHeight = Height / (double) sourceHeight;
                    break;
                default:
                    throw new Exception("7924688");
            }

            if (GrowShrinkMode == GrowShrinkMode.GrowOnly)
            {
                scaleWidth = Math.Max(1.0, scaleWidth);
                scaleHeight = Math.Max(1.0, scaleHeight);
            }
            else if (GrowShrinkMode == GrowShrinkMode.ShrinkOnly)
            {
                scaleWidth = Math.Min(1.0, scaleWidth);
                scaleHeight = Math.Min(1.0, scaleHeight);
            }

            var anchor = (AnchorRaw) Anchor;
            int anchorWidth = (int) Math.Ceiling((PositionByPixels ? pixels.Width : layer.Width) * scaleWidth);
            int anchorHeight = (int) Math.Ceiling((PositionByPixels ? pixels.Height : layer.Height) * scaleHeight);
            // Location of the top left corner of the anchored rectangle
            int tgtX = X - (anchor.HasFlag(AnchorRaw.Right) ? anchorWidth - 1 : anchor.HasFlag(AnchorRaw.Center) ? (anchorWidth - 1) / 2 : 0);
            int tgtY = Y - (anchor.HasFlag(AnchorRaw.Bottom) ? anchorHeight - 1 : anchor.HasFlag(AnchorRaw.Mid) ? (anchorHeight - 1) / 2 : 0);
            // Location of the top left corner of the whole scaled layer image
            double x = tgtX - (PositionByPixels ? pixels.Left * scaleWidth : 0);
            double y = tgtY - (PositionByPixels ? pixels.Top * scaleHeight : 0);

            var src = layer.ToBitmapGdi();
            if (ShowLayerBorders || ShowPixelBorders)
                using (var dc = System.Drawing.Graphics.FromImage(src.Bitmap))
                {
                    if (ShowLayerBorders)
                        dc.DrawRectangle(System.Drawing.Pens.Aqua, 0, 0, layer.Width - 1, layer.Height - 1);
                    if (ShowPixelBorders && !emptyPixels)
                        dc.DrawRectangle(System.Drawing.Pens.Red, pixels.Left, pixels.Top, pixels.Width - 1, pixels.Height - 1);
                }

            #if true
            // Using GDI: sharp-ish downscaling, but imprecise boundaries
            var result = new BitmapGdi(Math.Max(layer.Width, Layer.ParentStyle.IconWidth), Math.Max(layer.Height, Layer.ParentStyle.IconHeight));
            using (var dc = Graphics.FromImage(result.Bitmap))
            {
                dc.InterpolationMode = InterpolationMode.HighQualityBicubic;
                dc.DrawImage(src.Bitmap, (float) x, (float) y, (float) (src.Width * scaleWidth), (float) (src.Height * scaleHeight));
                if (ShowAnchor)
                    using (var pen = new Pen(Color.FromArgb(120, Color.Yellow), 0))
                    {
                        dc.DrawLine(pen, X - 1, Y, X + 1, Y);
                        dc.DrawLine(pen, X, Y - 1, X, Y + 1);
                    }
            }
            #else
            // Using WPF: precise boundaries but rather blurry downscaling
            var result = Ut.NewBitmapWpf(dc =>
            {
                var img = src.ToBitmapWpf().UnderlyingImage;

                var group = new System.Windows.Media.DrawingGroup();
                System.Windows.Media.RenderOptions.SetBitmapScalingMode(group, System.Windows.Media.BitmapScalingMode.Fant);
                group.Children.Add(new System.Windows.Media.ImageDrawing(img, new System.Windows.Rect(x, y, src.Width * scaleWidth, src.Height * scaleHeight)));
                dc.DrawDrawing(group);

                if (ShowTargetPosition)
                {
                    var pen = new System.Windows.Media.Pen(new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(200, 255, 255, 0)), 1);
                    dc.DrawLine(pen, new System.Windows.Point(X - 1 + 0.5, Y + 0.5), new System.Windows.Point(X + 1 + 0.5, Y + 0.5));
                    dc.DrawLine(pen, new System.Windows.Point(X + 0.5, Y - 1 + 0.5), new System.Windows.Point(X + 0.5, Y + 1 + 0.5));
                }
            }, Math.Max(layer.Width, Layer.Style.IconWidth), Math.Max(layer.Height, Layer.Style.IconHeight));
            #endif

            GC.KeepAlive(src);
            return result.ToBitmapRam();
        }
예제 #4
0
파일: Program.cs 프로젝트: clowd/bmplib
        static void WriteTableLine(string file, string defaultReferenceFile = null)
        {
            if (!(/*file.Contains("clip") &&*/ file.EndsWith(".bmp")))
            {
                return;
            }

            var    name          = Path.GetFileNameWithoutExtension(file);
            var    bmpPath       = Path.Combine(outputDir, name + ".bmp");
            var    refPath       = file.Substring(0, file.Length - 4) + ".png";
            var    refTargetPath = Path.Combine(outputDir, name + ".png");
            string error         = "";

            if (File.Exists(refPath))
            {
                File.Copy(refPath, refTargetPath);
            }
            else if (defaultReferenceFile != null)
            {
                refTargetPath = defaultReferenceFile;
            }

            File.Copy(file, bmpPath);
            var originalBytes = File.ReadAllBytes(file);

            File.AppendAllText(htmlPage, $"<tr> <td>{name}</td> <td><img src=\"{refTargetPath.Replace("\\", "/")}\" /><br/><br/><img src=\"{bmpPath.Replace("\\", "/")}\" /></td>");

            // WPF
            try
            {
                string suffix    = "_wpf";
                var    roundPath = Path.Combine(outputDir, name + suffix + ".bmp");
                var    bmp       = BitmapWpf.FromBytes(originalBytes, BitmapReaderFlags.PreserveInvalidAlphaChannel);
                File.WriteAllBytes(roundPath, BitmapWpf.ToBytes(bmp, BitmapWriterFlags.None));

                var pngPath    = Path.Combine(outputDir, name + suffix + ".png");
                var pngEncoder = new PngBitmapEncoder();
                pngEncoder.Frames.Add(BitmapFrame.Create(bmp));
                var ms = new MemoryStream();
                pngEncoder.Save(ms);
                File.WriteAllBytes(pngPath, ms.ToArray());

                File.AppendAllText(htmlPage, $"<td><img src=\"{pngPath.Replace("\\", "/")}\" /><br/><br/><img src=\"{roundPath.Replace("\\", "/")}\" /></td>");
            }
            catch (Exception ex)
            {
                File.AppendAllText(htmlPage, "<td></td>");
                error += ex.ToString();
            }

            // GDI
            try
            {
                string suffix    = "_gdi";
                var    roundPath = Path.Combine(outputDir, name + suffix + ".bmp");
                var    bmp       = BitmapGdi.FromBytes(originalBytes, BitmapReaderFlags.PreserveInvalidAlphaChannel);
                File.WriteAllBytes(roundPath, BitmapGdi.ToBytes(bmp, BitmapWriterFlags.None));

                var pngPath = Path.Combine(outputDir, name + suffix + ".png");
                bmp.Save(pngPath, ImageFormat.Png);

                File.AppendAllText(htmlPage, $"<td><img src=\"{pngPath.Replace("\\", "/")}\" /><br/><br/><img src=\"{roundPath.Replace("\\", "/")}\" /></td>");
            }
            catch (Exception ex)
            {
                File.AppendAllText(htmlPage, "<td></td>");
                error += ex.ToString();
            }

            File.AppendAllText(htmlPage, $"<td>{error}</td> </tr>");
        }