Beispiel #1
0
        public async Task OpenFileAsync(StorageFile file)
        {
            CanvasBitmap fileBitmap = null;
            var          extension  = file.FileType;

            switch (extension)
            {
            case ".bin":
                var buffer = await FileIO.ReadBufferAsync(file);

                var width  = 0;
                var height = 0;
                var format = DirectXPixelFormat.B8G8R8A8UIntNormalized;

                // If the image name ends in (width)x(height), then use that in the dialog
                var fileName = file.Name;
                var fileStem = fileName.Substring(0, fileName.LastIndexOf('.'));
                var pattern  = @".*[A-z](?<width>[0-9]+)x(?<height>[0-9]+)";
                var match    = Regex.Match(fileStem, pattern);
                if (match.Success)
                {
                    ResetBinaryDetailsInputDialog(int.Parse(match.Groups["width"].Value), int.Parse(match.Groups["height"].Value));
                }
                else
                {
                    ResetBinaryDetailsInputDialog();
                }

                var dialogResult = await BinaryDetailsInputDialog.ShowAsync();

                if (dialogResult == ContentDialogResult.Primary &&
                    ParseBinaryDetailsSizeBoxes(out width, out height))
                {
                    fileBitmap = CanvasBitmap.CreateFromBytes(_canvasDevice, buffer, width, height, format);
                }
                break;

            default:
                // open it with win2d
                using (var stream = await file.OpenReadAsync())
                {
                    fileBitmap = await CanvasBitmap.LoadAsync(_canvasDevice, stream);
                }
                break;
            }

            if (fileBitmap != null)
            {
                var size = fileBitmap.SizeInPixels;
                var backgroundSurface = _compositionGraphics.CreateDrawingSurface2(
                    new SizeInt32()
                {
                    Width = (int)size.Width, Height = (int)size.Height
                },
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    DirectXAlphaMode.Premultiplied);
                using (var drawingSession = CanvasComposition.CreateDrawingSession(backgroundSurface))
                {
                    drawingSession.FillRectangle(0, 0, size.Width, size.Height, _backgroundCavnasBrush);
                }

                var imageSurface = _compositionGraphics.CreateDrawingSurface2(
                    new SizeInt32()
                {
                    Width = (int)size.Width, Height = (int)size.Height
                },
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    DirectXAlphaMode.Premultiplied);
                using (var drawingSession = CanvasComposition.CreateDrawingSession(imageSurface))
                {
                    drawingSession.Clear(Colors.Transparent);
                    drawingSession.DrawImage(fileBitmap);
                }

                _backgroundBrush.Surface = backgroundSurface;
                _imageBrush.Surface      = imageSurface;
                ImageGrid.Width          = size.Width;
                ImageGrid.Height         = size.Height;
                ImageScrollViewer.ChangeView(0, 0, 1, true);
                if (_borderEnabled)
                {
                    ImageBorderBrush.Color = Colors.Black;
                }
                _currentFile   = file;
                _currentBitmap = fileBitmap;
            }
        }
        public async Task OpenFileAsync(StorageFile file)
        {
            CanvasBitmap fileBitmap = null;
            var          extension  = file.FileType;

            switch (extension)
            {
            case ".bin":
                var buffer = await FileIO.ReadBufferAsync(file);

                var width        = 0;
                var height       = 0;
                var format       = DirectXPixelFormat.B8G8R8A8UIntNormalized;
                var dialogResult = await BinaryDetailsInputDialog.ShowAsync();

                if (dialogResult == ContentDialogResult.Primary &&
                    ParseBinaryDetailsSizeBoxes(out width, out height))
                {
                    fileBitmap = CanvasBitmap.CreateFromBytes(_canvasDevice, buffer, width, height, format);
                }
                break;

            default:
                // open it with win2d
                using (var stream = await file.OpenReadAsync())
                {
                    fileBitmap = await CanvasBitmap.LoadAsync(_canvasDevice, stream);
                }
                break;
            }

            if (fileBitmap != null)
            {
                var size = fileBitmap.SizeInPixels;
                var backgroundSurface = _compositionGraphics.CreateDrawingSurface2(
                    new SizeInt32()
                {
                    Width = (int)size.Width, Height = (int)size.Height
                },
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    DirectXAlphaMode.Premultiplied);
                using (var drawingSession = CanvasComposition.CreateDrawingSession(backgroundSurface))
                {
                    drawingSession.FillRectangle(0, 0, size.Width, size.Height, _backgroundCavnasBrush);
                }

                var imageSurface = _compositionGraphics.CreateDrawingSurface2(
                    new SizeInt32()
                {
                    Width = (int)size.Width, Height = (int)size.Height
                },
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    DirectXAlphaMode.Premultiplied);
                using (var drawingSession = CanvasComposition.CreateDrawingSession(imageSurface))
                {
                    drawingSession.Clear(Colors.Transparent);
                    drawingSession.DrawImage(fileBitmap);
                }

                _backgroundBrush.Surface = backgroundSurface;
                _imageBrush.Surface      = imageSurface;
                ImageGrid.Width          = size.Width;
                ImageGrid.Height         = size.Height;
                ImageScrollViewer.ChangeView(0, 0, 1, true);
                if (_borderEnabled)
                {
                    ImageBorderBrush.Color = Colors.Black;
                }
                _currentFile = file;
            }
        }