public Bitmap ExporteImagemSemTileMap(List <SixLabors.ImageSharp.Image> tiles, Bitmap imagemFinal)
        {
            if (tiles.Count > 0)
            {
                int contadorTiles = 0;

                using (SixLabors.ImageSharp.Image <Rgba32> image = new SixLabors.ImageSharp.Image <Rgba32>(imagemFinal.Width, imagemFinal.Height))
                {
                    for (int y = 0; y < imagemFinal.Height; y += 8)
                    {
                        Span <Rgba32> pixelRowSpan = image.GetPixelRowSpan(y);
                        for (int x = 0; x < imagemFinal.Width; x += 8)
                        {
                            image.Mutate(o => o.DrawImage(tiles[contadorTiles], new SixLabors.ImageSharp.Point(x, y), 1f));
                            tiles[contadorTiles].Dispose();
                            //pixelRowSpan[x] = new Rgba32(x / 255, y / 255, 50, 255);
                            contadorTiles++;
                        }
                    }


                    return(image.ToBitmap());
                }
            }

            return(null);
        }
示例#2
0
        public Image(SixLabors.ImageSharp.Image <Rgba32> image)
        {
            //ImageSharp loads from the top-left pixel, whereas OpenGL loads from the bottom-left.
            image.Mutate(context => context.Flip(FlipMode.Vertical));
            image.TryGetSinglePixelSpan(out var span);
            //CheckSize(image.Width, image.Height);
            var pixels = new byte[(image.Width * image.Height) << 2];

            for (var i = 0; i < span.Length; i++)
            {
                var rgba32 = span[i];
                var tmp1   = i << 2;
                pixels[tmp1]        = rgba32.R;
                pixels[tmp1 | 0b01] = rgba32.G;
示例#3
0
        public static System.IO.Stream WriteTextToImage(string text)
        {
            using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image <SixLabors.ImageSharp.PixelFormats.Rgba32> .Load("Assets/share-bg.png"))
            {
                SixLabors.Fonts.FontCollection fontCollection = new SixLabors.Fonts.FontCollection();
                SixLabors.Fonts.Font           regularFont    = fontCollection.Install("Assets/TitilliumWeb-SemiBold.ttf").CreateFont(24, SixLabors.Fonts.FontStyle.Regular);
                SixLabors.Fonts.Font           italicFont     = fontCollection.Install("Assets/TitilliumWeb-BoldItalic.ttf").CreateFont(24, SixLabors.Fonts.FontStyle.Italic);

                image.Mutate(x => x.DrawText(text, regularFont, SixLabors.ImageSharp.Color.White, new SixLabors.ImageSharp.PointF(100, 100)));

                System.IO.MemoryStream stream = new System.IO.MemoryStream();
                image.Save(stream, new SixLabors.ImageSharp.Formats.Png.PngEncoder());
                stream.Position = 0;
                return(stream);
            }
        }
        public Bitmap LeiaTileMapERetorneImagem(List <ushort> tileMap, List <SixLabors.ImageSharp.Image> tiles, Bitmap imagemFinal)
        {
            int contadorTileMap = 0;
            int resY            = tileMap.Count / 32 * 8;

            imagemFinal = new Bitmap(256, resY);


            using (SixLabors.ImageSharp.Image <Rgba32> image = new SixLabors.ImageSharp.Image <Rgba32>(imagemFinal.Width, imagemFinal.Height))
            {
                for (int y = 0; y < imagemFinal.Height; y += 8)
                {
                    for (int x = 0; x < imagemFinal.Width; x += 8)
                    {
                        int valor   = tileMap[contadorTileMap];
                        int tileNum = valor & 0x3FF;
                        valor = valor >> 10;

                        var tile = tiles[tileNum].Clone(i => i.Flip(FlipMode.None));

                        int horizotal = valor & 1;

                        valor = valor >> 1;

                        int vertical = valor & 1;

                        if (horizotal == 1)
                        {
                            tile.Mutate(t => t.RotateFlip(RotateMode.Rotate180, FlipMode.Vertical));
                        }

                        if (vertical == 1)
                        {
                            tile.Mutate(t => t.RotateFlip(RotateMode.Rotate180, FlipMode.Horizontal));
                        }

                        image.Mutate(o => o.DrawImage(tile, new SixLabors.ImageSharp.Point(x, y), 1f));
                        contadorTileMap++;
                    }
                }


                return(image.ToBitmap());
            }
        }
示例#5
0
        private void Load(string relativePath)
        {
            GL.BindTexture(TextureTarget.Texture2D, Handle);
            //Load the image
            SixLabors.ImageSharp.Image <SixLabors.ImageSharp.PixelFormats.Rgba32> image = SixLabors.ImageSharp.Image.Load <SixLabors.ImageSharp.PixelFormats.Rgba32>(relativePath);

            //ImageSharp loads from the top-left pixel, whereas OpenGL loads from the bottom-left, causing the texture to be flipped vertically.
            //This will correct that, making the texture display properly.
            image.Mutate(x => x.Flip(FlipMode.Vertical));

            //Get an array of the pixels, in ImageSharp's internal format.
            image.TryGetSinglePixelSpan(out var span);
            SixLabors.ImageSharp.PixelFormats.Rgba32[] pixels = span.ToArray();

            //put this data into the active texture
            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, image.Width, image.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, pixels);
            GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
        }
