Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,

I have this code about an image in my page.

XML
<!--<Border BorderThickness="5" BorderBrush="#FF000000">-->
                    <Image  Height="304" HorizontalAlignment="Left" Stretch="Fill" VerticalAlignment="Top" Width="356" Margin="108,435,0,0"  />
            <!--</Border>-->



Inside my image box, I have an image, but is there anyway to capture the image?

Help needed. Thanks in advance.
Posted
Comments
Fredrik Bornander 5-May-14 3:34am    
What do you mean by 'capture'?
Get the image from the image into something that you can manipulate or save or what?
Alif Marz 5-May-14 3:49am    
Get the image from the image box so that I can manipulate in another code.

1 solution

One approach could be to create a RenderBitmapTarget and draw the source image onto that. And then use that as the source of another Image.

Consider this example;
XML
<window x:class="WpfApplication9.MainWindow" xmlns:x="#unknown">
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MyAwesomeWindow" Height="350" Width="525">
    <grid>
        <grid.rowdefinitions>
            <rowdefinition height="*" />
            <rowdefinition height="*" />
            <rowdefinition height="Auto" />
        </grid.rowdefinitions>       
        <image x:name="Src" grid.row="0" source="C:\Temp\someimage.png" />
        <image x:name="Dst" grid.row="1" />
        <button grid.row="2" content="Do it!" click="ButtonBase_OnClick" />
    </grid>
</window>

With the backing code of;
C#
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Media;

namespace WpfApplication9 {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e) {
            var visual = new DrawingVisual();
            var context = visual.RenderOpen();

            // Copy source image over
            context.DrawImage(Src.Source, new Rect(0, 0, Src.Source.Width, Src.Source.Height));
            // You can also draw whatever you like on this!
            context.DrawLine(new Pen(Brushes.PeachPuff, 4.0), new Point(0, 0), new Point(100, 100));
            context.Close();

            var renderTarget = new RenderTargetBitmap((int)Src.Source.Width, (int)Src.Source.Height, 96, 96, PixelFormats.Pbgra32);
            renderTarget.Render(visual);

            Dst.Source = renderTarget;
        }
    }
}


In the above example the image, as rendered onto the top image, Src, is draw onto the Dst image when the button is clicked. As the RenderContext is available you can manipulate the image any which way you like.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
Member 12261658 22-Sep-22 8:29am    
Thanks Fredrik Bornander, You saved my life....
I was capturing image using MediaCapture object using preview.StartAsync()and the captured Image's format was D3DImage so was not able to convert into Bitmapsource.
After getting your solution saved my effort.
Fredrik Bornander 2-Oct-22 5:04am    
6 years after I posted the reply it was still useful enough to save someone's life :)

Thank you for commenting, it really made my day.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900