1

Here I have code of XAML, I want save as Image using windows forms save file dailog and upload image to inkcanvas.

        <StackPanel Margin="0,4,45,0" DockPanel.Dock="Right" Width="580">
        <Border BorderBrush="Red" BorderThickness="5">
            <InkCanvas Name="InkCanvas1" Height="200">
                <InkCanvas.DefaultDrawingAttributes>
                    <DrawingAttributes Width="20" Height="20" Color="Red" ></DrawingAttributes>
                </InkCanvas.DefaultDrawingAttributes>
            </InkCanvas>
        </Border>
    </StackPanel>

How to save a particular area of inkcanvas and upload image to inkcanvas. Please Help me.

Sanjay
  • 1,226
  • 3
  • 21
  • 45
  • Please check below link. already answered. ------------------------------------------ https://stackoverflow.com/questions/17937856/save-an-image-from-ink-canvas/51207941#51207941 – Sandeep Jadhav Jul 06 '18 at 12:29

1 Answers1

0

You have to convert your InkCanvas to a bitmap. The following code is quick n dirty.

<Window x:Class="InkCanvas.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <InkCanvas Name="yourinkcanvas">
        <Image Source="http://blogs.msdn.com/blogfiles/expression/WindowsLiveWriter/ASmallInkCanvasSample_10388/inkcanvasExample_a6d9403d-4bca-40bb-93ad-6364d537f2c6.png"/>
    </InkCanvas>
    <Button Content="SAVE" Height="20" VerticalAlignment="Bottom" Click="Button_Click"/>
</Grid>

private void Button_Click(object sender, RoutedEventArgs e)
    {
        Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
        dlg.FileName = "savedimage"; // Default file name
        dlg.DefaultExt = ".jpg"; // Default file extension
        dlg.Filter = "Image (.jpg)|*.jpg"; // Filter files by extension

        // Show save file dialog box
        Nullable<bool> result = dlg.ShowDialog();

        // Process save file dialog box results
        if (result == true)
        {
            // Save document
            string filename = dlg.FileName;
            //get the dimensions of the ink control
            int margin = (int)this.yourinkcanvas.Margin.Left;
            int width = (int)this.yourinkcanvas.ActualWidth - margin;
            int height = (int)this.yourinkcanvas.ActualHeight - margin;
            //render ink to bitmap
            RenderTargetBitmap rtb =
            new RenderTargetBitmap(width, height, 96d, 96d, PixelFormats.Default);
            rtb.Render(yourinkcanvas);

            using (FileStream fs = new FileStream(filename, FileMode.Create))
            {
                BmpBitmapEncoder encoder = new BmpBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(rtb));
                encoder.Save(fs);    
            }
        }
    }

To save a particular area use margin, width and height.

aDoubleSo
  • 1,128
  • 9
  • 19