Exemple #1
0
        public NinePatchImage(ImageBase source, double scale = 1)
        {
            if (source.PixelWidth < 3)
            {
                throw new ArgumentOutOfRangeException(nameof(PixelWidth));
            }
            if (source.PixelHeight < 3)
            {
                throw new ArgumentOutOfRangeException(nameof(PixelHeight));
            }

            var maxX = source.PixelWidth - 1;
            var maxY = source.PixelHeight - 1;

            // Left/Right
            for (var x = 1; x <= maxX; x++)
            {
                if (source[x, 0].A > 0)
                {
                    Left = x - 1; break;
                }
            }
            for (var x = 1; x <= maxX; x++)
            {
                if (source[maxX - x, 0].A > 0)
                {
                    Right = x - 1; break;
                }
            }

            // Top/Bottom
            for (var y = 1; y <= maxY; y++)
            {
                if (source[0, y].A > 0)
                {
                    Top = y - 1; break;
                }
            }
            for (var y = 1; y <= maxY; y++)
            {
                if (source[0, maxY - y].A > 0)
                {
                    Bottom = y - 1; break;
                }
            }

            // PaddingLeft/PaddingRight
            for (var x = 1; x <= maxX; x++)
            {
                if (source[x, maxY].A > 0)
                {
                    PaddingLeft = x - 1; break;
                }
            }
            for (var x = 1; x <= maxX; x++)
            {
                if (source[maxX - x, maxY].A > 0)
                {
                    PaddingRight = x - 1; break;
                }
            }

            // PaddingTop/PaddingBottom
            for (var y = 1; y <= maxY; y++)
            {
                if (source[maxX, y].A > 0)
                {
                    PaddingTop = y - 1; break;
                }
            }
            for (var y = 1; y <= maxY; y++)
            {
                if (source[maxX, maxY - y].A > 0)
                {
                    PaddingBottom = y - 1; break;
                }
            }

            if (scale != 1)
            {
                Left   = Math.Max(1, (int)Math.Round(Left * scale));
                Right  = Math.Max(1, (int)Math.Round(Right * scale));
                Top    = Math.Max(1, (int)Math.Round(Top * scale));
                Bottom = Math.Max(1, (int)Math.Round(Bottom * scale));

                PaddingLeft   = Math.Max(1, (int)Math.Round(PaddingLeft * scale));
                PaddingRight  = Math.Max(1, (int)Math.Round(PaddingRight * scale));
                PaddingTop    = Math.Max(1, (int)Math.Round(PaddingTop * scale));
                PaddingBottom = Math.Max(1, (int)Math.Round(PaddingBottom * scale));
            }

            var w      = source.PixelWidth - 2;
            var h      = source.PixelHeight - 2;
            var pixels = new byte[w * h * 4];

            for (var y = 0; y < h; y++)
            {
                Array.Copy(source.Pixels, ((y + 1) * source.PixelWidth + 1) * 4, pixels, y * w * 4, w * 4);
            }

            SetPixels(w, h, pixels);

            if (scale != 1)
            {
                var resampler = new SuperSamplingSampler();
                var inner     = new Image();
                inner.SetPixels(w, h, pixels);

                w = Math.Max(3, (int)Math.Round((source.PixelWidth - 1) * scale));
                h = Math.Max(3, (int)Math.Round((source.PixelHeight - 1) * scale));

                resampler.Sample(inner, this, w, h);
            }

            patches = new Lazy <IReadOnlyList <ImageBase> >(CreatePatches);
        }
Exemple #2
0
 public static void Save(this ImageBase image, Stream stream, IImageEncoder encoder) => encoder.Encode(image, stream);
Exemple #3
0
 public static void SaveAsPng(this ImageBase image, Stream stream) => new PngEncoder().Encode(image, stream);
Exemple #4
0
        public static void SaveAsJpeg(this ImageBase image, Stream stream, int quality = 80) => new JpegEncoder
        {
            Quality = quality
        }

        .Encode(image, stream);