示例#6
0
 private void readImage(Stream stream)
 {
     image = SixLabors.ImageSharp.Image.Load(stream);
     if (Width == 0)
     {
         Width = image.Width;
     }
     if (Height == 0)
     {
         Height = image.Height;
     }
     image.Mutate(ctx =>
     {
         if (Width != image.Width || Height != image.Height)
         {
             ctx.Resize(Width, Height, KnownResamplers.Box);
         }
     });
 }
示例#7
0
        } // End Function Crop

        public override System.IO.Stream ResizeImage(
            System.IO.Stream inputStream,
            System.IO.Stream outputStream,
            SaveFormat saveFormat,
            float ratio,
            System.Drawing.Size?maxSize)
        {
            SixLabors.ImageSharp.Image <SixLabors.ImageSharp.Rgba32> img =
                SixLabors.ImageSharp.Image.Load(inputStream);
            SixLabors.Primitives.SizeF oldSizeSize = img.Size();

            if (maxSize.HasValue)
            {
                float ratioX = (float)maxSize.Value.Width / (float)img.Width;
                float ratioY = (float)maxSize.Value.Height / (float)img.Width;

                ratio = ratioX < ratioY ? ratioX : ratioY;
            } // End if (maxSize.HasValue)

            SixLabors.Primitives.SizeF newSize = oldSizeSize * ratio;

            if (outputStream == null)
            {
                outputStream = new System.IO.MemoryStream();
            }


            // Compand: whether to compress or expand individual pixel color values
            img.Mutate(x => x.Resize((SixLabors.Primitives.Size)newSize, KnownResamplers.Bicubic, false));

            img.Save(outputStream, GetEncoder(saveFormat));


            // NO, no seek, if we're respone.OutputStream
            if (outputStream.CanSeek)
            {
                outputStream.Seek(0, System.IO.SeekOrigin.Begin);
            }

            return(outputStream);
        } // End Function ResizeImage
示例#8
0
        } // End Function GetEncoder

        public override System.IO.Stream Crop(
            System.IO.Stream stream,
            SaveFormat saveFormat,
            int x, int y, int width, int height)
        {
            SixLabors.ImageSharp.Image <SixLabors.ImageSharp.Rgba32> img =
                SixLabors.ImageSharp.Image.Load(stream);

            System.IO.Stream outputStream = new System.IO.MemoryStream();

            img.Mutate(a => a.Crop(new SixLabors.Primitives.Rectangle(x, y, width, height)));

            img.Save(outputStream, GetEncoder(saveFormat));

            // NO, no seek, if we're respone.OutputStream
            if (outputStream.CanSeek)
            {
                outputStream.Seek(0, System.IO.SeekOrigin.Begin);
            }

            return(outputStream);
        } // End Function Crop
示例#9
0
        private void TODO(string path)
        {
            SixLabors.ImageSharp.Image <SixLabors.ImageSharp.Rgba32> img =
                SixLabors.ImageSharp.Image.Load(path);

            img.Mutate(x => x.Grayscale());
            img.Mutate(x => x.BlackWhite());
            img.Mutate(x => x.Invert());

            img.Mutate(x => x.Flip(FlipType.Horizontal));

            img.Mutate(x => x.RotateFlip(RotateType.Rotate270, FlipType.None));

            float degrees = 30;

            img.Mutate(x => x.Rotate(degrees, KnownResamplers.Bicubic));


            float degreesX = 5, degreesY = 5;

            img.Mutate(x => x.Skew(degreesX, degreesY, KnownResamplers.Bicubic));


            img.Mutate(x => x.OilPaint());
            img.Mutate(x => x.Sepia());


            img.Mutate(x => x.Pixelate());


            SixLabors.Primitives.Rectangle bounds            =
                new SixLabors.Primitives.Rectangle(10, 10, img.Width / 2, img.Height / 2);
            img.Mutate(x => x.BackgroundColor(SixLabors.ImageSharp.PixelFormats.NamedColors <
                                                  SixLabors.ImageSharp.Rgba32> .HotPink, bounds));

            img.Mutate(x => x.GaussianBlur());
            img.Mutate(x => x.GaussianSharpen());
            img.Mutate(x => x.DetectEdges());


            img.Mutate(x => x.Glow());
            img.Mutate(x => x.Vignette());


            SixLabors.ImageSharp.Dithering.IOrderedDither defaultDitherer =
                new SixLabors.ImageSharp.Dithering.OrderedDither();
            img.Mutate(x => x.Dither(defaultDitherer));

            SixLabors.ImageSharp.Dithering.IErrorDiffuser ad =
                new SixLabors.ImageSharp.Dithering.AtkinsonDiffuser();
            float threshold = 0.5f;

            img.Mutate(x => x.Dither(ad, threshold));


            SixLabors.Primitives.Rectangle thresholdBounds =
                new SixLabors.Primitives.Rectangle(10, 10, img.Width / 2, img.Height / 2);
            float thresholdValue = 0.5f;

            img.Mutate(x => x.BinaryThreshold(thresholdValue, thresholdBounds));


            int value = 5;

            SixLabors.Primitives.Rectangle blurBounds =
                new SixLabors.Primitives.Rectangle(10, 10, img.Width / 2, img.Height / 2);
            img.Mutate(x => x.BoxBlur(value, blurBounds));

            img.Mutate(x => x.ColorBlindness(ColorBlindness.Tritanopia));
        }
