Exemplo n.º 1
2
        public async Task<ValidationResult> Execute(MediaInfo mediaInfo, Int32 x, Int32 y, Int32 width, Int32 height)
        {
            if (x < 0 || y < 0 || width <= 0 || height <= 0)
            {
                return new ValidationResult("サイズの指定が不正です。");
            }

            var mediaContent = await _mediaRepository.Get(mediaInfo);
            if (mediaContent == null)
            {
                return new ValidationResult("指定されたメディアが見つかりませんでした。");
            }

            var data = await mediaContent.GetContentAsync();
            var imageInfo = new MagickImageInfo(data);
            if (imageInfo.Width < (x + width) || imageInfo.Height < (y + height))
            {
                return new ValidationResult("指定されたサイズは画像のサイズを超えています。");
            }

            // リサイズするよ!
            using (var image = new MagickImage(data))
            {
                image.Crop(new MagickGeometry(x, y, width, height));
                image.Page = new MagickGeometry(0, 0, width, height);
                
                // そして更新
                await _mediaRepository.Update(mediaInfo, image.ToByteArray());
            }

            return ValidationResult.Success;
        }
Exemplo n.º 2
1
        public string Execute(FileItem item, string infile, string dest, ValuePairEnumerator configData)
        {
            var conf = new CropTransformViewModel(configData);
            using (MagickImage image = new MagickImage(infile))
            {
                if (conf.FromLiveView && ServiceProvider.DeviceManager.SelectedCameraDevice != null)
                {
                    var prop = ServiceProvider.DeviceManager.SelectedCameraDevice.LoadProperties();
                    conf.Left = (int) (image.Width*prop.LiveviewSettings.HorizontalMin/100);
                    conf.Width = (image.Width*
                                  (prop.LiveviewSettings.HorizontalMax - prop.LiveviewSettings.HorizontalMin)/100);
                    conf.Top = (image.Height*prop.LiveviewSettings.VerticalMin/100);
                    conf.Height = (image.Height*(prop.LiveviewSettings.VerticalMax - prop.LiveviewSettings.VerticalMin)/
                                   100);
                }
                if (conf.CropMargins)
                {
                    conf.Left = image.Width * conf.WidthProcent / 100;
                    conf.Width = image.Width - (conf.Left*2);
                    conf.Top = image.Height * conf.HeightProcent / 100;
                    conf.Height = image.Height - (conf.Top*2);
                }

                MagickGeometry geometry = new MagickGeometry();
                geometry.Width = conf.Width;
                geometry.Height = conf.Height;
                geometry.X = conf.Left;
                geometry.Y = conf.Top;
                image.Crop(geometry);
                image.Format = MagickFormat.Jpeg;
                image.Write(dest);
            }
            return dest;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns the path to the sketch image just created
        /// <param name="mask">Mask image of the button</param>
        /// <param name="texture">Texture image of the item</param>
        /// <param name="nameSketch">Name of the image to create</param>
        public static string CreateSketchesPath(string mask, string texture, string nameSketch)
        {
            MagickImage Mask = new MagickImage(mask);

            MagickImage Texture = new MagickImage(texture);

            Texture.Crop(Mask.Width, Mask.Height);

            Texture.Composite(Mask, CompositeOperator.CopyAlpha);
            Mask.Composite(Texture, CompositeOperator.Multiply);
            MagickImage sketch = Mask;

            try
            {
                // sketch.Write(Helpers.ResourcesHelper.SketchesPath() + nameSketch);
                string p = Helpers.ResourcesHelper.SketchesPath() + nameSketch;
                System.IO.Stream s = new System.IO.FileStream(p, System.IO.FileMode.Create);

                sketch.Write(s);
                s.Close();
            }
            catch (MagickException ex)
            {
                string s= ex.Message;
            }
            catch
            {

            }
            sketch.Dispose();
            sketch = null;
            string path = Helpers.ResourcesHelper.SketchesPath() + nameSketch;
            return path;
        }
Exemplo n.º 4
0
 public static void crop(string image_path, string output_path, Tuple<int, int> from, Tuple<int, int> to)
 {
     using (MagickImage image = new MagickImage(image_path)) {
         image.Crop(new MagickGeometry(from.Item1, from.Item2, to.Item1 - from.Item1, to.Item2 - from.Item2));
         image.Write(output_path);
     }
 }
        public double Compare(MagickImage leftImage, MagickImage rightImage, out MagickImage diffImage)
        {
            // See:
            // [1] https://stackoverflow.com/questions/5132749/diff-an-image-using-imagemagick
            // [2] https://stackoverflow.com/questions/20582620/how-to-compare-2-images-ignoring-areas#26584462

            // resize image to the same size - ImageMagick requires both images to be of the same size
            rightImage.Crop(leftImage.BaseWidth, leftImage.BaseHeight);

            // apply the mask to the screenshot
            rightImage.Composite(leftImage, CompositeOperator.DstIn);

            // compare masked screenshot against reference image
            diffImage = new MagickImage();
            return rightImage.Compare(leftImage, ErrorMetric.Absolute, diffImage);
        }
Exemplo n.º 6
0
        public string Execute(FileItem item, string infile, string dest, ValuePairEnumerator configData)
        {
            var conf = new PixelBinningViewModel(configData);
            dest = Path.Combine(Path.GetDirectoryName(dest), Path.GetFileNameWithoutExtension(dest) + ".jpg");

            using (MagickImage image = new MagickImage(infile))
            {
                int newx = image.Width/(conf.SelectedMode + 2);
                int newy = image.Height / (conf.SelectedMode + 2);
                int cropx = newx * (conf.SelectedMode + 2);
                int cropy = newy * (conf.SelectedMode + 2);
                if (cropx != image.Width || cropy != image.Height)
                    image.Crop(cropx, cropy, Gravity.Center);
                image.FilterType = FilterType.Box;
                image.Resize(newx,newy);
                image.Format = MagickFormat.Jpeg;
                image.Write(dest);
            }
            return dest;
        }
Exemplo n.º 7
0
        public void CopyFile(string filename, string destFile)
        {
            using (MagickImage image = new MagickImage(filename))
            {
                double zw = (double)Width / image.Width;
                double zh = (double)Height /image.Height;
                double za = FillImage ? ((zw <= zh) ? zw : zh) : ((zw >= zh) ? zw : zh);

                if (FillImage)
                {
                    double aspect = (double) VideoType.Width/VideoType.Height;
                    double pAspect = (double) image.Width/image.Height;
                    if (aspect > pAspect)
                        image.Crop(image.Width, (int) (image.Width/aspect), Gravity.Center);
                    else
                        image.Crop((int) (image.Height/aspect), image.Height, Gravity.Center);
                }

                MagickGeometry geometry = new MagickGeometry(VideoType.Width, VideoType.Height)
                {
                    IgnoreAspectRatio = false,
                    FillArea = false
                };

                image.FilterType = FilterType.Point;
                image.Resize(geometry);
                image.Quality = 80;
                image.Format = MagickFormat.Jpeg;
                image.Write(destFile);
            }
        }
Exemplo n.º 8
0
 private void ExecuteCrop(XmlElement element, MagickImage image)
 {
   Hashtable arguments = new Hashtable();
   foreach (XmlAttribute attribute in element.Attributes)
   {
     if (attribute.Name == "geometry")
       arguments["geometry"] = Variables.GetValue<MagickGeometry>(attribute);
     else if (attribute.Name == "gravity")
       arguments["gravity"] = Variables.GetValue<Gravity>(attribute);
     else if (attribute.Name == "height")
       arguments["height"] = Variables.GetValue<Int32>(attribute);
     else if (attribute.Name == "width")
       arguments["width"] = Variables.GetValue<Int32>(attribute);
     else if (attribute.Name == "x")
       arguments["x"] = Variables.GetValue<Int32>(attribute);
     else if (attribute.Name == "y")
       arguments["y"] = Variables.GetValue<Int32>(attribute);
   }
   if (OnlyContains(arguments, "geometry"))
     image.Crop((MagickGeometry)arguments["geometry"]);
   else if (OnlyContains(arguments, "width", "height"))
     image.Crop((Int32)arguments["width"], (Int32)arguments["height"]);
   else if (OnlyContains(arguments, "width", "height", "gravity"))
     image.Crop((Int32)arguments["width"], (Int32)arguments["height"], (Gravity)arguments["gravity"]);
   else if (OnlyContains(arguments, "x", "y", "width", "height"))
     image.Crop((Int32)arguments["x"], (Int32)arguments["y"], (Int32)arguments["width"], (Int32)arguments["height"]);
   else
     throw new ArgumentException("Invalid argument combination for 'crop', allowed combinations are: [geometry] [width, height] [width, height, gravity] [x, y, width, height]");
 }
Exemplo n.º 9
0
        public static List<PartOfImage> PartOfImage(string ImgUrl, string onlyFileName, int pxFormat)
        {
            List<PartOfImage> ImgsInfo = new List<Api._QPR.PartOfImage>();
            //resize with pixformat
            MagickImage img = new MagickImage(ImgUrl);
            int _w = img.Width;
            int _h = img.Height;

            int partWComp = (_w / UserProperty.ComputerNumber);
            int partHComp = (_h / UserProperty.ComputerNumber);

            int PartWPixFormat = (partWComp / pxFormat);
            int PartHPixFormat = (partHComp / pxFormat);

            int newPartWcomp = PartWPixFormat * pxFormat;
            int newPartHcomp = PartHPixFormat * pxFormat;

            int newW = newPartWcomp * UserProperty.ComputerNumber;
            int newH = newPartHcomp * UserProperty.ComputerNumber;

            String newGeomStr = newW.ToString() + "x" + newH.ToString();
            MagickGeometry intermediate_geo = new MagickGeometry(newGeomStr);
            img.Crop(intermediate_geo);

            string partImgFolder = Path.Combine(UserProperty.ResourcePhotos_Path, "ImagePart");
            if (!Directory.Exists(partImgFolder))
            {
                Directory.CreateDirectory(partImgFolder);
            }
            if (_w >= _h)
            {
                int cont = 0;
                do
                {
                    Rectangle recti = new Rectangle(cont * (newW / UserProperty.ComputerNumber), 0, (newW / UserProperty.ComputerNumber), newH);
                    MagickGeometry intermediate_geo1 = new MagickGeometry(recti);
                    MagickImage newPartImg = img.Clone();
                    newPartImg.Crop(intermediate_geo1);
                    string file = Path.Combine(partImgFolder, "output_" + cont + ImageProperty.JPG);
                    newPartImg.Write(file);
                    ImgsInfo.Add(new Api._QPR.PartOfImage(file, recti));
                    cont++;
                } while (cont < UserProperty.ComputerNumber);
            }
            else
            {
                int cont = 0;
                do
                {
                    Rectangle recti = new Rectangle(0, cont * (newH / UserProperty.ComputerNumber), newW, (newH / UserProperty.ComputerNumber));
                    MagickGeometry intermediate_geo1 = new MagickGeometry(recti);
                    MagickImage newPartImg = img.Clone();
                    newPartImg.Crop(intermediate_geo1);
                    string file = Path.Combine(partImgFolder, onlyFileName + "_" + cont + ImageProperty.JPG);
                    newPartImg.Write(file);
                    ImgsInfo.Add(new Api._QPR.PartOfImage(file, recti));
                    cont++;
                } while (cont < UserProperty.ComputerNumber);
            }
            return ImgsInfo;
            //img.Write(UserProperty.ResourcePhotos_Path + "\\" + imgNamei + "1" + ImageProperty.JPG);
            //////////////////////////////////////////
        }
Exemplo n.º 10
0
        public static void ApplyPreviewImageMagick(string source, string destination, double maxWidth = 0d, double maxHeight = 0d) {
            using (var image = new MagickImage(source)) {
                if (maxWidth > 0d || maxHeight > 0d) {
                    var k = Math.Max(maxHeight / image.Height, maxWidth / image.Width);
                    image.Interpolate = PixelInterpolateMethod.Bicubic;
                    image.FilterType = FilterType.Lanczos;
                    image.Sharpen();
                    image.Resize((int)(k * image.Width), (int)(k * image.Height));
                    image.Crop(CommonAcConsts.PreviewWidth, CommonAcConsts.PreviewHeight, Gravity.Center);
                }

                image.Quality = 95;
                image.Density = new MagickGeometry(96, 96);
                if (File.Exists(destination)) {
                    try {
                        File.Delete(destination);
                    } catch (UnauthorizedAccessException) {
                        Thread.Sleep(200);
                        File.Delete(destination);
                    }
                }

                image.Write(destination);
            }
        }
Exemplo n.º 11
0
    public void Test_CannyEdge_HoughLine()
    {
      using (MagickImage image = new MagickImage(Files.ConnectedComponentsPNG))
      {
        image.Threshold(new Percentage(50));

        ColorAssert.AreEqual(MagickColors.Black, image, 150, 365);
        image.Negate();
        ColorAssert.AreEqual(MagickColors.White, image, 150, 365);

        image.CannyEdge();
        ColorAssert.AreEqual(MagickColors.Black, image, 150, 365);

        image.Crop(260, 180, 215, 200);

        image.Settings.FillColor = MagickColors.Red;
        image.Settings.StrokeColor = MagickColors.Red;

        image.HoughLine();
        ColorAssert.AreEqual(MagickColors.Red, image, 105, 25);
      }
    }
Exemplo n.º 12
0
 protected MagickImage takeSnapshot(IWebElement webElement, int offset)
 {
     try
     {
         Screenshot image = ((ITakesScreenshot)pageManager.driver).GetScreenshot();
         MagickImage MImage = new MagickImage(image.AsByteArray);
         Point coords = (webElement as ILocatable).LocationOnScreenOnceScrolledIntoView;
         // MagickGeometry n = new MagickGeometry(webElement.Location.X - offset, webElement.Location.Y - offset, webElement.Size.Width + 2 * offset, webElement.Size.Height + 2 * offset);
         MagickGeometry n = new MagickGeometry(coords.X - offset, coords.Y - offset, webElement.Size.Width + 2 * offset, webElement.Size.Height + 2 * offset);
         MImage.Crop(n);
         return MImage;
     }
     catch
     {
         Console.Out.WriteLine("Ошибка при снятии скриншота");
         return null;
     }
 }