Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is my Xaml code
XML
<Grid>
                       <Rectangle>
                           <Rectangle.Fill>
                               <ImageBrush x:Name="ImgServiceOn" ImageSource="Images/btn-on.png"/>
                           </Rectangle.Fill>
                       </Rectangle>
                   </Grid>

I want to change the source of this Image brush from C#
C#
ImgServiceOn.BeginInit();
           ImgServiceOn.Source = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
           ImgServiceOn.EndInit();


But giving error.
Posted
Comments
Sergey Alexandrovich Kryukov 13-Nov-14 15:54pm    
Why not using an image from embedded resources?
—SA

1 solution

C#
var image = System.Drawing.Image.FromFile("..."); // or wherever it comes from
var bitmap = new System.Drawing.Bitmap(image);
var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(),IntPtr.Zero,Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
bitmap.Dispose();
var brush = new ImageBrush(bitmapSource);
 
Share this answer
 

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