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'.

3
  • have you solved it ? Commented Jul 16, 2015 at 18:41
  • @AbinMathew no... i really need to.. Commented Jul 16, 2015 at 20:19
  • i just added another method to convert please have a look. Commented Jul 16, 2015 at 20:30

2 Answers 2

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;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

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

10 Comments

allright what i do need to do now in order to assign the Imagepath in the c#?
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
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,
if that is the case you can take the answer given by "Schwarzbrot" and set the bitmapImage to the xaml.
its slow as hell bro...
|