I have some trouble about drawing and then saving.
I want to open a image file, then when i click the button i made,
I can draw on the image what i open. And Drawing and images can be saved together.
First, I thought I'd save the path to the open image and use it to save the image with ink.
But this way ins't right way. Because I encountered System.UnauthorizedAccessException (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) this error.
So I'm in trouble now. I could save my image with drawing when the image is static .
Yet, when i dynamically open a image, then i don't know how to do that.
--this is my code--
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);
}
}
And this is my xaml code
<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>
User contributions licensed under CC BY-SA 3.0