예제 #1
0
        /// <summary>
        /// Creates an animated GIF and writes it to <paramref name="outputStream"/>.
        /// </summary>
        /// <param name="animation"></param>
        /// <param name="outputStream"></param>
        public void Create(ConstRawAnimation animation, Stream outputStream)
        {
            List <Image> rawFrames = new List <Image>();

            // Load each image in the animation.
            foreach (ConstAnimationFrame frame in animation.Frames)
            {
                NpkPath frameImagePath           = frame.Image.ImageFilePath;
                DFO.Common.Images.Image rawFrame = m_imageSource.GetImage(frameImagePath, frame.Image.FrameIndex);
                rawFrames.Add(rawFrame);
            }

            int smallestX;
            int largestX;
            int smallestY;
            int largestY;

            // Frames can have different start positions and widths/heights. Normalize the images to a common coordinate system.
            FrameInfo.GetNormalizedCoordinates(rawFrames.Select(image => image.Attributes), out smallestX, out largestX, out smallestY, out largestY);

            int normalizedWidth  = largestX - smallestX + 1;
            int normalizedHeight = largestY - smallestY + 1;

            List <MagickImage> renderedFrames = new List <MagickImage>();

            try
            {
                // Composite each frame on top of a canvas of normalized width and height.
                for (int frameIndex = 0; frameIndex < animation.Frames.Count; frameIndex++)
                {
                    Image rawFrameImage = rawFrames[frameIndex];
                    ConstAnimationFrame frameAnimationInfo = animation.Frames[frameIndex];

                    MagickImage renderedFrame = RenderFrame(rawFrameImage, frameAnimationInfo, smallestX, largestX, smallestY, largestY, normalizedWidth, normalizedHeight);
                    renderedFrames.Add(renderedFrame);
                }

                // Make the GIF from the frames and write it out to the stream.
                using (MagickImageCollection frameCollection = new GraphicsMagick.MagickImageCollection(renderedFrames))
                {
                    frameCollection.Write(outputStream, MagickFormat.Gif);
                }
            }
            finally
            {
                renderedFrames.ForEach(f => f.Dispose());
            }
        }
예제 #2
0
        /// <summary>
        /// Exports a frame as a PNG file.
        /// </summary>
        /// <param name="imageSource">Source of images. Normally an NPK reader, but could be also be a source that reads from
        /// an extraction or a mock source.</param>
        /// <param name="imgPath"></param>
        /// <param name="frameIndex"></param>
        /// <param name="outputStream">stream to write the PNG to</param>
        /// <exception cref="System.IO.FileNotFoundException">Image with the given path and frame index does not exist.</exception>
        public static void ToPng(IImageSource imageSource, NpkPath imgPath, int frameIndex, Stream outputStream)
        {
            Image image = imageSource.GetImage(imgPath, frameIndex);

            MagickReadSettings pixelDataSettings = new MagickReadSettings()
            {
                ColorSpace   = ColorSpace.RGB,
                Width        = image.Attributes.Width,
                Height       = image.Attributes.Height,
                PixelStorage = new PixelStorageSettings(StorageType.Char, "RGBA")
            };

            using (MagickImage magickImage = new MagickImage(image.PixelData, pixelDataSettings))
            {
                magickImage.Write(outputStream, MagickFormat.Png);
            }
        }
 public Image GetImage(int minX, int minY)
 {
     if (imageSources == null || imageSources.Count == 0)
     {
         return(fallbackImageSource.GetImage(minX, minY));
     }
     try {
         Image image = imageSources[index].GetImage(minX, minY);
         index = ++index % imageSources.Count;
         return(image);
     }
     catch (ImageSourceFailedException ex) {
         Log.Instance.Write("Image source failure", ex);
         imageSources.RemoveAt(index);
         index = 0;
         return(GetImage(minX, minY));
     }
 }
예제 #4
0
 private void element_ImageProduced(IImageSource source)
 {
     if ((selectedSourceIndex < 0) || (sources[selectedSourceIndex] != source))
         return;
     Bitmap image = source.GetImage(0);
     if(image != null)
         capturedImages.Produce(image);
 }