Reputation: 920
I created new WPF control, added a Rectangle to it, and everyhing works alright, its drawn like it should be. But I just cant paint the rectangle with an actual Image.
BitmapImage bi = GetImage();
ImageBrush imgBrush= new ImageBrush(bi);
this.rectangle.Fill = imgBrush;
But this code just makes the rectangle transparent, except the stroke.
This is the GetImage()
method:
BitmapImage bi;
using (MemoryStream ms = new MemoryStream())
{
bi = new BitmapImage();
bi.CacheOption = BitmapCacheOption.OnLoad;
texture.SaveAsPng(ms, texture.Width, texture.Height);
ms.Seek(0, SeekOrigin.Begin);
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
ms.Close();
}
return bi;
texture
is an Texture2D
class, that is made before this code.
If I return Bitmap
insted of BitmapImage
here and then save that Bitmap
the picture is drawn correctly.
Thank you for your help
Upvotes: 2
Views: 7105
Reputation: 920
This is the correct way to convert Bitmap to
BitmapImage
:
using(MemoryStream memory = new MemoryStream()) { bitmap.Save(memory, ImageFormat.Png); memory.Position = 0; BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = memory; bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.EndInit(); }
Thanks to "Pawel Lesnikowski", he posted the anwser in the following topic:
Load a WPF BitmapImage from a System.Drawing.Bitmap
Upvotes: 4