Exemple #1
0
        public byte[] DrawText(string text, bool x2 = false)
        {
            using (var image = new MagickImage(MagickColors.White, 380, 400))
            {
                var x = new Drawables()
                        .FontPointSize(16)
                        .Font("Courier New")
                        .TextAntialias(true)
                        .StrokeColor(MagickColors.Black)
                        .StrokeWidth(0)
                        .FillColor(MagickColors.Black)
                        .TextAlignment(TextAlignment.Left)
                        .Text(10, 128, text)
                        .Draw(image);

                image.Trim();
                if (x2)
                {
                    image.Extent(new MagickGeometry(-10, -10, image.Width + 20, image.Height * 2 + 20), MagickColors.White);
                }
                else
                {
                    image.Extent(new MagickGeometry(-10, -10, image.Width + 20, image.Height + 20), MagickColors.White);
                }

                return(image.ToByteArray(MagickFormat.Png));
            }
        }
            public void ShouldSortThePixels()
            {
                using (var image = new MagickImage(MagickColors.Blue, 1, 1))
                {
                    image.Extent(1, 2, MagickColors.Green);
                    image.Extent(2, 2, MagickColors.Red);

                    image.SortPixels();

                    ColorAssert.Equal(MagickColors.Blue, image, 0, 0);
                    ColorAssert.Equal(MagickColors.Red, image, 1, 0);
                    ColorAssert.Equal(MagickColors.Red, image, 0, 1);
                    ColorAssert.Equal(MagickColors.Green, image, 1, 1);
                }
            }
        public void Process()
        {
            using (var bg = new MagickImage(MagickColor.FromRgba(255, 255, 255, 255), width, height))
            {
                using (var image = new MagickImage(input))
                {
                    if (image.HasAlpha == false)
                    {
                        image.Alpha(AlphaOption.Opaque);
                    }
                    using (var cover = image.Clone())
                    {
                        MagickGeometry resize = new MagickGeometry(width + "x" + height + "^");
                        image.Resize(resize);
                        image.Extent(width, height, Gravity.Center);
                        image.Blur(80, 30);

                        bg.Composite(image, 0, 0, CompositeOperator.Over);

                        MagickGeometry centerResize = new MagickGeometry(height * 0.7 + "x" + height * 0.7);
                        cover.Resize(centerResize);
                        using (var coverShadow = cover.Clone())
                        {
                            coverShadow.BackgroundColor = MagickColor.FromRgba(192, 192, 192, 192);
                            coverShadow.Shadow(50, 50, 10, new Percentage(90));

                            bg.Composite(coverShadow, Gravity.Center, CompositeOperator.Over);
                        }

                        bg.Composite(cover, Gravity.Center, CompositeOperator.Over);
                    }
                }
                bg.Write(output);
            }
        }
Exemple #4
0
        public static void CropPadding(string path, int pixMin, int pixMax, bool cut)
        {
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }
            PreProcessing(path);

            Random rand = new Random();
            int    pix  = rand.Next(pixMin, pixMax + 1);
            int    w    = img.Width;
            int    h    = img.Height;

            if (!cut)
            {
                w = img.Width + pix * 2;
                h = img.Height + pix * 2;
            }
            else
            {
                w = img.Width - pix;
                h = img.Height - pix;
            }
            img.BackgroundColor = new MagickColor("#" + Config.Get("backgroundColor"));
            MagickGeometry geom = new MagickGeometry(w + "x" + h + "!");

            img.Extent(geom, Gravity.Center);

            img.Write(path);
            PostProcessing(img, path);
        }
            public void ShouldBeAbleToConvertGrayImage()
            {
                using (var image = new MagickImage(MagickColors.Magenta, 5, 1))
                {
                    image.Extent(5, 2, MagickColors.Honeydew);

                    image.ColorType = ColorType.Bilevel;
                    image.ClassType = ClassType.Direct;

                    Assert.Equal(ColorSpace.Gray, image.ColorSpace);

                    using (var bitmap = image.ToBitmapWithDensity())
                    {
                        for (var i = 0; i < image.Width; i++)
                        {
                            ColorAssert.Equal(MagickColors.Black, ToMagickColor(bitmap.GetPixel(i, 0)));
                        }

                        for (var i = 0; i < image.Width; i++)
                        {
                            ColorAssert.Equal(MagickColors.White, ToMagickColor(bitmap.GetPixel(i, 1)));
                        }
                    }
                }
            }
