示例#1
0
        //===========================================================================================
        private bool WriteScriptedFile(HttpContext context)
        {
            IXPathNavigable xml = _UrlResolver.Script;

            if (xml == null)
            {
                return(false);
            }

            string cacheFileName = GetCacheFileName(xml);

            if (CanUseCache(cacheFileName))
            {
                WriteFile(context, cacheFileName);
                return(true);
            }

            MagickScript script = new MagickScript(xml);

            script.Read += OnScriptRead;

            using (MagickImage image = script.Execute())
            {
                image.Format = _UrlResolver.Format;
                WriteToCache(image, cacheFileName);
                WriteFile(context, cacheFileName);
            }

            return(true);
        }
示例#2
0
        public static void Resize()
        {
            // Load resize script and execute it
            MagickScript script = new MagickScript(SampleFiles.ResizeMsl);

            script.Execute();
        }
示例#3
0
        public static void WriteMultipleOutputFiles()
        {
            // Load clone script and execute it
            MagickScript script = new MagickScript(SampleFiles.CloneMsl);

            script.Execute();
        }
示例#4
0
            public void ShouldExecuteTheEventsScript()
            {
                MagickScript script = new MagickScript(Files.Scripts.Events);
                script.Read += EventsScriptRead;
                script.Write += EventsScriptWrite;

                script.Execute();
            }
示例#5
0
            public void ShouldExecuteTheDistortScript()
            {
                var          script = new MagickScript(Files.Scripts.Distort);
                IMagickImage image  = script.Execute();

                Assert.IsNotNull(image);
                Assert.AreEqual(500, image.Width);
                Assert.AreEqual(500, image.Height);
            }
示例#6
0
        public void Test_Execute_Distort()
        {
            MagickScript script = new MagickScript(Files.Scripts.Distort);
            MagickImage  image  = script.Execute();

            Assert.IsNotNull(image);
            Assert.AreEqual(500, image.Width);
            Assert.AreEqual(500, image.Height);
        }
示例#7
0
            public void ShouldThrowExceptionWhenReadEventNotSet()
            {
                MagickScript script = new MagickScript(Files.Scripts.Events);

                ExceptionAssert.Throws <InvalidOperationException>(() =>
                {
                    script.Execute();
                }, "The Read event should be bound when the fileName attribute is not set.");
            }
示例#8
0
            public void ShouldThrowExceptionWhenReadEventDoesNotSetImage()
            {
                MagickScript script = new MagickScript(Files.Scripts.Events);
                script.Read += ReadNothing;

                ExceptionAssert.Throws<InvalidOperationException>(() =>
                {
                    script.Execute();
                }, "The Image property should not be null after the Read event has been raised.");
            }
示例#9
0
            public void ShouldExecuteTheDefinesScript()
            {
                MagickScript script = new MagickScript(Files.Scripts.Defines);
                script.Read += DefinesScriptRead;

                IMagickImage image = script.Execute();

                Assert.IsNotNull(image);
                Assert.AreEqual(827, image.Width);
                Assert.AreEqual(700, image.Height);
            }
示例#10
0
    private void CreateScriptedFile(IXPathNavigable xml, string cacheFileName)
    {
      MagickScript script = new MagickScript(xml);
      script.Read += OnScriptRead;

      using (MagickImage image = script.Execute())
      {
        image.Format = UrlResolver.Format;
        WriteToCache(image, cacheFileName);
      }
    }
示例#11
0
            public void ShouldLoadTheVariablesFromTheScript()
            {
                MagickScript script = new MagickScript(Files.Scripts.Variables);

                string[] names = script.Variables.Names.ToArray();
                Assert.AreEqual(4, names.Length);
                Assert.AreEqual("width", names[0]);
                Assert.AreEqual("height", names[1]);
                Assert.AreEqual("color", names[2]);
                Assert.AreEqual("fillColor", names[3]);
            }
