示例#1
0
        public static HttpResponseMessage GenerateImageFractal(
            [HttpTrigger(AuthorizationLevel.Anonymous, methods: "get")]
            HttpRequestMessage req,
            ILogger log)
        {
            FractalInit initdata = new FractalInit
            {
                height        = 800,
                maxIterations = 128,
                width         = 800,
                xMax          = 1,
                xMin          = -3,
                yMax          = 2,
                yMin          = -2
            };

            // crate a surface
            var info = new SKImageInfo(initdata.width, initdata.height);

            //var pixmap = new SKPixmap();
            //pixmap.

            using (var surface = SKSurface.Create(info))
            {
                var mandelbrot = new FractalMandelbrot(initdata);
                var bytes      = mandelbrot.compute();

                // the the canvas and properties
                var canvas = surface.Canvas;

                for (int y = 0; y < initdata.height; y++)
                {
                    for (int x = 0; x < initdata.width; x++)
                    {
                        int index = 4 * (x + y * initdata.width);
                        canvas.DrawPoint(new SKPoint(x, y), new SKColor(bytes[index], bytes[index + 1], bytes[index + 2]));
                    }
                }

                using (var image = surface.Snapshot())
                    using (var data = image.Encode(SKEncodedImageFormat.Png, 100))
                    {
                        var response = new HttpResponseMessage();
                        response.Content    = new StreamContent(data.AsStream());
                        response.StatusCode = System.Net.HttpStatusCode.OK;
                        response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
                        return(response);
                    }
            }
        }
        private static SKData CreateFractalImageData(FractalInput input)
        {
            FractalInit initdata = InitData(0, 0, input.zoom);

            // Create a surface.
            var info = new SKImageInfo(initdata.width, initdata.height, SKImageInfo.PlatformColorType, SKAlphaType.Unpremul);

            //using (var surface = SKSurface.Create(info))
            {
                var mandelbrot = new FractalMandelbrot(initdata);
                var bytes      = mandelbrot.compute();

                // the the canvas and properties
                //var canvas = surface.Canvas;

                //for (int y = 0; y < initdata.height; y++)
                //{
                //    for (int x = 0; x < initdata.width; x++)
                //    {
                //        int index = 4 * (x + y * initdata.width);
                //        canvas.DrawPoint(new SKPoint(x, y), new SKColor(bytes[index + 2], bytes[index + 1], bytes[index]));
                //    }
                //}

                // Optimization taken from https://github.com/mono/SkiaSharp/issues/416
                // create an empty bitmap
                var bitmap = new SKBitmap();
                // pin the managed array so that the GC doesn't move it
                var gcHandle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
                // install the pixels with the color type of the pixel data
                bitmap.InstallPixels(info, gcHandle.AddrOfPinnedObject(), info.RowBytes, delegate { gcHandle.Free(); }, null);

                //using (var image = surface.Snapshot())
                using (var image = SKImage.FromBitmap(bitmap))
                {
                    SKData data = image.Encode(SKEncodedImageFormat.Png, 100);
                    return(data);
                }
            }
        }