示例#10
0
        public async Task <string> Post(IFormFile image)
        {
            string f      = Directory.GetCurrentDirectory();
            string path   = "/uploads/img/" + string.Format(@"{0}.webp", DateTime.Now.Ticks);
            int    result = 0;
            string response;
            Image  responseImage = new Image();

            try
            {
                using (AnimalProjectDbContext _context = new AnimalProjectDbContext())
                {
                    int maxid = 0;
                    if (_context.Images.Count() > 0)
                    {
                        maxid = _context.Images.OrderByDescending(a => a.id).FirstOrDefault().id;
                    }

                    responseImage.id = maxid + 1;
                    if (image == null || image.Length == 0)
                    {
                        response = BadRequest().StatusCode.ToString() + "," + "Add at least 1 photo";
                    }
                    else
                    {
                        var resizedImgStream = new MemoryStream();

                        using (var memoryStream = new MemoryStream())
                        {
                            await image.CopyToAsync(memoryStream);

                            responseImage.sha = GetHashSha256(memoryStream);
                            if (_context.Images.Any(a => a.sha == responseImage.sha))
                            {
                                responseImage = _context.Images.First(a => a.sha == responseImage.sha);
                                response      = responseImage.path;
                            }
                            else
                            {
                                IResampler sampler = KnownResamplers.Lanczos3;
                                memoryStream.Position = 0;
                                SixLabors.ImageSharp.Image img = SixLabors.ImageSharp.Image.Load(memoryStream);

                                // TODO: ResizeImage(img, 100, 100);
                                if (img.Width * img.Height > 2073600)
                                {
                                    if (img.Width == img.Height)
                                    {
                                        img.Mutate(x => x.Resize(1080, 1080, sampler));
                                    }
                                    else
                                    {
                                        if (img.Width > img.Height)
                                        {
                                            //16:9 case else 4:3 case
                                            if ((img.Width / img.Height) > 1.34)
                                            {
                                                img.Mutate(x => x.Resize(1920, 1080, sampler));
                                            }
                                            else
                                            {
                                                img.Mutate(x => x.Resize(1280, 960, sampler));
                                            }
                                        }
                                        else
                                        {
                                            //16:9 case else 4:3 case
                                            if ((img.Width / img.Height) > 1.34)
                                            {
                                                img.Mutate(x => x.Resize(1080, 1920, sampler));
                                            }
                                            else
                                            {
                                                img.Mutate(x => x.Resize(960, 1280, sampler));
                                            }
                                        }
                                    }

                                    img.Save(resizedImgStream, new BmpEncoder());
                                    using (var webPImage = new ImageFactory(preserveExifData: false))
                                    {
                                        resizedImgStream.Position = 0;
                                        webPImage.Load(resizedImgStream)
                                        .Format(new WebPFormat())
                                        .Quality(90)
                                        .Save(Directory.GetCurrentDirectory() + path);
                                    }
                                    response = path;
                                }
                                else
                                {
                                    using (var webPImage = new ImageFactory(preserveExifData: false))
                                    {
                                        memoryStream.Position = 0;
                                        webPImage.Load(memoryStream)
                                        .Format(new WebPFormat())
                                        .Quality(90)
                                        .Save(Directory.GetCurrentDirectory() + path);
                                    }
                                    response = path;
                                }

                                responseImage.path = path;
                                _context.Images.Add(responseImage);
                                try
                                {
                                    result = _context.SaveChanges();
                                }
                                catch (Exception e)
                                {
                                    response = e.Message;
                                }
                            }
                        };
                    }
                }
            }
            catch (Exception e)
            {
                response = "Fail" + Environment.NewLine + e.Message;
            }

            return(response);
        }