示例#12
0
            public void ShouldThrowExceptionWhenWriteEventIsNotSet()
            {
                var script = new MagickScript(Files.Scripts.Events);

                script.Read += EventsScriptRead;

                ExceptionAssert.Throws <InvalidOperationException>(() =>
                {
                    script.Execute();
                }, "The Write event should be bound when the fileName attribute is not set.");
            }
示例#13
0
            public void ShouldExecuteTheCollectionScript()
            {
                MagickScript script = new MagickScript(Files.Scripts.Collection);
                script.Read += CollectionScriptRead;

                IMagickImage image = script.Execute();

                Assert.IsNotNull(image);
                Assert.AreEqual(MagickFormat.Png, image.Format);
                Assert.AreEqual(128, image.Width);
                Assert.AreEqual(128, image.Height);
            }
示例#14
0
        private void CreateScriptedFile(string cacheFileName)
        {
            MagickScript script = new MagickScript(_scriptResolver.Script);

            script.Read += OnScriptRead;

            using (IMagickImage image = script.Execute())
            {
                image.Format = _scriptResolver.OutputFormat;
                WriteToCache(image, cacheFileName);
            }
        }
示例#15
0
        public static void ReadWriteEvents()
        {
            // Load crop script
            MagickScript script = new MagickScript(SampleFiles.CropMsl);

            // Event that will be raised when the script wants to read a file
            script.Read += OnScriptRead;
            // Event that will be raised when the script wants to write a file
            script.Write += OnScriptWrite;
            // Execute the script
            script.Execute();
        }
示例#16
0
        private void CreateScriptedFile(IXPathNavigable xml, string cacheFileName)
        {
            MagickScript script = new MagickScript(xml);

            script.Read += OnScriptRead;

            using (MagickImage image = script.Execute())
            {
                image.Format = UrlResolver.Format;
                WriteToCache(image, cacheFileName);
            }
        }
示例#17
0
            public void ShouldThrowExceptionWhenVariableNotSet()
            {
                MagickScript script = new MagickScript(Files.Scripts.Variables);

                using (IMagickImage image = new MagickImage(Files.MagickNETIconPNG))
                {
                    ExceptionAssert.ThrowsArgumentNullException("attribute", () =>
                    {
                        script.Execute(image);
                    });
                }
            }
示例#18
0
        public void Test_Execute_Draw()
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(Files.Scripts.Draw);

            MagickScript script = new MagickScript(doc);

            using (MagickImage image = new MagickImage(Files.ImageMagickJPG))
            {
                script.Execute(image);
            }
        }
示例#19
0
            public void ShouldThrowExceptionWhenVariableHasInvalidValue()
            {
                MagickScript script = new MagickScript(Files.Scripts.Variables);
                script.Variables["width"] = "test";

                using (IMagickImage image = new MagickImage(Files.MagickNETIconPNG))
                {
                    ExceptionAssert.Throws<FormatException>(() =>
                    {
                        script.Execute(image);
                    });
                }
            }
        public void Test_Execute_Collection()
        {
            MagickScript script = new MagickScript(Files.CollectionScript);

            script.Read += CollectionScriptRead;

            MagickImage image = script.Execute();

            Assert.IsNotNull(image);
            Assert.AreEqual(MagickFormat.Png, image.Format);
            Assert.AreEqual(128, image.Width);
            Assert.AreEqual(30, image.Height);
        }
示例#21
0
            public void ShouldExecutePixelStorageScript()
            {
                MagickScript script = new MagickScript(Files.Scripts.PixelStorage);
                script.Read += PixelStorageScriptRead;

                using (IMagickImage image = script.Execute())
                {
                    Assert.AreEqual(1, image.Width);
                    Assert.AreEqual(2, image.Height);
                    Assert.AreEqual(1, image.ChannelCount);
                    ColorAssert.AreEqual(MagickColors.White, image, 0, 0);
                    ColorAssert.AreEqual(MagickColors.Black, image, 0, 1);
                }
            }