Exemple #6
0
        public MagickImage GetText()
        {
            var image = new MagickImage(new MagickColor(255, 255, 255, 0), 4000, 2000);

            var drawables = new Drawables()
                            //.Rotation(30)
                            .Text(0, 0, Text)
                            .TextAntialias(true)
                            .Font("CarbonRegular W00")
                            .FontPointSize(387)
                            //.FontPointSize(360)
                            //.FontPointSize(-1)
                            .TextInterlineSpacing(-50)
                            .TextKerning(20)
                            //.TextKerning(9)
                            .FillColor(TextColor)
                            .Gravity(Gravity.West);

            //return drawables;
            image.Draw(drawables);

            image.Scale(new Percentage(GetScalingFactor() * 100));
            image.Trim();
            image.Extent(Math.Max(image.Width, image.Height), Math.Max(image.Width, image.Height), Gravity.West);
            image.BorderColor = new MagickColor("transparent");
            image.Border(20);
            image.BorderColor = new MagickColor("#ffffff01");
            image.Border(1);
            //image.Write("./text-face.png");

            return(image);
        }
Exemple #7
0
        private void crop_images()
        {
            foreach (var s in sheet.sprites)
            {
                using (MagickImage image_a = new MagickImage(alphaPath))
                {
                    using (MagickImage image_rgb = new MagickImage(rgbPath))
                    {
                        var g = new ImageMagick.MagickGeometry(s.x, s.y, s.w, s.h);
                        image_a.Crop(g);

                        image_rgb.Crop(g);
                        image_rgb.Composite(image_a, CompositeOperator.CopyAlpha);
                        int i = 1;
                        while (i < Math.Max(s.w, s.h))
                        {
                            i = i * 2;
                        }
                        image_rgb.Extent(new MagickGeometry(-(i / 2 - s.w / 2), -(i / 2 - s.h / 2), i, i), MagickColor.FromRgba(255, 255, 255, 0));
                        image_rgb.Write(folderPath + "/" + s.n);
                    }
                }
                //return;
            }
        }
Exemple #8
0
 public void ShouldThrowExceptionWhenGeometryIsNull()
 {
     using (var image = new MagickImage(MagickColors.Black, 1, 1))
     {
         Assert.Throws <ArgumentNullException>("geometry", () => image.Extent(null));
     }
 }
Exemple #9
0
        private void MergeImage(string phase, string[] words)
        {
            var position = 1;

            using (MagickImageCollection images = new MagickImageCollection())
            {
                foreach (var line in words)
                {
                    foreach (var word in line)
                    {
                        MagickImage image = new MagickImage($"~/Words/{word}/{word}-{phase}.png");
                        //image.Extent(90, 400, position == 1 ? Gravity.Center : Gravity.South);
                        image.Extent(-5, -90 * position, 90, 400);
                        //image.Resize(500, 350);
                        image.Transparent(new MagickColor("#ffffff"));
                        images.Add(image);
                    }

                    position++;
                }

                using (MagickImage horizontal = images.AppendHorizontally())
                {
                    MagickImage background = new MagickImage("~/Words/background.png");
                    horizontal.Composite(background, CompositeOperator.DstAtop);

                    Directory.CreateDirectory($"{OutputPath}/{FolderName}");
                    horizontal.Write($"{OutputPath}/{FolderName}/{FolderName}-{phase}.png");
                    background.Dispose();
                }
            }
        }
