Binding an Image in WPF?


I wanna show an image in WPF that is created by a process,
e.g : we have a method that is named createWPFImage()

Image createWPFImage() { ... }

So, the output of createWPFImage() is an Image.
In the XAML code, we have a something like below :

    <StackPanel Orientation="Horizontal">
        <Image Width="64" Height="64" Margin="0 2 4 0" />
        <TextBlock Text="{Binding Path=Description}" VerticalAlignment="Center" />

Now, How can I bind the output of createWPFImage() to the Image in XAML code ?
I would be appreciate if you guide me.

2010-02-04 12:06:07 Scores:0
What do you mean by "binding" to a method? In WPF, "binding" means changing the target value when the source value changes (or vice-versa; except for one-time binding). Or you just want to "set" the output of method as Image? Is the method returning different output based on different conditions? Have you tried using a dependency property?
2010-02-04 16:19:27 Scores:0

2 answers

Answer 1

Say you have class "MyClass" with method "CreateWpfImage" (see example below).

In your XAML you can create MyClass, and then call CreateWpfImage, using ObjectDataProvider in a Resources section (See Bea Stollnitz blog article ObjectDataProvider).


<Window x:Class="MyApplicationNamespace.Window1"
    Title="Window1" Height="300" Width="300">       

    <ObjectDataProvider ObjectType="{x:Type MyApplicationNamespace:MyClass}" x:Key="MyClass" />
    <ObjectDataProvider ObjectInstance="{StaticResource MyClass}" MethodName="CreateWpfImpage" x:Key="MyImage" />

    <Image Source="{Binding Source={StaticResource MyImage}, Path=Source}"/>

Example MyClass code to create an image for the XAML to use -

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace MyApplicationNamespace
    public class MyClass
        public Image CreateWpfImpage()
            GeometryDrawing aGeometryDrawing = new GeometryDrawing();
            aGeometryDrawing.Geometry = new EllipseGeometry(new Point(50, 50), 50, 50);
            aGeometryDrawing.Pen = new Pen(Brushes.Red, 10);
            aGeometryDrawing.Brush = Brushes.Blue;
            DrawingImage geometryImage = new DrawingImage(aGeometryDrawing);

            Image anImage = new Image();
            anImage.Source = geometryImage;
            return anImage;
Edward H
2010-02-04 16:30:04
ObjectDataProvider is Static, but I'm gonna do it Dynamic !
Mohammad H
2010-02-05 06:01:07 Scores:0
Answer 2

If you have a path to your image and just want to be able to change the image on the fly, then bind to a dependency property of type string and in your method, set the value of the dependency property.

<Image Source="{Binding MyImagePath}" />

    public static readonly DependencyProperty MyImagePathProperty = DependencyProperty.Register("MyImagePath", typeof(string), typeof(ClassName), new PropertyMetadata("pack://application:,,,/YourAssembly;component//icons/icon1.png"));

    public string MyImagePath
        get { return (string)GetValue(MyImagePathhProperty); }
        set { SetValue(MyImagePathProperty, value); }
Alex B H
2010-02-04 17:12:11
Share |
View original post at

Related topics

Binding Image.Source to String in WPF?

I have below XAML code : <Window x:Class="WpfApplication1.Window1" xmlns="" xmlns:x="" DataContext="{Binding RelativeSource={RelativeSource Self}}" WindowStartupLocation="CenterScreen" Title="Window1" Height="300" Width="300"> <Grid> <Image x:Name="TestImage" Source="{Binding Path=ImageSource}" /> ...

Binding image in Isolated Storage

Hey. I have a list of items that the user can search. The search results are displayed in a listbox. Each animal object has a path to an image in Isolated Storage. What's the quickest way to bind my Image control inside the listboxitem to the image in the ...

Solution to binding errors thrown by setting Image.Source to a binding to null. (suppression?)

I've got a scenario where I have Image Sources set to null which are resolved later and then displayed. Any good way to prevent my code from throwing binding errors? An example: System.Windows.Data Error: 23 : Cannot convert '' from type '' to type 'System.Windows.Media.ImageSource' for 'en-US' culture with ...

how Image Binding Dynamically WPF?

i need to bind an image on run time in code behind thing is it's for WPF user control how can i do it? think i'm totally new to WPF and please explain how to do it appreciate your reply. thanks ...

Image Binding, Silverlight, C#, WP7

I have successfully managed to bind images stored in Isolated Storage to "Image" elements inside a listbox defined in XAML w/o any problems. What I did was to use a property that stored the name and location of the image in isolated storage and an IValueConverter to actually open the file ...

Image binding in WPF

I have an Image control that is supposed to do a slide show. Here are the binding I used to achieve this: Binding mapBinding = new Binding(); mapBinding.Source = slideView; mapBinding.Path = new PropertyPath("ImageDrawing"); sliderImage.SetBinding(System.Windows.Controls.Image.SourceProperty, mapBinding); And a class SlideImage public ...

Add Image Column to a GridView After Binding

This is May Sample Code in ASP.NET How Can I Add A similar Image Column To All Rows After Binding gridview1.DataSource = MB.GetTest(); gridview1.DataBind(); And My ASPX Page: <asp:GridView ID="gridview1" runat="server"> ...

Image not showing up while binding using ContentTemplate

I have a control to which i need to bind a instances of following object. However the image is not showing up in the control. please tell me what am i doing wrong. public class DataNeeded { public ...

When binding an image source to a BitmapSource, the image stays blank

I'm trying to show a camera feed from a Kinect in a WPF application. However, the image appears blank. Below is a snippet of what I have in my Kinect class, this all fires correctly, and the BitmapSource appears to be created fine. public delegate void FrameChangedDelegate(BitmapSource frame); public event FrameChangedDelegate FrameChanged; //this event ...

Binding source to image in button WP7

I am actually trying to bind the image source to my image in my button, dunno what's wrong but nothing appears... This is my xaml <Grid> <Grid x:Name="FakeGridBig" Background="White" Opacity="0.1" HorizontalAlignment="Left" Width="714" Height="269" Margin="3,0,0,0"></Grid> <Button x:Name="Featured_Big" ...