Exemplo n.º 1
0
        private async Task ConvertToAnimatedGifFileAsync(string targetDirectory, string outputPath, float frameRate)
        {
            // 画像ファイルを名前昇順で全取得
            var filePaths = Directory.GetFiles(targetDirectory)
                            .Where(name => name.EndsWith(".jpg") || name.EndsWith(".png"))
                            .OrderBy(name => name);

            await Task.Run(() =>
            {
                using (var collection = new ImageMagick.MagickImageCollection())
                {
                    foreach (var filePath in filePaths)
                    {
                        collection.Add(filePath);
                    }
                    foreach (var item in collection)
                    {
                        item.AnimationDelay = (int)(1000.0f / frameRate / 10.0f); // 10 ms 単位
                        // item.Resize(100, (int)(100.0f / item.Width * item.Height));
                    }
                    collection.Optimize();
                    collection.Write(outputPath);
                }
            }).ConfigureAwait(false);
        }
Exemplo n.º 2
0
        private static void FixedImageSize(AppDbContext context, IHostingEnvironment environment, IConfiguration configuration)
        {
            if (!bool.Parse(configuration["CompressImages"]))
            {
                return;
            }

            var products = context.Products.Include(p => p.ProductImages);

            foreach (var product in products)
            {
                var filesPath = Directory.GetFiles($"{environment.WebRootPath}/Product Images/{product.Id}");

                foreach (var path in filesPath)
                {
                    try
                    {
                        var imageStream      = new FileStream(path, FileMode.Open);
                        var magickCollection = new ImageMagick.MagickImageCollection(imageStream);
                        var fImage           = magickCollection.First();

                        //if (fImage == null)
                        //    continue;

                        if (imageStream.Length > 1000000L && imageStream.Length < 1500000L)
                        {
                            fImage.Quality = 80;
                            imageStream.Close();
                            fImage.Write(path);
                        }

                        if (imageStream.Length > 1500000L && imageStream.Length < 2000000L)
                        {
                            fImage.Quality = 70;
                            imageStream.Close();
                            fImage.Write(path);
                        }

                        if (imageStream.Length > 2000000L)
                        {
                            fImage.Quality = 60;
                            imageStream.Close();
                            fImage.Write(path);
                        }

                        imageStream.Close();
                    }
                    catch
                    {
                        continue;
                    }
                }
            }
        }
        private static void FileLoad(string fileName)
        {
            labelItems.Clear();
            using (var images = new ImageMagick.MagickImageCollection())
            {
                var settings = new ImageMagick.MagickReadSettings();
                settings.Density = new ImageMagick.Density(density, density);

                images.Read(fileName, settings);

                // split
                int page = 1;
                foreach (var image in images)
                {
                    int width  = (int)(image.Width / rowCount);
                    int height = (int)(image.Height / columnCount);

                    int number = 0;
                    foreach (var labelImage in image.CropToTiles(width, height))
                    {
                        number++;
                        if (labelImage.TotalColors == 1)
                        {
                            continue;
                        }

                        var encoder = new System.Windows.Media.Imaging.JpegBitmapEncoder();
                        encoder.QualityLevel = 100;

                        using (MemoryStream stream = new MemoryStream())
                        {
                            encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(labelImage.ToBitmapSource()));
                            encoder.Save(stream);
                            stream.Position = 0;

                            var label = DYMO.Label.Framework.Label.Open(
                                Application.GetResourceStream(
                                    new Uri("BarcodeAsImage.label", UriKind.RelativeOrAbsolute)).Stream);

                            label.SetImagePngData("Image", stream);

                            var labelItem = new LabelItem(labelImage, label);
                            AddLabelItem(page, number, labelItem);
                        }
                    }
                    page++;
                }
            }
        }
Exemplo n.º 4
0
        public async void Run()
        {
            //DateTime first = new DateTime(2017, 7, 9, 14, 50, 00);
            //DateTime last = new DateTime(2017, 7, 10, 14, 30, 00);
            //List<DateTime> dates = new List<DateTime>();
            //dates.Add(first);
            //do
            //{
            //    dates.Add(dates.Last().AddMinutes(10));
            //} while (dates.Last() < last && dates.Count < 160000000);

            //List<string> images = new List<string>();

            //foreach (var date in dates)
            //{
            //    images.Add(await sateliteService.DownloadImage(date, false));
            //    Console.Clear();
            //    Console.WriteLine(dates.IndexOf(date)/dates.Count);
            //    Console.WriteLine(dates.IndexOf(date));


            //}

            var images = System.IO.Directory.GetFiles(@"D:\Temp");

            using (ImageMagick.MagickImageCollection coll = new ImageMagick.MagickImageCollection())
            {
                try
                {
                    for (int i = 0; i < images.Length; i++)
                    {
                        coll.Add(new ImageMagick.MagickImage(images[i]));
                        coll[i].AnimationDelay = 5;
                    }
                }
                catch (Exception)
                {
                    throw;
                }

                //coll.OptimizePlus();

                coll.Write("D:\\earth2.gif");
            }
        }