0

I used to work with WFA but no I want to try the WPF platform, too.

I need to assign an existing bitmap to the image control.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    Bitmap sc = screenshot();
    ima.Source = new Bitmap(sc);
 }

this line just not working: ima.Source = new Bitmap(sc);

in picturebox it i can only use this

ima.Image = new Bitmap(sc);

but now its throwing an error:

Cannot implicitly convert type 'System.Drawing.Bitmap' to 'System.Windows.Media.ImageSource'.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

2 Answers2

1

You could also use the Search function: Convert System.Drawing.Bitmap to BitmapImage

private BitmapImage BitmapToImageSource(System.Drawing.Bitmap bitmap)
    {
        Image i = new Image();

        using (MemoryStream memory = new MemoryStream())
        {
            bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
            memory.Position = 0;
            BitmapImage bitmapimage = new BitmapImage();
            bitmapimage.BeginInit();
            bitmapimage.StreamSource = memory;
            bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
            bitmapimage.EndInit();

            i.Source = bitmapimage;

            return bitmapimage;
        }
    }
Community
  • 1
  • 1
ozzy123lel
  • 41
  • 5
0

This will convert the path to ImageSource.

<Image>
    <Image.Source>
         <BitmapImage UriSource="{Binding ImagePath}" />
    </Image.Source>
</Image>

try in this way.

public ImageSource imageSourceForImageControl
{
  get
   {
     ImageSourceConverter c = new ImageSourceConverter();
     return (ImageSource)c.ConvertFrom(yourBitmap);
   }
}

https://msdn.microsoft.com/en-us/library/system.windows.media.imagesourceconverter(v=vs.110).aspx

Abin
  • 2,868
  • 1
  • 23
  • 59
  • allright what i do need to do now in order to assign the Imagepath in the c#? –  Jul 16 '15 at 17:44
  • Create a property in C#/VB called ImagePath, then assign the path to that property. eg: VB Public Property ImagePath() As String Get Return _ImagePath End Get Set(value As String) _ImagePath = value NotifyPropertyChanged("ImagePath") End Set End Property – Abin Jul 16 '15 at 17:48
  • but i dont have here the source.. im just generation an new bitmap using `screenshot` method. it returns a screen shot. its not an local image on the computer or somthing, –  Jul 16 '15 at 17:50
  • if that is the case you can take the answer given by "Schwarzbrot" and set the bitmapImage to the xaml. – Abin Jul 16 '15 at 17:52
  • its slow as hell bro... –  Jul 16 '15 at 17:53
  • how about saving screen shot and take the path ? – Abin Jul 16 '15 at 17:55
  • no no no i use it for screen sharing program –  Jul 16 '15 at 17:57
  • Am happy that it worked for you. – Abin Jul 17 '15 at 13:22
  • it didnt but thanks. i just using `windowsformshost` and added a picturebox to my wpf project... best solution trust me :P –  Jul 17 '15 at 13:23
  • .Net framework 4.5 and above, if you are using then it works for ImageSourceConverter – Abin Jul 17 '15 at 13:25