Exemple #10
0
        public static void TiledText()
        {
            // Dynamic label height
            using (MagickImageCollection images = new MagickImageCollection())
            {
                // Read the image you want to put a label on.
                MagickImage logo = new MagickImage("radial-gradient:green-yellow", 400, 175);
                logo.Lower(8);
                images.Add(logo);

                MagickImage label = new MagickImage();
                label.BackgroundColor        = new MagickColor();
                label.Settings.FontPointsize = 70;
                label.Read("caption:Magick.NET is coming for you tinight");
                label.Scale(logo.Width, logo.Height);
                // Resize the label to the width of the image and place the text in the center
                label.Extent(logo.Width, logo.Height, Gravity.Center);
                images.Add(label);

                // Append the images
                using (MagickImage output = images.Flatten())
                {
                    output.Alpha(AlphaOption.Set);
                    output.Transparent(new MagickColor(System.Drawing.Color.White));
                    output.Blur(0, 8);
                    output.Threshold((Percentage)50);
                    ///output.Polaroid("", 2, PixelInterpolateMethod.Bilinear);
                    output.Write("output-b.png");
                }
            }
        }
Exemple #11
0
        public static MagickImage PadImage(MagickImage image, int x, int y)
        {
            MagickImage result = (MagickImage)image.Clone();

            int[] newDimensions = Helper.GetGoodDimensions(image.Width, image.Height, x, y);
            result.Extent(newDimensions[0], newDimensions[1]);
            return(result);
        }
Exemple #12
0
        public static MagickImage ResizeAndFill(MagickImage img, MagickGeometry size, MagickColor fill)
        {
            MagickImage i = (MagickImage)img.Clone();

            i.Resize(size);
            i.Extent(size, Gravity.Center, fill);
            return(i);
        }
Exemple #13
0
        public void PrepareForPlate(MagickImage image, Plate plate, Gravity gravity = Gravity.Center, bool extent = true)
        {
            image.BackgroundColor = new MagickColor(255, 255, 255, 0);
            switch (plate)
            {
            case Plate.Left:
                if (extent)
                {
                    image.Trim();
                    image.Extent(Math.Max(image.Width, image.Height), Math.Max(image.Width, image.Height), gravity);
                    image.BorderColor = new MagickColor("transparent");
                    image.Border(20);
                    image.BorderColor = new MagickColor("#ffffff01");
                    image.Border(1);
                }
                image.Shear(-30, 0);
                image.Rotate(30);
                break;

            case Plate.Right:
                image.Trim();
                image.Extent(Math.Max(image.Width, image.Height), Math.Max(image.Width, image.Height), gravity);
                image.BorderColor = new MagickColor("transparent");
                image.Border(20);
                image.BorderColor = new MagickColor("#ffffff01");
                image.Border(1);
                image.Shear(30, 0);
                image.Rotate(-30);
                break;

            case Plate.Top:
                //image.Scale(new Percentage(GetScalingFactor() * 100));
                image.Trim();
                image.Extent(Math.Max(image.Width, image.Height), Math.Max(image.Width, image.Height), gravity);
                image.BorderColor = new MagickColor("transparent");
                image.Border(10);
                image.BorderColor = new MagickColor("#ffffff01");
                image.Border(1);
                image.Shear(30, 0);
                image.Rotate(30);
                break;
            }
            image.Trim();
        }
        public void Waaw()
        {
            MagickImage clone = new MagickImage(image);

            clone.Flop();
            clone.Extent(clone.Width / 2, clone.Height);
            image.Composite(clone, CompositeOperator.Over);

            clone.Dispose();
        }
Exemple #15
0
                public void ShouldExtentTheImage()
                {
                    using (var image = new MagickImage(MagickColors.Black, 1, 1))
                    {
                        image.Extent(new MagickGeometry(2, 3));

                        Assert.Equal(2, image.Width);
                        Assert.Equal(3, image.Height);
                    }
                }
        public void Haah()
        {
            MagickImage clone = new MagickImage(image);

            clone.Flip();
            clone.Extent(clone.Width, clone.Height / 2);
            image.Composite(clone, CompositeOperator.Over);

            clone.Dispose();
        }