示例#22
0
            public void ShouldExecuteTheDrawScript()
            {
                XmlDocument doc = new XmlDocument();
                using (FileStream stream = File.OpenRead(Files.Scripts.Draw))
                {
                    doc.Load(stream);
                }

                MagickScript script = new MagickScript(doc.CreateNavigator());

                using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
                {
                    script.Execute(image);
                }
            }
示例#23
0
            public void ShouldExecuteTheResizeScript()
            {
                var script = new MagickScript(Files.Scripts.Resize);

                using (IMagickImage image = new MagickImage(Files.ImageMagickJPG))
                {
                    script.Execute(image);
                    TestResizeResult(image);

                    script.Read += ResizeScriptRead;
                    using (IMagickImage result = script.Execute())
                    {
                        TestResizeResult(result);
                    }
                }
            }
示例#24
0
        public void Test_Execute_Resize()
        {
            MagickScript script = new MagickScript(Files.Scripts.Resize);

            using (MagickImage image = new MagickImage(Files.ImageMagickJPG))
            {
                script.Execute(image);
                TestScriptResizeResult(image);

                script.Read += ResizeScriptRead;
                using (MagickImage result = script.Execute())
                {
                    TestScriptResizeResult(result);
                }
            }
        }
示例#25
0
        public void Test_Execute_Variables()
        {
            MagickScript script = new MagickScript(Files.Scripts.Variables);

            string[] names = script.Variables.Names.ToArray();
            Assert.AreEqual(4, names.Length);
            Assert.AreEqual("width", names[0]);
            Assert.AreEqual("height", names[1]);
            Assert.AreEqual("color", names[2]);
            Assert.AreEqual("fillColor", names[3]);

            using (MagickImage image = new MagickImage(Files.MagickNETIconPNG))
            {
                ExceptionAssert.Throws <ArgumentNullException>(delegate()
                {
                    script.Execute(image);
                });

                script.Variables["width"] = "test";

                ExceptionAssert.Throws <FormatException>(delegate()
                {
                    script.Execute(image);
                });

                script.Variables.Set("width", 100);

                ExceptionAssert.Throws <ArgumentNullException>(delegate()
                {
                    script.Execute(image);
                });

                script.Variables["height"] = "100";
                Assert.AreEqual("100", script.Variables.Get("height"));
                script.Variables["color"]     = MagickColors.Yellow;
                script.Variables["fillColor"] = MagickColors.Red;

                script.Execute(image);

                Assert.AreEqual(100, image.Width);
                Assert.AreEqual(100, image.Height);
                ColorAssert.AreEqual(MagickColors.Yellow, image, 0, 0);

                ColorAssert.AreEqual(MagickColors.Yellow, image.Settings.StrokeColor);
                ColorAssert.AreEqual(MagickColors.Red, image.Settings.FillColor);
            }
        }
示例#26
0
            public void ShouldExecuteTheImageProfileScript()
            {
                var script = new MagickScript(Files.Scripts.ImageProfile);

                using (IMagickImage image = new MagickImage(Files.MagickNETIconPNG))
                {
                    var colorProfile = image.GetColorProfile();
                    Assert.IsNull(colorProfile);

                    script.Execute(image);

                    colorProfile = image.GetColorProfile();

                    Assert.IsNotNull(colorProfile);
                    Assert.AreEqual(colorProfile.ToByteArray().Length, ColorProfile.SRGB.ToByteArray().Length);
                }
            }
示例#27
0
        public void Test_Execute_ImageProfile()
        {
            MagickScript script = new MagickScript(Files.Scripts.ImageProfile);

            using (MagickImage image = new MagickImage(Files.MagickNETIconPNG))
            {
                ColorProfile colorProfile = image.GetColorProfile();
                Assert.IsNull(colorProfile);

                script.Execute(image);

                colorProfile = image.GetColorProfile();

                Assert.IsNotNull(colorProfile);
                Assert.AreEqual(colorProfile.ToByteArray().Length, ColorProfile.SRGB.ToByteArray().Length);
            }
        }
