How do I get WPF/Silverlight to show an image in memory?

I am trying to build a strategy game in WPF, and it will involve lots of interactive tiles and graphics.  The resources of constantly regenerating XAML on the fly is a pain on resources, when its including 100 or more various image placements.

I am used to writing games using an image buffer, and plan to apply the same technique in WPF.  

I will be storing a bitmap in memory, manipulating it, and then refreshing the XAML that holds a reference to it.  

***  I want XAML to refer to a location in memory, instead of a file in specific, then I can just refresh it everytime I change the image in memory.

If anyone understands what I'm trying to do, and has a better way, please let me know.
LVL 8
hpdvs2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tsellsCommented:
You may want to create an object data provider in XAML to an  object class with an image property then stream the image from memory to the property on that object.  Use the INotifiyPropertyChanged interface so the UI updates automatically when the source is changed.  

0
hpdvs2Author Commented:
could you give an example of that?  What you've suggested isn't straight forward for me to apply.  perhaps a bit of sample code?
0
Rob SiklosCommented:
I'm not sure about WPF, but in Silverlight you can just set the source of an Image control to a BitmapSource which comes from a byte array:


Stream _s = new MemoryStream(bytes);
_s.Position = 0;
BitmapImage _img = new BitmapImage();
_img.SetSource(_s);
imageControl.Source = _img;

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

mikebirtCommented:
Hi,

i'm not sure if this question is still open?

the c# code sample i've provided shows a provider class and the Xaml illustrates how it could be used to provide the image for something.

HTH

Mike


    public class ImageProvider
    {
        public BitmapImage Image
        {
            get
            {
                MemoryStream ms = new MemoryStream(File.ReadAllBytes(@"someimage.jpg"));
                BitmapImage bmp = new BitmapImage();
                bmp.BeginInit();
                bmp.StreamSource = ms;
                bmp.EndInit();
                return bmp;
            }
        }
    }

// xaml
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <local:ImageProvider x:Key="imageProvider" />
    </Window.Resources>

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

</Window>

Open in new window

0
ashersiddiqiCommented:
public class ImageProvider
    {
        public BitmapImage Image
        {
            get
            {
                MemoryStream ms = new MemoryStream(File.ReadAllBytes(@"someimage.jpg"));
                BitmapImage bmp = new BitmapImage();
                bmp.BeginInit();
                bmp.StreamSource = ms;
                bmp.EndInit();
                return bmp;
            }
        }
    }

// xaml
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <local:ImageProvider x:Key="imageProvider" />
    </Window.Resources>

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

</Window>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hpdvs2Author Commented:
This was about the best method to work with, but my real goal was not improved by it.  Silverlight/WPF is not capable of a good Back Buffered 2D game.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Game Programming

From novice to tech pro — start learning today.