コード例 #1
0
        private async void Paster_Click(object sender, RoutedEventArgs e)
        {
            string        desiredName       = DateTime.Now.Ticks + ".jpg";
            StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
            StorageFolder folder            = await applicationFolder.CreateFolderAsync("Pic", CreationCollisionOption.OpenIfExists);

            StorageFile saveFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.OpenIfExists);

            RenderTargetBitmap bitmap = new RenderTargetBitmap();
            await bitmap.RenderAsync(Img);

            var pixelBuffer = await bitmap.GetPixelsAsync();

            using (var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite))
            {
                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream);

                encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                     BitmapAlphaMode.Ignore,
                                     (uint)bitmap.PixelWidth,
                                     (uint)bitmap.PixelHeight,
                                     DisplayInformation.GetForCurrentView().LogicalDpi,
                                     DisplayInformation.GetForCurrentView().LogicalDpi,
                                     pixelBuffer.ToArray());
                await encoder.FlushAsync();
            }



            if (saveFile != null)
            {
                paster editor = new paster();
                editor.Show(saveFile);

                editor.ImageEditedCompleted += (image_edited) =>
                {
                    Img.Source = image_edited;
                };
            }
        }
コード例 #2
0
ファイル: Page1.xaml.cs プロジェクト: tyuchn/PhotoShop-UWP
        private async void Options_changed(object sender, SelectionChangedEventArgs e)
        {
            if (Cut.IsSelected)
            {
                string        desiredName       = DateTime.Now.Ticks + ".jpg";
                StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
                StorageFolder folder            = await applicationFolder.CreateFolderAsync("Pic", CreationCollisionOption.OpenIfExists);

                StorageFile saveFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.OpenIfExists);

                RenderTargetBitmap bitmap = new RenderTargetBitmap();
                await bitmap.RenderAsync(Img);

                var pixelBuffer = await bitmap.GetPixelsAsync();

                using (var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream);

                    encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                         BitmapAlphaMode.Ignore,
                                         (uint)bitmap.PixelWidth,
                                         (uint)bitmap.PixelHeight,
                                         DisplayInformation.GetForCurrentView().LogicalDpi,
                                         DisplayInformation.GetForCurrentView().LogicalDpi,
                                         pixelBuffer.ToArray());
                    await encoder.FlushAsync();
                }



                if (saveFile != null)
                {
                    CutPicture(saveFile);
                }
            }
            else
            {
                if (addfilter.IsSelected)
                {
                    string        desiredName       = DateTime.Now.Ticks + ".jpg";
                    StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
                    StorageFolder folder            = await applicationFolder.CreateFolderAsync("Pic", CreationCollisionOption.OpenIfExists);

                    StorageFile saveFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.OpenIfExists);

                    RenderTargetBitmap bitmap = new RenderTargetBitmap();
                    await bitmap.RenderAsync(Img);

                    var pixelBuffer = await bitmap.GetPixelsAsync();

                    using (var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream);

                        encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                             BitmapAlphaMode.Ignore,
                                             (uint)bitmap.PixelWidth,
                                             (uint)bitmap.PixelHeight,
                                             DisplayInformation.GetForCurrentView().LogicalDpi,
                                             DisplayInformation.GetForCurrentView().LogicalDpi,
                                             pixelBuffer.ToArray());
                        await encoder.FlushAsync();
                    }
                    if (saveFile != null)
                    {
                        BlankPage2 editor = new BlankPage2();
                        editor.Show(saveFile);

                        editor.ImageEditedCompleted += (image_edited) =>
                        {
                            Img.Source = image_edited;
                        };
                    }
                }
                else
                {
                    if (blur.IsSelected)
                    {
                        string        desiredName       = DateTime.Now.Ticks + ".jpg";
                        StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
                        StorageFolder folder            = await applicationFolder.CreateFolderAsync("Pic", CreationCollisionOption.OpenIfExists);

                        StorageFile saveFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.OpenIfExists);

                        RenderTargetBitmap bitmap = new RenderTargetBitmap();
                        await bitmap.RenderAsync(Img);

                        var pixelBuffer = await bitmap.GetPixelsAsync();

                        using (var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite))
                        {
                            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream);

                            encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                                 BitmapAlphaMode.Ignore,
                                                 (uint)bitmap.PixelWidth,
                                                 (uint)bitmap.PixelHeight,
                                                 DisplayInformation.GetForCurrentView().LogicalDpi,
                                                 DisplayInformation.GetForCurrentView().LogicalDpi,
                                                 pixelBuffer.ToArray());
                            await encoder.FlushAsync();
                        }


                        if (saveFile == null)
                        {
                            // The user cancelled the picking operation
                            return;
                        }


                        SoftwareBitmap inputBitmap;
                        using (IRandomAccessStream stream = await saveFile.OpenAsync(FileAccessMode.Read))
                        {
                            // Create the decoder from the stream
                            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

                            // Get the SoftwareBitmap representation of the file
                            inputBitmap = await decoder.GetSoftwareBitmapAsync();
                        }

                        if (inputBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 ||
                            inputBitmap.BitmapAlphaMode != BitmapAlphaMode.Premultiplied)
                        {
                            inputBitmap = SoftwareBitmap.Convert(inputBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                        }

                        SoftwareBitmap outputBitmap = new SoftwareBitmap(inputBitmap.BitmapPixelFormat, inputBitmap.PixelWidth, inputBitmap.PixelHeight, BitmapAlphaMode.Premultiplied);


                        var helper = new OpenCVBridge.OpenCVHelper();
                        helper.Blur(inputBitmap, outputBitmap);

                        var bitmapSource = new SoftwareBitmapSource();
                        await bitmapSource.SetBitmapAsync(outputBitmap);

                        Img.Source = bitmapSource;
                    }
                    else
                    {
                        if (scrawl.IsSelected)
                        {
                            string        desiredName       = DateTime.Now.Ticks + ".jpg";
                            StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
                            StorageFolder folder            = await applicationFolder.CreateFolderAsync("Pic", CreationCollisionOption.OpenIfExists);

                            StorageFile saveFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.OpenIfExists);

                            RenderTargetBitmap bitmap = new RenderTargetBitmap();
                            await bitmap.RenderAsync(Img);

                            var pixelBuffer = await bitmap.GetPixelsAsync();

                            using (var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite))
                            {
                                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream);

                                encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                                     BitmapAlphaMode.Ignore,
                                                     (uint)bitmap.PixelWidth,
                                                     (uint)bitmap.PixelHeight,
                                                     DisplayInformation.GetForCurrentView().LogicalDpi,
                                                     DisplayInformation.GetForCurrentView().LogicalDpi,
                                                     pixelBuffer.ToArray());
                                await encoder.FlushAsync();
                            }

                            if (saveFile != null)
                            {
                                scrawl editor = new scrawl();
                                editor.Show(saveFile);

                                editor.ImageEditedCompleted += (image_edited) =>
                                {
                                    Img.Source = image_edited;
                                };
                            }
                        }
                        else
                        {
                            if (open.IsSelected)
                            {
                                //文件选择器
                                FileOpenPicker openPicker = new FileOpenPicker();
                                //初始位置
                                openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
                                //添加文件类型
                                openPicker.FileTypeFilter.Add(".jpg");
                                openPicker.FileTypeFilter.Add(".jpeg");
                                openPicker.FileTypeFilter.Add(".png");
                                openPicker.FileTypeFilter.Add(".gif");
                                //选取单个文件
                                Windows.Storage.StorageFile file = await openPicker.PickSingleFileAsync();



                                if (file != null)
                                {
                                    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
                                    {
                                        var srcImage = new BitmapImage();
                                        await srcImage.SetSourceAsync(stream);

                                        Img.Source = srcImage;
                                    }
                                }
                            }
                            else
                            {
                                if (regenerate.IsSelected)
                                {
                                    MyFrame.Navigate(typeof(Page1));
                                }
                                else
                                {
                                    if (Camera.IsSelected)
                                    {
                                        CameraCaptureUI captureUI = new CameraCaptureUI();
                                        captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
                                        captureUI.PhotoSettings.CroppedSizeInPixels = new Size(200, 200);

                                        StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);

                                        if (photo == null)
                                        {
                                            // User cancelled photo capture
                                            return;
                                        }

                                        IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);

                                        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

                                        SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();

                                        SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap,
                                                                                                   BitmapPixelFormat.Bgra8,
                                                                                                   BitmapAlphaMode.Premultiplied);

                                        SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
                                        await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);

                                        Img.Source = bitmapSource;
                                    }
                                    else
                                    {
                                        if (save.IsSelected)
                                        {
                                            FileSavePicker savefile = new FileSavePicker();
                                            //初始位置
                                            savefile.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
                                            // 显示在下拉列表的文件类型
                                            savefile.FileTypeChoices.Add("图片", new List <string>()
                                            {
                                                ".png", ".jpg", ".jpeg", ".bmp"
                                            });
                                            // 默认的文件名
                                            savefile.SuggestedFileName = "SaveFile";

                                            StorageFile sFile = await savefile.PickSaveFileAsync();

                                            if (sFile != null)
                                            {
                                                // 在用户完成更改并调用CompleteUpdatesAsync之前,阻止对文件的更新
                                                CachedFileManager.DeferUpdates(sFile);
                                                //把控件变成图像
                                                RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
                                                //传入参数Image控件
                                                await renderTargetBitmap.RenderAsync(Img);

                                                IBuffer pixelbuffer = await renderTargetBitmap.GetPixelsAsync();

                                                using (var fileStream = await sFile.OpenAsync(FileAccessMode.ReadWrite))
                                                {
                                                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream);

                                                    encoder.SetPixelData(
                                                        BitmapPixelFormat.Bgra8,
                                                        BitmapAlphaMode.Ignore,
                                                        (uint)renderTargetBitmap.PixelWidth,
                                                        (uint)renderTargetBitmap.PixelHeight,
                                                        DisplayInformation.GetForCurrentView().LogicalDpi,
                                                        DisplayInformation.GetForCurrentView().LogicalDpi,
                                                        pixelbuffer.ToArray()
                                                        );
                                                    //刷新图像
                                                    await encoder.FlushAsync();
                                                }
                                            }
                                        }
                                        else
                                        {
                                            if (Sharpen.IsSelected)
                                            {
                                                string        desiredName       = DateTime.Now.Ticks + ".jpg";
                                                StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
                                                StorageFolder folder            = await applicationFolder.CreateFolderAsync("Pic", CreationCollisionOption.OpenIfExists);

                                                StorageFile saveFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.OpenIfExists);

                                                RenderTargetBitmap bitmap = new RenderTargetBitmap();
                                                await bitmap.RenderAsync(Img);

                                                var pixelBuffer = await bitmap.GetPixelsAsync();

                                                using (var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite))
                                                {
                                                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream);

                                                    encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                                                         BitmapAlphaMode.Ignore,
                                                                         (uint)bitmap.PixelWidth,
                                                                         (uint)bitmap.PixelHeight,
                                                                         DisplayInformation.GetForCurrentView().LogicalDpi,
                                                                         DisplayInformation.GetForCurrentView().LogicalDpi,
                                                                         pixelBuffer.ToArray());
                                                    await encoder.FlushAsync();
                                                }

                                                if (saveFile != null)
                                                {
                                                    BlankPage1 editor = new BlankPage1();
                                                    editor.Show(saveFile);

                                                    editor.ImageEditedCompleted += (image_edited) =>
                                                    {
                                                        Img.Source = image_edited;
                                                    };
                                                }
                                            }
                                            else
                                            {
                                                string        desiredName       = DateTime.Now.Ticks + ".jpg";
                                                StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
                                                StorageFolder folder            = await applicationFolder.CreateFolderAsync("Pic", CreationCollisionOption.OpenIfExists);

                                                StorageFile saveFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.OpenIfExists);

                                                RenderTargetBitmap bitmap = new RenderTargetBitmap();
                                                await bitmap.RenderAsync(Img);

                                                var pixelBuffer = await bitmap.GetPixelsAsync();

                                                using (var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite))
                                                {
                                                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream);

                                                    encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                                                         BitmapAlphaMode.Ignore,
                                                                         (uint)bitmap.PixelWidth,
                                                                         (uint)bitmap.PixelHeight,
                                                                         DisplayInformation.GetForCurrentView().LogicalDpi,
                                                                         DisplayInformation.GetForCurrentView().LogicalDpi,
                                                                         pixelBuffer.ToArray());
                                                    await encoder.FlushAsync();
                                                }



                                                if (saveFile != null)
                                                {
                                                    paster editor = new paster();
                                                    editor.Show(saveFile);

                                                    editor.ImageEditedCompleted += (image_edited) =>
                                                    {
                                                        Img.Source = image_edited;
                                                    };
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }