Пример #1
0
        async void mainCanvas_CreateResources(Microsoft.Graphics.Canvas.CanvasControl sender, object args)
        {
            m_isResourceLoadingDone = false;

            // WIN2D: resource loading from WinRT types including StorageFile and IRAS
            m_sourceBitmap = await CanvasBitmap.LoadAsync(sender, "Chrysanthemum.jpg");

            var sourceFile = await Package.Current.InstalledLocation.GetFileAsync("Chrysanthemum.jpg");

            // Win2D: because we can't lock/read pixels we rely on BitmapDecoder
            var stream = await sourceFile.OpenReadAsync();

            var decoder = await BitmapDecoder.CreateAsync(stream);

            // Technically these should always be identical to m_sourceBitmap.SizeInPixels;
            m_pixelArrayHeight = decoder.PixelHeight;
            m_pixelArrayWidth  = decoder.PixelWidth;
            var pixelProvider = await decoder.GetPixelDataAsync(
                BitmapPixelFormat.Bgra8,
                BitmapAlphaMode.Premultiplied,
                new BitmapTransform(),
                ExifOrientationMode.IgnoreExifOrientation, // Must do this.
                ColorManagementMode.ColorManageToSRgb
                );

            m_pixelArray = pixelProvider.DetachPixelData();

            m_targetBitmap = new CanvasRenderTarget(sender, new Size(m_pixelArrayWidth, m_pixelArrayHeight));

            m_rnd = new Random();

            m_isResourceLoadingDone = true;

            mainCanvas.Invalidate();
        }
Пример #2
0
        void mainCanvas_Draw(Microsoft.Graphics.Canvas.CanvasControl sender, Microsoft.Graphics.Canvas.CanvasDrawEventArgs args)
        {
            if (m_isResourceLoadingDone)
            {
                //args.DrawingSession.DrawImage(m_sourceBitmap);

                using (var ds = m_targetBitmap.CreateDrawingSession())
                {
                    // Draw a bunch of solid color strokes.
                    // - Centered at a random point
                    // - Color is determined by the bitmap value at that point
                    // - Fixed stroke length and width
                    // - Random angle
                    for (int i = 0; i < m_numStrokes; i++)
                    {
                        double centerXFactor = m_rnd.NextDouble();
                        double centerYFactor = m_rnd.NextDouble();
                        float  centerX       = (float)(centerXFactor * m_sourceBitmap.Size.Width);
                        float  centerY       = (float)(centerYFactor * m_sourceBitmap.Size.Height);
                        float  angle         = (float)(m_rnd.NextDouble() * 2 * Math.PI);
                        Color  color         = getColorFromBitmapCoordinates(centerXFactor, centerYFactor);

                        // Convert the stroke definition to bounding box for line.
                        float x1 = centerX + (float)Math.Cos(angle) * m_strokeLength / 2;
                        // Technically the coordinate systems don't match for Y, but it doesn't matter here.
                        float y1 = centerY + (float)Math.Sin(angle) * m_strokeLength / 2;
                        float x2 = 2 * centerX - x1;
                        float y2 = 2 * centerY - y1;

                        ds.DrawLine(x1, y1, x2, y2, color, m_strokeWidth);
                    }
                }

                args.DrawingSession.DrawImage(m_targetBitmap);
            }

            // Render looping.
            mainCanvas.Invalidate();
        }