Read() публичный Метод

Read single image frame.
Thrown when an error is raised by ImageMagick.
public Read ( Bitmap bitmap ) : void
bitmap Bitmap The bitmap to read the image from.
Результат void
Пример #1
0
    public void Test_RemoveAlpha()
    {
      string tempFile = Path.GetTempPath() + Guid.NewGuid().ToString() + ".png";
      try
      {
        using (MagickImage image = new MagickImage(Files.MagickNETIconPNG))
        {
          Assert.IsTrue(image.HasAlpha);
          image.ColorAlpha(new MagickColor("yellow"));
          image.HasAlpha = true;
          image.Write(tempFile);

          image.Read(tempFile);

          // TODO: Figure out why this does not fail in a single run but does when all tests are run.
          //Assert.IsTrue(image.HasAlpha);

          PngOptimizer optimizer = new PngOptimizer();
          optimizer.LosslessCompress(tempFile);

          image.Read(tempFile);
          Assert.IsFalse(image.HasAlpha);
        }
      }
      finally
      {
        if (File.Exists(tempFile))
          File.Delete(tempFile);
      }
    }
Пример #2
0
    public void Test_RemoveAlpha()
    {
      string tempFile = GetTemporaryFileName(".png");

      try
      {
        using (MagickImage image = new MagickImage(Files.MagickNETIconPNG))
        {
          Assert.IsTrue(image.HasAlpha);
          image.ColorAlpha(new MagickColor("yellow"));
          image.HasAlpha = true;
          image.Write(tempFile);

          image.Read(tempFile);

          Assert.IsTrue(image.HasAlpha);

          PngOptimizer optimizer = new PngOptimizer();
          optimizer.LosslessCompress(tempFile);

          image.Read(tempFile);
          Assert.IsFalse(image.HasAlpha);
        }
      }
      finally
      {
        File.Delete(tempFile);
      }
    }
