代码之家  ›  专栏  ›  技术社区  ›  Kay

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

  •  1
  • Kay  · 技术社区  · 6 年前

    我想打开一个图像文件,然后当我点击我做的按钮,

    我可以在打开的图片上画画。图形和图像可以一起保存。

    但这条路不对。因为我遇到了 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 回复  |  直到 6 年前