Exemple #17
0
        public static void CropDivisible(string path, int divisibleBy, Gravity grav, bool expand)
        {
            MagickImage img = IOUtils.ReadImage(path);

            if (img == null)
            {
                return;
            }

            int divisbleWidth   = img.Width;
            int divisibleHeight = img.Height;

            if (!expand) // Crop
            {
                while (divisbleWidth % divisibleBy != 0)
                {
                    divisbleWidth--;
                }
                while (divisibleHeight % divisibleBy != 0)
                {
                    divisibleHeight--;
                }
            }
            else // Expand
            {
                while (divisbleWidth % divisibleBy != 0)
                {
                    divisbleWidth++;
                }
                while (divisibleHeight % divisibleBy != 0)
                {
                    divisibleHeight++;
                }
                img.BackgroundColor = new MagickColor("#" + Config.Get("backgroundColor"));
            }

            if (divisbleWidth == img.Width && divisibleHeight == img.Height)
            {
                Program.Print("-> Skipping " + Path.GetFileName(path) + " as its resolution is already divisible by " + divisibleBy);
            }
            else
            {
                Program.Print("-> Divisible resolution: " + divisbleWidth + "x" + divisibleHeight);
                if (!expand) // Crop
                {
                    img.Crop(divisbleWidth, divisibleHeight, grav);
                }
                else // Expand
                {
                    img.Extent(divisbleWidth, divisibleHeight, grav);
                }
                img.RePage();
                img.Write(path);
            }
        }
Exemple #18
0
                public void ShouldExtentTheImageWithTheSpecifiedColor()
                {
                    using (var image = new MagickImage(MagickColors.Black, 1, 1))
                    {
                        image.Extent(new MagickGeometry(2, 3), MagickColors.Purple);

                        Assert.Equal(2, image.Width);
                        Assert.Equal(3, image.Height);
                        ColorAssert.Equal(MagickColors.Purple, image, 1, 1);
                    }
                }
Exemple #19
0
                public void ShouldExtentTheImageWithTheSpecifiedGravityAndColor()
                {
                    using (var image = new MagickImage(MagickColors.Black, 1, 1))
                    {
                        image.Extent(2, 3, Gravity.Northwest, MagickColors.Purple);

                        Assert.Equal(2, image.Width);
                        Assert.Equal(3, image.Height);
                        ColorAssert.Equal(MagickColors.Purple, image, 1, 1);
                    }
                }
Exemple #20
0
                public void ShouldExtentTheImageAtOffset()
                {
                    using (var image = new MagickImage(MagickColors.Black, 1, 1))
                    {
                        image.BackgroundColor = MagickColors.Purple;
                        image.Extent(-1, -1, 2, 3);

                        Assert.Equal(2, image.Width);
                        Assert.Equal(3, image.Height);
                        ColorAssert.Equal(MagickColors.Purple, image, 0, 0);
                    }
                }
Exemple #21
0
                public void ShouldExtentTheImageWithTheSpecifiedGravity()
                {
                    using (var image = new MagickImage(MagickColors.Black, 1, 1))
                    {
                        image.BackgroundColor = MagickColors.Purple;
                        image.Extent(2, 3, Gravity.Southeast);

                        Assert.Equal(2, image.Width);
                        Assert.Equal(3, image.Height);
                        ColorAssert.Equal(MagickColors.Purple, image, 0, 0);
                    }
                }
        public void Haah()
        {
            for (int i = 0; i < image.Count; i++)
            {
                MagickImage clone = new MagickImage(image[i]);
                clone.Flip();
                clone.Extent(clone.Width, clone.Height / 2);
                image[i].Composite(clone, CompositeOperator.Over);

                clone.Dispose();
            }
        }