Пример #3
0
    public void Test_Disabled()
    {
      using (MemoryStream memStream = new MemoryStream())
      {
        using (StreamWriter writer = new StreamWriter(memStream))
        {
          writer.Write(@"push graphic-context
              viewbox 0 0 640 480
              image over 0,0 0,0 ""label:Magick.NET""
              pop graphic-context");

          writer.Flush();

          memStream.Position = 0;

          using (MagickImage image = new MagickImage())
          {
            ExceptionAssert.Throws<MagickMissingDelegateErrorException>(delegate ()
            {
              image.Read(memStream);
            });

            ExceptionAssert.Throws<MagickPolicyErrorException>(delegate ()
            {
              MagickReadSettings settings = new MagickReadSettings()
              {
                Format = MagickFormat.Mvg
              };

              image.Read(memStream, settings);
            });
          }
        }
      }
    }
    public void Test_Image_Read()
    {
      using (MagickImage image = new MagickImage())
      {
        MagickReadSettings settings = new MagickReadSettings();
        settings.Density = new PointD(300);

        image.Read(Files.SnakewarePNG, settings);

        Assert.AreEqual(300, image.Density.X);

        settings = null;
        image.Read(Files.ImageMagickJPG, settings);
      }

      using (MagickImage image = new MagickImage(Files.RoseSparkleGIF))
      {
        MagickImage imageA = new MagickImage();
        MagickImage imageB = new MagickImage();

        MagickReadSettings settings = new MagickReadSettings();

        imageA.Read(Files.RoseSparkleGIF, settings);
        Assert.AreEqual(image, imageA);

        settings = new MagickReadSettings();
        settings.FrameIndex = 1;

        imageA.Read(Files.RoseSparkleGIF, settings);
        Assert.AreNotEqual(image, imageA);

        imageB.Read(Files.RoseSparkleGIF + "[1]");
        Assert.AreEqual(imageA, imageB);

        settings = new MagickReadSettings();
        settings.FrameIndex = 2;

        imageA.Read(Files.RoseSparkleGIF, settings);
        Assert.AreNotEqual(image, imageA);

        imageB.Read(Files.RoseSparkleGIF + "[2]");
        Assert.AreEqual(imageA, imageB);

        ExceptionAssert.Throws<MagickOptionErrorException>(delegate ()
        {
          settings = new MagickReadSettings();
          settings.FrameIndex = 3;

          imageA.Read(Files.RoseSparkleGIF, settings);
        });

        imageA.Dispose();
        imageB.Dispose();
      }
    }
Пример #5
0
    public void Test_Dimensions()
    {
      using (MagickImage image = new MagickImage())
      {
        MagickReadSettings settings = new MagickReadSettings();
        settings.Width = 100;

        image.Read(Files.Logos.MagickNETSVG, settings);

        Assert.AreEqual(100, image.Width);
        Assert.AreEqual(48, image.Height);

        settings.Width = null;
        settings.Height = 200;

        image.Read(Files.Logos.MagickNETSVG, settings);

        Assert.AreEqual(415, image.Width);
        Assert.AreEqual(200, image.Height);

        settings.Width = 300;
        settings.Height = 300;

        image.Read(Files.Logos.MagickNETSVG, settings);

        Assert.AreEqual(300, image.Width);
        Assert.AreEqual(144, image.Height);

        image.Ping(Files.Logos.MagickNETSVG, settings);

        Assert.AreEqual(300, image.Width);
        Assert.AreEqual(144, image.Height);

        using (FileStream fs = File.OpenRead(Files.Logos.MagickNETSVG))
        {
          fs.Seek(55, SeekOrigin.Begin);

          ExceptionAssert.Throws<MagickMissingDelegateErrorException>(() =>
          {
            new MagickImageInfo(fs, settings);
          });

          fs.Seek(55, SeekOrigin.Begin);

          settings.Format = MagickFormat.Svg;

          MagickImageInfo info = new MagickImageInfo(fs, settings);
          Assert.AreEqual(300, info.Width);
          Assert.AreEqual(144, info.Height);
        }
      }
    }
Пример #6
0
    public void Test_ColorSpace()
    {
      using (MagickImage image = new MagickImage())
      {
        Assert.AreEqual(ColorSpace.Undefined, image.Settings.ColorSpace);

        image.Read(Files.ImageMagickJPG);
        ColorAssert.AreEqual(MagickColors.White, image, 0, 0);

        image.Settings.ColorSpace = ColorSpace.Rec601YCbCr;
        image.Read(Files.ImageMagickJPG);
        ColorAssert.AreEqual(new MagickColor("#ff8080"), image, 0, 0);
      }
    }
Пример #7
0
    public void Test_BackgroundColor()
    {
      using (MagickImage image = new MagickImage())
      {
        ColorAssert.AreEqual(MagickColors.White, image.Settings.BackgroundColor);

        image.Read(Files.Logos.MagickNETSVG);
        ColorAssert.AreEqual(MagickColors.White, image, 0, 0);

        image.Settings.BackgroundColor = MagickColors.Yellow;
        image.Read(Files.Logos.MagickNETSVG);
        ColorAssert.AreEqual(MagickColors.Yellow, image, 0, 0);
      }
    }
        public void WhenCompareItToTheScreenshot(string screenshotName)
        {
            using (var leftImage = new MagickImage(new FileInfo(this.screenshotFileName)))
            {
                using (var rightImage = new MagickImage())
                {
                    using (var extractResource = EmbeddedResources.ExtractResource(Assembly.GetExecutingAssembly().GetName().Name, "Screenshots", screenshotName))
                    {
                        rightImage.Read(extractResource);
                    }

                    MagickImage diffImage = null;

                    try
                    {
                        this.screenshotDistortion = this.screenShotService.Compare(leftImage, rightImage, out diffImage);
                    }
                    finally
                    {
                        if (diffImage != null)
                        {
                            var diffFileName = Path.GetFileNameWithoutExtension(this.screenshotFileName) + "_diff" + Path.GetExtension(this.screenshotFileName);
                            // ReSharper disable once AssignNullToNotNullAttribute
                            diffImage.Write(Path.Combine(Path.GetDirectoryName(this.screenshotFileName), diffFileName));
                            diffImage.Dispose();
                        }
                    }
                }
            }
        }
Пример #9
0
    public void Test_AlphaUnblend()
    {
      MagickReadSettings settings = new MagickReadSettings()
      {
        Defines = new PsdReadDefines()
        {
          AlphaUnblend = false
        }
      };

      using (MagickImage image = new MagickImage())
      {
        image.Read(Files.Coders.PlayerPSD, settings);

        string define = image.Settings.GetDefine(MagickFormat.Psd, "alpha-unblend");
        Assert.AreEqual("False", define);
      }

      using (MagickImage image = new MagickImage())
      {
        settings.Defines = new PsdReadDefines()
        {
          AlphaUnblend = true
        };

        image.Read(Files.Coders.PlayerPSD, settings);

        string define = image.Settings.GetDefine(MagickFormat.Psd, "alpha-unblend");
        Assert.IsNull(define);
      }
    }
Пример #10
0
    public void Test_Disabled()
    {
      using (MemoryStream memStream = new MemoryStream())
      {
        using (StreamWriter writer = new StreamWriter(memStream))
        {
          writer.Write(@"
            <?xml version=""1.0"" encoding=""UTF-8""?>
            <image>
              <read filename=""/tmp/text.gif"" />
            </image>");

          writer.Flush();

          memStream.Position = 0;

          using (MagickImage image = new MagickImage())
          {
            MagickReadSettings readSettings = new MagickReadSettings()
            {
              Format = MagickFormat.Msl
            };

            ExceptionAssert.Throws<MagickPolicyErrorException>(delegate ()
            {
              image.Read(memStream, readSettings);
            });
          }
        }
      }
    }
Пример #11
0
 public void Test_IgnoreTags()
 {
   using (MagickImage image = new MagickImage())
   {
     image.SetDefine(MagickFormat.Tiff, "ignore-tags", "32934");
     image.Read(Files.Coders.IgnoreTagTIF);
   }
 }
Пример #12
0
    public void Test_IgnoreExifPoperties()
    {
      MagickReadSettings settings = new MagickReadSettings()
      {
        Defines = new TiffReadDefines()
        {
          IgnoreExifPoperties = true
        }
      };

      using (MagickImage image = new MagickImage())
      {
        image.Read(Files.InvitationTif);
        Assert.IsNotNull(image.GetAttribute("exif:PixelXDimension"));

        image.Read(Files.InvitationTif, settings);
        Assert.IsNull(image.GetAttribute("exif:PixelXDimension"));
      }
    }
Пример #13
0
 public static void ObtainWarningThatOccurredDuringRead()
 {
   using (MagickImage image = new MagickImage())
   {
     // Attach event handler to warning event
     image.Warning += MagickImage_Warning;
     // Read file that will raise a warning.
     image.Read(SampleFiles.FileWithWarningJpg);
   }
 }
Пример #14
0
    public void Test_SkipProfiles()
    {
      MagickReadSettings settings = new MagickReadSettings()
      {
        Defines = new JpegReadDefines()
        {
          SkipProfiles = ProfileTypes.Iptc | ProfileTypes.Icc
        }
      };

      using (MagickImage image = new MagickImage())
      {
        image.Read(Files.FujiFilmFinePixS1ProJPG);
        Assert.IsNotNull(image.GetIptcProfile());

        image.Read(Files.FujiFilmFinePixS1ProJPG, settings);
        Assert.IsNull(image.GetIptcProfile());
        Assert.AreEqual("Icc,Iptc", image.Settings.GetDefine("profile:skip"));
      }
    }
Пример #15
0
    public static void ReadImage()
    {
      // Read from file.
      using (MagickImage image = new MagickImage(SampleFiles.SnakewareJpg))
      {
      }

      // Read from stream.
      using (MemoryStream memStream = LoadMemoryStreamImage())
      {
        using (MagickImage image = new MagickImage(memStream))
        {
        }
      }

      // Read from byte array.
      byte[] data = LoadImageBytes();
      using (MagickImage image = new MagickImage(data))
      {
      }

      // Read image that has no predefined dimensions.
      MagickReadSettings settings = new MagickReadSettings();
      settings.Width = 800;
      settings.Height = 600;
      using (MagickImage image = new MagickImage("xc:yellow", settings))
      {
      }

      using (MagickImage image = new MagickImage())
      {
        image.Read(SampleFiles.SnakewareJpg);
        image.Read(data);
        image.Read("xc:yellow", settings);

        using (MemoryStream memStream = LoadMemoryStreamImage())
        {
          image.Read(memStream);
        }
      }
    }
    public static void ReadImage()
    {
      // Log all events
      ImageMagick.MagickNET.SetLogEvents(LogEvents.All | LogEvents.Trace);
      // Set the log handler (all threads use the same handler)
      ImageMagick.MagickNET.Log += MagickNET_Log;

      using (MagickImage image = new MagickImage())
      {
        // Reading the image will send all log events to the log handler
        image.Read(SampleFiles.SnakewarePng);
      }
    }
Пример #17
0
    public void Test_SkipProfiles()
    {
      MagickReadSettings settings = new MagickReadSettings()
      {
        Defines = new PngReadDefines()
        {
          SkipProfiles = ProfileTypes.Xmp | ProfileTypes.Exif
        }
      };

      using (MagickImage image = new MagickImage())
      {
        image.Read(Files.FujiFilmFinePixS1ProPNG);
        Assert.IsNotNull(image.GetExifProfile());
        Assert.IsNotNull(image.GetXmpProfile());

        image.Read(Files.FujiFilmFinePixS1ProPNG, settings);
        Assert.IsNull(image.GetExifProfile());
        Assert.IsNull(image.GetXmpProfile());
        Assert.AreEqual("Exif,Xmp", image.Settings.GetDefine("profile:skip"));
      }
    }
Пример #18
0
    public void Test_PreserveCorruptImage()
    {
      MagickReadSettings settings = new MagickReadSettings()
      {
        Defines = new PngReadDefines()
        {
          PreserveCorruptImage = false
        }
      };

      MagickImage image = new MagickImage();

      try
      {
        image.Read(Files.CorruptPNG, settings);
      }
      catch (MagickCoderErrorException)
      {
        Assert.AreEqual(0, image.Width);
        Assert.AreEqual(0, image.Height);
      }

      ((PngReadDefines)settings.Defines).PreserveCorruptImage = true;

      try
      {
        image.Read(Files.CorruptPNG, settings);
      }
      catch (MagickCoderErrorException)
      {
        Assert.AreEqual(1920, image.Width);
        Assert.AreEqual(1440, image.Height);
      }
      finally
      {
        image.Dispose();
      }
    }
Пример #19
0
 private void Drukuj(string text)
 {
     var img = new MagickImage("image.jpg");
     using (var imgText = new MagickImage())
     {
         imgText.FontPointsize = 24;
         imgText.BackgroundColor = new MagickColor(Color.White);
         imgText.FillColor = new MagickColor(Color.Black);
         imgText.AntiAlias = true;
         imgText.FontFamily = "Fake Receipt";
         imgText.Read("label:" + text);
         img.Composite(imgText, Gravity.West);
     }
     image.Source = img.ToBitmapSource();
 }
Пример #20
0
    public void Test_Image_Read_Density()
    {
      using (MagickImage image = new MagickImage())
      {
        MagickReadSettings settings = new MagickReadSettings();
        settings.Density = new Density(300);

        image.Read(Files.SnakewarePNG, settings);

        Assert.AreEqual(300, image.Density.X);

        settings = null;
        image.Read(Files.ImageMagickJPG, settings);
      }
    }
Пример #21
0
    public void Test_IgnoreTags()
    {
      using (MagickImage image = new MagickImage())
      {
        image.Settings.SetDefine(MagickFormat.Tiff, "ignore-tags", "32934");
        image.Read(Files.Coders.IgnoreTagTIF);
      }

      using (MagickImage image = new MagickImage())
      {
        MagickReadSettings readSettings = new MagickReadSettings(new TiffReadDefines()
        {
          IgnoreTags = new string[] { "32934" }
        });
        image.Read(Files.Coders.IgnoreTagTIF, readSettings);
      }
    }
Пример #22
0
    public void Test_FitPage()
    {
      MagickReadSettings settings = new MagickReadSettings()
      {
        Defines = new PdfReadDefines()
        {
          FitPage = new MagickGeometry(50, 40)
        }
      };

      using (MagickImage image = new MagickImage())
      {
        image.Read(Files.Coders.CartoonNetworkStudiosLogoAI, settings);

        Assert.IsTrue(image.Width <= 50);
        Assert.IsTrue(image.Height <= 40);
      }
    }
Пример #23
0
    public void Test_BlockSmoothing_DctMethod_FancyUpsampling()
    {
      MagickReadSettings settings = new MagickReadSettings()
      {
        Defines = new JpegReadDefines()
        {
          BlockSmoothing = true,
          DctMethod = DctMethod.Slow,
          FancyUpsampling = false
        }
      };

      using (MagickImage image = new MagickImage())
      {
        image.Read(Files.ImageMagickJPG, settings);

        Assert.AreEqual("True", image.GetDefine(MagickFormat.Jpeg, "block-smoothing"));
        Assert.AreEqual("Slow", image.GetDefine(MagickFormat.Jpeg, "dct-method"));
        Assert.AreEqual("False", image.GetDefine(MagickFormat.Jpeg, "fancy-upsampling"));
      }
    }
Пример #24
0
    public void Test_Colors_Size()
    {
      MagickReadSettings settings = new MagickReadSettings()
      {
        Defines = new JpegReadDefines()
        {
          Colors = 100,
          Size = new MagickGeometry(61, 59)
        }
      };

      using (MagickImage image = new MagickImage())
      {
        image.Read(Files.ImageMagickJPG, settings);

        Assert.IsTrue(image.TotalColors <= 100);
        Assert.AreEqual(100, image.TotalColors, 1);
        Assert.AreEqual(62, image.Width);
        Assert.AreEqual(59, image.Height);
      }
    }
Пример #25
0
    public void Test_Multithreading()
    {
      ReadDelegate action = delegate ()
      {
        using (MagickImage image = new MagickImage())
        {
          image.Read(Files.Coders.CartoonNetworkStudiosLogoAI);
        }
      };

      IAsyncResult[] results = new IAsyncResult[3];

      for (int i = 0; i < results.Length; ++i)
      {
        results[i] = action.BeginInvoke(null, null);
      }

      for (int i = 0; i < results.Length; ++i)
      {
        action.EndInvoke(results[i]);
      }
    }
Пример #26
0
    public static void ConvertCmykToRgb()
    {
      // Uses sRGB.icm, eps/pdf produce better result when you set this before loading.
      MagickReadSettings settings = new MagickReadSettings();
      settings.ColorSpace = ColorSpace.sRGB;

      // Create empty image
      using (MagickImage image = new MagickImage())
      {
        // Reads the eps image, the specified settings tell Ghostscript to create an sRGB image
        image.Read(SampleFiles.SnakewareEps, settings);
        // Save image as tiff
        image.Write(SampleFiles.OutputDirectory + "Snakeware.tiff");
      }

      // Read image from file
      using (MagickImage image = new MagickImage(SampleFiles.SnakewareJpg))
      {
        // First add a CMYK profile if your image does not contain a color profile.
        image.AddProfile(ColorProfile.USWebCoatedSWOP);

        // Adding the second profile will transform the colorspace from CMYK to RGB
        image.AddProfile(ColorProfile.SRGB);
        // Save image as png
        image.Write(SampleFiles.OutputDirectory + "Snakeware.png");
      }

      // Read image from file
      using (MagickImage image = new MagickImage(SampleFiles.SnakewareJpg))
      {
        // First add a CMYK profile if your image does not contain a color profile.
        image.AddProfile(ColorProfile.USWebCoatedSWOP);

        // Adding the second profile will transform the colorspace from your custom icc profile
        image.AddProfile(new ColorProfile(SampleFiles.YourProfileIcc));
        // Save image as tiff
        image.Write(SampleFiles.OutputDirectory + "Snakeware.tiff");
      }
    }
Пример #27
0
    public void Test_Image_Read_FrameIndex()
    {
      using (MagickImage image = new MagickImage(Files.RoseSparkleGIF))
      {
        MagickImage imageA = new MagickImage();
        MagickImage imageB = new MagickImage();

        MagickReadSettings settings = new MagickReadSettings();

        imageA.Read(Files.RoseSparkleGIF, settings);
        Assert.AreEqual(image, imageA);

        settings = new MagickReadSettings();
        settings.FrameIndex = 1;

        imageA.Read(Files.RoseSparkleGIF, settings);
        Assert.AreNotEqual(image, imageA);

        imageB.Read(Files.RoseSparkleGIF + "[1]");
        Assert.AreEqual(imageA, imageB);

        settings = new MagickReadSettings();
        settings.FrameIndex = 2;

        imageA.Read(Files.RoseSparkleGIF, settings);
        Assert.AreNotEqual(image, imageA);

        imageB.Read(Files.RoseSparkleGIF + "[2]");
        Assert.AreEqual(imageA, imageB);

        ExceptionAssert.Throws<MagickOptionErrorException>(delegate ()
        {
          settings = new MagickReadSettings();
          settings.FrameIndex = 3;

          imageA.Read(Files.RoseSparkleGIF, settings);
        });

        imageA.Dispose();
        imageB.Dispose();
      }
    }
Пример #28
0
    public void Test_Image_Read_Dimensions()
    {
      using (MagickImage image = new MagickImage())
      {
        MagickReadSettings settings = new MagickReadSettings();
        settings.Width = 10;

        image.Read("xc:fuchsia", settings);

        Assert.AreEqual(10, image.Width);
        Assert.AreEqual(1, image.Height);

        settings.Width = null;
        settings.Height = 20;

        image.Read("xc:fuchsia", settings);

        Assert.AreEqual(1, image.Width);
        Assert.AreEqual(20, image.Height);

        settings.Width = 30;
        settings.Height = 40;

        image.Read("xc:fuchsia", settings);

        Assert.AreEqual(30, image.Width);
        Assert.AreEqual(40, image.Height);
      }
    }
Пример #29
0
    public void Test_WithImage()
    {
      using (MagickImage image = new MagickImage())
      {
        image.AddProfile(ColorProfile.USWebCoatedSWOP);
        ExceptionAssert.Throws<MagickCacheErrorException>(delegate ()
        {
          image.ColorSpace = ColorSpace.CMYK;
        });
        image.Read(Files.SnakewarePNG);

        ColorProfile profile = image.GetColorProfile();
        Assert.IsNull(profile);

        image.AddProfile(ColorProfile.SRGB);
        TestProfile(image.GetColorProfile(), "icc");
      }
    }
    public void Test_Image_Read()
    {
      MagickReadSettings settings = CreateSettings();

      using (MagickImage image = new MagickImage())
      {
        byte[] data = new byte[]
          {
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0xf0, 0x3f,
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0xf0, 0x3f,
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0,
          };
        image.Read(data, settings);

        Assert.AreEqual(2, image.Width);
        Assert.AreEqual(1, image.Height);

        using (PixelCollection pixels = image.GetReadOnlyPixels())
        {
          Pixel pixel = pixels.GetPixel(0, 0);
          Assert.AreEqual(4, pixel.Channels);
          Assert.AreEqual(0, pixel.GetChannel(0));
          Assert.AreEqual(0, pixel.GetChannel(1));
          Assert.AreEqual(0, pixel.GetChannel(2));
          Assert.AreEqual(Quantum.Max, pixel.GetChannel(3));

          pixel = pixels.GetPixel(1, 0);
          Assert.AreEqual(4, pixel.Channels);
          Assert.AreEqual(0, pixel.GetChannel(0));
          Assert.AreEqual(Quantum.Max, pixel.GetChannel(1));
          Assert.AreEqual(0, pixel.GetChannel(2));
          Assert.AreEqual(0, pixel.GetChannel(3));

          ExceptionAssert.Throws<ArgumentException>(delegate ()
          {
            pixels.GetPixel(0, 1);
          });
        }
      }
    }