public override void RenderStep(MazeGenerationResults results) { Polygons = new List <ColoredPolygon>(); base.RenderStep(results); var allPoints = Polygons.SelectMany(x => x.Points).ToArray(); var commonPolygon = new Polygon(allPoints); var boundingRectangle = commonPolygon.GetBoundingRectangle(); var bmp = new Bitmap(boundingRectangle.Width, boundingRectangle.Height); var graphics = Graphics.FromImage(bmp); foreach (var polygon in Polygons) { var adjustedPoints = polygon.Points.Select(x => new System.Drawing.Point(x.X - boundingRectangle.X, x.Y - boundingRectangle.Y)).ToArray(); graphics.FillPolygon(Brushes[polygon.Color], adjustedPoints); } GifImage.AddFrame(bmp, FrameDelay, boundingRectangle.X, boundingRectangle.Y); if (results.ResultsType == GenerationResultsType.GenerationCompleted) { GifImage.Complete(); GifImage.OutStream.Position = 0; using (var fs = new FileStream(FilePath, FileMode.Create)) { GifImage.OutStream.CopyTo(fs); } } }
public BRGifMapRenderer(IMap map, Point targetSize, string filePath, int frameDelay = 10) : base(map, targetSize) { FilePath = filePath; FrameDelay = frameDelay; var ms = new MemoryStream(); GifImage = new GifImage(ms); GifImage.DefaultFrameDelay = FrameDelay; GifImage.DefaultWidth = targetSize[0]; GifImage.DefaultHeight = targetSize[1]; Brushes = new Dictionary <Color, Brush>(); foreach (var color in Colors) { Brushes.Add(color.Value, new SolidBrush(color.Value)); } var initialImage = new Bitmap(targetSize[0], targetSize[1]); //var initialGraphics = Graphics.FromImage(initialImage); //initialGraphics.FillRectangle(new SolidBrush(Color.Red), new Rectangle(0,0,initialImage.Width, initialImage.Height)); GifImage.AddFrame(initialImage, FrameDelay); }
private void ReadFrameColors(byte[] indices, byte[] colorTable, GifImageDescriptor descriptor) { int imageWidth = _logicalScreenDescriptor.Width; int imageHeight = _logicalScreenDescriptor.Height; if (_currentFrame == null) { _currentFrame = new byte[imageWidth * imageHeight * 4]; } byte[] lastFrame = null; if (_graphicsControl != null && _graphicsControl.DisposalMethod == DisposalMethod.RestoreToPrevious) { lastFrame = new byte[imageWidth * imageHeight * 4]; Array.Copy(_currentFrame, lastFrame, lastFrame.Length); } int offset = 0, i = 0, index = -1; int iPass = 0; // the interlace pass int iInc = 8; // the interlacing line increment int iY = 0; // the current interlaced line int writeY = 0; // the target y offset to write to for (int y = descriptor.Top; y < descriptor.Top + descriptor.Height; y++) { // Check if this image is interlaced. if (descriptor.InterlaceFlag) { // If so then we read lines at predetermined offsets. // When an entire image height worth of offset lines has been read we consider this a pass. // With each pass the number of offset lines changes and the starting line changes. if (iY >= descriptor.Height) { iPass++; switch (iPass) { case 1: iY = 4; break; case 2: iY = 2; iInc = 4; break; case 3: iY = 1; iInc = 2; break; } } writeY = iY + descriptor.Top; iY += iInc; } else { writeY = y; } for (int x = descriptor.Left; x < descriptor.Left + descriptor.Width; x++) { offset = writeY * imageWidth + x; index = indices[i]; if (_graphicsControl == null || _graphicsControl.TransparencyFlag == false || _graphicsControl.TransparencyIndex != index) { _currentFrame[offset * 4 + 0] = colorTable[index * 3 + 0]; _currentFrame[offset * 4 + 1] = colorTable[index * 3 + 1]; _currentFrame[offset * 4 + 2] = colorTable[index * 3 + 2]; _currentFrame[offset * 4 + 3] = (byte)255; } i++; } } byte[] pixels = new byte[imageWidth * imageHeight * 4]; Array.Copy(_currentFrame, pixels, pixels.Length); _currentFrame = new byte[imageWidth * imageHeight * 4]; GifFrame frame = new GifFrame(imageWidth, imageHeight); int indx = 0; byte r, g, b, a; for (uint y = 0; y < frame.height; y++) { for (uint x = 0; x < frame.width; x++) { r = pixels[indx]; indx++; g = pixels[indx]; indx++; b = pixels[indx]; indx++; a = pixels[indx]; indx++; frame.SetPixelEx(x, y, new Color32(r, g, b, a)); } } pixels = null; System.GC.Collect(); _image.AddFrame(frame); if (_graphicsControl != null) { if (_graphicsControl.DelayTime > 0) { _image.timePerFrame = _graphicsControl.DelayTime; } if (_graphicsControl.DisposalMethod == DisposalMethod.RestoreToBackground) { //GifFrame im = new GifFrame(imageWidth, imageHeight); //im.Clear(new Color32(0,0,0,0)); //_image.AddFrame(im); //_image.loop = false; } else if (_graphicsControl.DisposalMethod == DisposalMethod.RestoreToPrevious) { _image.loop = true; } } }