Exemple #23
0
        /// <summary>
        /// ベース画像に新規画像を重ねるときに必要な、サイズの補正を行う
        /// </summary>
        /// <param name="baseImg">ベース画像</param>
        /// <param name="newImg">新規画像 (補正が必要であれば変更される)</param>
        protected virtual void SizeAdjustForComposingBitmap(MagickImage baseImg, ref MagickImage newImg, int?newLayerOffsetX = null, int?newLayerOffsetY = null)
        {
            // オフセットが指定されているかどうか
            var newLayerOffsetSpecified = (newLayerOffsetX.HasValue && newLayerOffsetY.HasValue);

            // 新規画像の方が縦横ともに小さい場合、余白を追加して補正
            if (newImg.Width <= baseImg.Width && newImg.Height <= baseImg.Height)
            {
                var backgroundColor = ImageMagick.MagickColor.FromRgba(255, 255, 255, 0);
                if (newLayerOffsetSpecified)
                {
                    var offsetX = newLayerOffsetX.Value;
                    var offsetY = newLayerOffsetY.Value;

                    // オフセット指定がある場合、オフセット分の余白を左上に追加し、その後に右下に余白追加
                    newImg.Extent(offsetX + newImg.Width, offsetY + newImg.Height,
                                  gravity: ImageMagick.Gravity.Southeast, // 右下寄せ
                                  backgroundColor: backgroundColor);      // アルファチャンネルで透過色を設定
                    newImg.RePage();                                      // ページ情報更新
                    newImg.Extent(baseImg.Width, baseImg.Height,
                                  gravity: ImageMagick.Gravity.Northwest, // 左上寄せ
                                  backgroundColor: backgroundColor);      // アルファチャンネルで透過色を設定
                }
                else
                {
                    // オフセット指定がなければ、右下に余白追加
                    newImg.Extent(baseImg.Width, baseImg.Height,
                                  gravity: ImageMagick.Gravity.Northwest, // 左上寄せ
                                  backgroundColor: backgroundColor);      // アルファチャンネルで透過色を設定
                }
            }

            // 新規画像の方が縦横ともに大きい場合、余分な幅を切る
            if (newImg.Width >= baseImg.Width && newImg.Height >= baseImg.Height)
            {
                newImg.Crop(baseImg.Width, baseImg.Height);
            }
        }
            public void ShouldTrimTheSpecifiedEdge(Gravity edge, int width, int height, int redX, int redY)
            {
                using (var image = new MagickImage(MagickColors.Red, 1, 1))
                {
                    image.Extent(3, 3, Gravity.Center, MagickColors.White);

                    image.Trim(edge);

                    Assert.Equal(width, image.Width);
                    Assert.Equal(height, image.Height);

                    ColorAssert.Equal(MagickColors.Red, image, redX, redY);
                }
            }
        public override void Convert()
        {
            MagickImage image = Converter.MyPreviewImage;

            using (MagickImage color = new MagickImage(image.Clone()))
            {
                int length = Math.Max(color.Height, color.Width) + 2;
                length -= (length % 4);

                color.Format = MagickFormat.Png32;
                color.Extent(length, length, Gravity.Center, new MagickColor(0, 0, 0, 0));

                color.ColorAlpha(MagickColors.None);

                color.Format = MagickFormat.Dds;
                color.Settings.SetDefine(MagickFormat.Dds, "compression", "dxt5"); //dxt1, dxt5, none
                color.Settings.SetDefine(MagickFormat.Dds, "fast-mipmaps", "true");
                color.Settings.SetDefine(MagickFormat.Dds, "mipmaps", "10");
                color.Settings.SetDefine(MagickFormat.Dds, "cluster-fit", "true");
                color.Write(MyImagePath + "_c.dds");
            }

            if (GenerateMask)
            {
                using (MagickImage alpha = new MagickImage(image.Clone()))
                {
                    int length = Math.Max(alpha.Height, alpha.Width) + 2;
                    length -= (length % 4);

                    alpha.Format = MagickFormat.Png;
                    alpha.Extent(length, length, Gravity.Center, new MagickColor(0, 0, 0, 0));

                    alpha.Grayscale();
                    foreach (Pixel p in alpha.GetPixels())
                    {
                        MagickColor c = (MagickColor)p.ToColor();
                        p.SetChannel(0, c.A);
                        p.SetChannel(1, 0);
                    }

                    alpha.Format = MagickFormat.Dds;
                    alpha.Settings.SetDefine(MagickFormat.Dds, "compression", "dxt5"); //dxt1, dxt5, none
                    alpha.Settings.SetDefine(MagickFormat.Dds, "fast-mipmaps", "true");
                    alpha.Settings.SetDefine(MagickFormat.Dds, "mipmaps", "10");
                    alpha.Settings.SetDefine(MagickFormat.Dds, "cluster-fit", "true");
                    alpha.Write(MyImagePath + "_a.dds");
                }
            }
        }
        public void Test_BorderColor()
        {
            using (MagickImage image = new MagickImage(MagickColors.MediumTurquoise, 10, 10))
            {
                ColorAssert.AreEqual(new MagickColor("#df"), image.Settings.BorderColor);

                image.Settings.FillColor   = MagickColors.Beige;
                image.Settings.BorderColor = MagickColors.MediumTurquoise;
                image.Extent(20, 20, Gravity.Center, MagickColors.Aqua);
                image.Draw(new DrawableAlpha(0, 0, PaintMethod.FillToBorder));

                ColorAssert.AreEqual(MagickColors.Beige, image, 0, 0);
                ColorAssert.AreEqual(MagickColors.MediumTurquoise, image, 10, 10);
            }
        }