示例#28
0
        public void Test_Execute_Variables()
        {
            MagickScript script = new MagickScript(Files.Scripts.Variables);

            string[] names = script.Variables.Names.ToArray();
            Assert.AreEqual(3, names.Length);
            Assert.AreEqual("width", names[0]);
            Assert.AreEqual("height", names[1]);
            Assert.AreEqual("color", names[2]);

            using (MagickImage image = new MagickImage(Files.MagickNETIconPNG))
            {
                ExceptionAssert.Throws <ArgumentNullException>(delegate()
                {
                    script.Execute(image);
                });

                script.Variables["width"] = "test";

                ExceptionAssert.Throws <FormatException>(delegate()
                {
                    script.Execute(image);
                });

                script.Variables.Set("width", 100);

                ExceptionAssert.Throws <ArgumentNullException>(delegate()
                {
                    script.Execute(image);
                });

                script.Variables["height"] = "100";
                Assert.AreEqual("100", script.Variables.Get("height"));
                script.Variables["color"] = Color.Yellow;

                script.Execute(image);

                Assert.AreEqual(100, image.Width);
                Assert.AreEqual(100, image.Height);
                using (PixelCollection pixels = image.GetReadOnlyPixels())
                {
                    ColorAssert.AreEqual(Color.Yellow, pixels.GetPixel(0, 0));
                }
            }
        }
        public string Execute(FileItem item, string infile, string dest, ValuePairEnumerator configData)
        {
            var conf = new ScriptTransformViewModel(configData);

            using (Stream stream = ToStream(conf.Script))
            {
                MagickScript script = new MagickScript(stream);

                // Read image from file
                using (MagickImage image = new MagickImage(infile))
                {
                    // Execute script with the image and write it to a jpg file
                    script.Execute(image);
                    image.Write(dest);
                }
            }
            return(dest);
        }
示例#30
0
        public static void ReuseSameScript()
        {
            // Load wave script
            MagickScript script = new MagickScript(SampleFiles.WaveMsl);

            // Execute script multiple times
            string[] files = new string[] { SampleFiles.FujiFilmFinePixS1ProJpg, SampleFiles.SnakewareJpg };
            foreach (string fileName in files)
            {
                // Read image from file
                using (MagickImage image = new MagickImage(fileName))
                {
                    // Execute script with the image and write it to a jpg file
                    script.Execute(image);
                    image.Write(SampleFiles.OutputDirectory + fileName + ".wave.jpg");
                }
            }
        }
示例#31
0
            public void ShouldExecuteTheVariablesScript()
            {
                MagickScript script = new MagickScript(Files.Scripts.Variables);
                script.Variables.Set("width", 120);
                script.Variables.Set("height", 150);
                script.Variables["color"] = MagickColors.Yellow;
                script.Variables["fillColor"] = MagickColors.Red;

                using (IMagickImage image = new MagickImage(Files.MagickNETIconPNG))
                {
                    script.Execute(image);

                    Assert.AreEqual(120, image.Width);
                    Assert.AreEqual(120, image.Height);
                    ColorAssert.AreEqual(MagickColors.Yellow, image, 0, 0);

                    ColorAssert.AreEqual(MagickColors.Yellow, image.Settings.StrokeColor);
                    ColorAssert.AreEqual(MagickColors.Red, image.Settings.FillColor);
                }
            }
		//===========================================================================================
		private bool WriteScriptedFile(HttpContext context)
		{
			IXPathNavigable xml = _UrlResolver.Script;
			if (xml == null)
				return false;

			string cacheFileName = GetCacheFileName(xml);
			if (CanUseCache(cacheFileName))
			{
				WriteFile(context, cacheFileName);
				return true;
			}

			MagickScript script = new MagickScript(xml);
			script.Read += OnScriptRead;

			using (MagickImage image = script.Execute())
			{
				image.Format = _UrlResolver.Format;
				WriteToCache(image, cacheFileName);
				WriteFile(context, cacheFileName);
			}

			return true;
		}