vaspo wrote in nullzone

Concatenate Images

Что-то сходу онлайн сервиса не нашёл, т.ч. вот примерчик консольного приложения, которое по маске ищет ряд файлов и объединяет их в одну большую картинку (draft).

http://svn2.assembla.com/svn/DevSamples/ImageConcat (Binary and Sources)

Если кто подскажет, как теперь это в Silverlight приложение переделать, будет вообще замечательно. Хотелось бы использовать по http протоколу всё таки. Тем более, что в 4ой версии silverlight точно можно сделать multiupload’er.

Исходный код под катом.
 

Code:

 private static void Main(string[] args)
        {
            Console.WriteLine("Parsing command line arguments...");
            //TODO: Validate
            var path = args[0];
            string destination = Path.Combine(Path.GetDirectoryName(path), "output.jpg");
            Console.WriteLine("Destination: {0}", destination);
            Console.WriteLine("Scanning files...");
            var files = Directory.GetFiles(Path.GetDirectoryName(path), Path.GetFileName(path)).Where(x=>x != destination).ToList();
            //TODO: Sort
            Console.WriteLine("Loading {0} images...", files.Count);
            var images = files.Select(file => new BitmapImage(new Uri(file, UriKind.Absolute))).ToList();
            var drawingVisual = new DrawingVisual();
            var drawingContext = drawingVisual.RenderOpen();
            double maxWidth = 0;
            double maxHeight = 0;
            double height = 0;
            for (int index = 0; index < images.Count; index++)
            {
                var image = images[index];
                Console.WriteLine("Processing {0}/{1}...", index+1, images.Count);
                maxWidth = Math.Max(maxWidth, image.Width);
                maxHeight = Math.Max(maxHeight, image.Height);
                drawingContext.DrawImage(image, new Rect(0, height, image.Width, image.Height));
                height += image.Height;
            }
            drawingContext.Close();

            var bitmap = new RenderTargetBitmap((int) maxWidth, (int) height, 96d, 96d, PixelFormats.Pbgra32);
            bitmap.Render(drawingVisual);

            if (bitmap.CanFreeze)
            {
                bitmap.Freeze();
            }

            var frame = BitmapFrame.Create(bitmap);
            //TODO: Detect image format
            var encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(frame);

            if (File.Exists(destination))
            {
                File.Delete(destination);
            }
            using (var fs = new FileStream(destination, FileMode.Create))
            {
                encoder.Save(fs);
                fs.Flush();
            }

            Console.WriteLine("Done.");
            Console.ReadKey();
        }