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?
 
ashersiddiqiConnect With a Mentor Commented:
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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
 
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
All Courses

From novice to tech pro — start learning today.