Set ImageBrush.ImageSource to Image in Windows 8 store app

2.2k views Asked by At

I have an Image object in my C# code and I'd like to use it as ImageSource for ImageBrush. Is there a way to do this?

In other words, I need something like this:

Image image = new Image();

image.source = GetBitmapImage();

//execute various image transforms here...

ImageBrush imageBrush = new ImageBrush();

imageBrush.ImageSource = image; // this doesn't work

Thanks.

2

There are 2 answers

0
JleruOHeP On

You already have ImageSource - it is your GetBitmapImage(), so you can use

 ImageBrush imageBrush = new ImageBrush(GetBitmapImage());

or use your image.source:

imageBrush.ImageSource = image.source;
0
Nico On

The ImageSource property is set to type of Windows.UI.Xaml.Media.ImageSource. Therefore you must provide an object that derives from Windows.UI.Xaml.Media.ImageSource.

Your object "image" is of type Windows.UI.Xaml.Controls.Image which is not derrived from the ImageSource type.

However, your method GetBitmapImage() returns a type of ImageSource so you can call the code below after you have finished your modifications.

imageBrush.ImageSource = image.Source;

Cheers.