如何绘制图像并保存在UWP中?

时间:2018-08-07 07:09:50

标签: c# image uwp draw drawimage

我在绘制然后保存时遇到一些麻烦。

我想打开一个图像文件,然后单击我制作的按钮,

我可以在打开的图像上绘制图像。而且图形和图像可以一起保存。

首先,我想我将保存打开图像的路径,并用它来保存带有墨水的图像。

但是这种方式不是正确的方式。因为我遇到了 System.UnauthorizedAccessException(HRESULT的异常:0x80070005(E_ACCESSDENIED)) 。此错误。

所以我现在有麻烦了。当图像是静态的时,我可以通过绘图保存图像。

但是,当我动态打开图像时,我不知道该怎么做。

-这是我的代码-

private async void IMG_open2_Click(object sender, RoutedEventArgs e)
        {


            var picker = new Windows.Storage.Pickers.FileOpenPicker
            {
                ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail,
                SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary
            };
            picker.FileTypeFilter.Add(".jpg");
            picker.FileTypeFilter.Add(".jpeg");
            picker.FileTypeFilter.Add(".png");
            picker.FileTypeFilter.Add(".bmp");

            Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
            if (file != null)
            {

                if (!IMG_G2.IsOpen) { IMG_G2.IsOpen = true; }

    //when i open image, then i save image's path so i thought that i can use to save it
                Img_path = file;

                // Application now has read/write access to the picked file
                using (Windows.Storage.Streams.IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
                {
                    // Set the image source to the selected bitmap 
                    Windows.UI.Xaml.Media.Imaging.BitmapImage bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage
                    {

                        DecodePixelWidth = 600 
                    };
                    await bitmapImage.SetSourceAsync(fileStream);                  

                    img2.Source = bitmapImage;                    

                    IMG_C2.Visibility = Visibility.Visible;                   

                }
            }
            else
            {
                this.textBlock.Text = "Operation cancelled.";
            }
        }

     async private void Img2_save_Click(object sender, RoutedEventArgs e)
                {
                    StorageFolder storageFolder = KnownFolders.PicturesLibrary;
                    var img_ink = await storageFolder.CreateFileAsync(ink_img + index + ".jpg", CreationCollisionOption.ReplaceExisting);
                CanvasDevice device = CanvasDevice.GetSharedDevice();

                    CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, (int)img2.ActualWidth, (int)img2.ActualHeight, 96);

                    //get image's path
                    var inputFile = Img_path;



                    using (var ds = renderTarget.CreateDrawingSession())
                    {
                        ds.Clear(Colors.White);

                        var image = await CanvasBitmap.LoadAsync(device, inputFile.Path);

                        //var image = img2.Source;
                        // I want to use this too, but I have no idea about this


                        ds.DrawImage(image);                

                        ds.DrawInk(img2_ink.InkPresenter.StrokeContainer.GetStrokes());
                    }


                    // save results           

                    using (var fileStream = await img_ink.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        await renderTarget.SaveAsync(fileStream, CanvasBitmapFileFormat.Jpeg, 1f);
                    }
                }

这是我的xaml代码

 <Popup x:Name="IMG_G2"  ManipulationMode="All" Canvas.ZIndex="5" Width="600" Height="300"  >
        <Grid x:Name="img2_grid" ManipulationDelta="Img_ManipulationDelta2"  ManipulationMode="Scale">
            <Image  VerticalAlignment="Top" HorizontalAlignment="Left" x:Name="img2" ManipulationMode="All" Stretch="Fill" Height="400" Width="600" >
                <Image.RenderTransform>
                    <ScaleTransform x:Name="ScaleTransform2" ScaleX="1.0" ScaleY="1.0"/>
                </Image.RenderTransform>                    
            </Image>
            <InkCanvas x:Name="img2_ink" />
            <InkToolbar x:Name="img2_toolbar" Canvas.ZIndex="7" TargetInkCanvas="{x:Bind img2_ink}" >
                <InkToolbarCustomToggleButton x:Name="Img2_save" Click="Img2_save_Click">
                    <SymbolIcon Symbol="Save"/>
                </InkToolbarCustomToggleButton>
            </InkToolbar>

            <Button x:Name="IMG_C2" Canvas.ZIndex="8" Content="Close" Background="White"  VerticalAlignment="Top" HorizontalAlignment="Left" Click="IMG_C2_Click" />
            <Button x:Name="Img2_draw" Content="Draw" HorizontalAlignment="Right" Click="Img2_draw_Click" Canvas.ZIndex="7"/>                
        </Grid>

    </Popup>


    <StackPanel>        
    <Button x:Name="Img_open2" Content="Open_image2" Click="IMG_open2_Click" />
    </StackPanel>

0 个答案:

没有答案