Exemple #27
0
            public void ShouldUseImageBackground()
            {
                using (var images = new MagickImageCollection())
                {
                    var image = new MagickImage(MagickColors.Red, 10, 10);
                    image.Extent(110, 110, Gravity.Center, MagickColors.None);
                    image.BackgroundColor = MagickColors.Moccasin;

                    images.Add(image);

                    using (var result = images.Flatten())
                    {
                        ColorAssert.Equal(MagickColors.Moccasin, result, 0, 0);
                    }
                }
            }
            public void ShouldTrimTheBackgroundVertically()
            {
                using (var image = new MagickImage(MagickColors.Red, 1, 1))
                {
                    image.Extent(3, 3, Gravity.Center, MagickColors.White);

                    image.Trim(Gravity.North, Gravity.South);

                    Assert.Equal(3, image.Width);
                    Assert.Equal(1, image.Height);

                    ColorAssert.Equal(MagickColors.White, image, 0, 0);
                    ColorAssert.Equal(MagickColors.Red, image, 1, 0);
                    ColorAssert.Equal(MagickColors.White, image, 2, 0);
                }
            }
Exemple #29
0
                public void ShouldAddTransparency()
                {
                    using (IMagickImage image = new MagickImage(MagickColors.Red, 2, 1))
                    {
                        using (IMagickImage alpha = new MagickImage(MagickColors.Black, 1, 1))
                        {
                            alpha.BackgroundColor = MagickColors.White;
                            alpha.Extent(2, 1, Gravity.East);

                            image.Composite(alpha, CompositeOperator.CopyAlpha);

                            ColorAssert.AreEqual(MagickColors.Red, image, 0, 0);
                            ColorAssert.AreEqual(new MagickColor("#f000"), image, 1, 0);
                        }
                    }
                }
            public void ShouldTrimTheBackgroundHorizontally()
            {
                using (var image = new MagickImage(MagickColors.Red, 1, 1))
                {
                    image.Extent(3, 3, Gravity.Center, MagickColors.White);

                    image.Trim(Gravity.East, Gravity.West);

                    Assert.Equal(1, image.Width);
                    Assert.Equal(3, image.Height);

                    ColorAssert.Equal(MagickColors.White, image, 0, 0);
                    ColorAssert.Equal(MagickColors.Red, image, 0, 1);
                    ColorAssert.Equal(MagickColors.White, image, 0, 2);
                }
            }
		public void Test_Extent()
		{
			using (MagickImage image = new MagickImage())
			{
				image.Read(Files.RedPNG);
				image.Resize(new MagickGeometry(100, 100));
				Assert.AreEqual(100, image.Width);
				Assert.AreEqual(33, image.Height);

				image.BackgroundColor = MagickColor.Transparent;
				image.Extent(100, 100, Gravity.Center);
				Assert.AreEqual(100, image.Width);
				Assert.AreEqual(100, image.Height);

				using (PixelCollection pixels = image.GetReadOnlyPixels())
				{
					Assert.IsTrue(pixels.GetPixel(0, 0).ToColor() == MagickColor.Transparent);
				}
			}
		}