How to get BitmapImage in codebehind from the image tag in xaml in wpf/silverlight?

Create a WriteableBitmap from the Image control WriteableBitmap bmp = new WriteableBitmap(imagetaginxaml, null); // Load the contents of a MemoryStream from the WritableBitmap MemoryStream m = new MemoryStream(); bmp. SaveJpeg(m, bmp. PixelWidth, bmp.

PixelHeight, 0, 100); // Read from the stream into a new BitmapImage object m. Position = 0; BitmapImage image = new BitmapImage(); image. SetSource(m); // do something with the new BitmapImage object // (for example, load another image control) anotherimagetaginxaml.

Source = image.

Thanks, i'll check – RemotePath Sep 26 at 15:14.

Well, Image. Source is of type ImageSource, there is no quarantee that it will be a BitmapImage, it may be though. If the source is created by the XAML parser it will be a BitmapFrameDecode (which is an internal class).

Anyway, the only save assignment is: ImageSource source = img. Source; otherwise you need to cast: BitmapImage source = (BitmapImage)img. Source; which will throw an exception if the Source is not of this type.So you can either save-cast or try-catch: //(Possibly check for img.

Source! = null first) BitmapImage source = img. Source as BitmapImage; if (source!

= null) { //If img. Source is not null the cast worked. } try { BitmapImage source = (BitmapImage)img.

Source; //If this line is reached it worked. } catch (Exception) { //Cast failed } You could also check the type beforehand using img. SourceisBitmapImage.

Thanks.......... – RemotePath Sep 22 at 0:05 Unable to cast object of type 'System.Windows.Media.Imaging. BitmapFrameDecode' to type 'System.Windows.Media.Imaging. BitmapImage'.

I get this error if I do BitmapImage source = (BitmapImage)img. Source; – RemotePath Sep 22 at 0:10 @RemotePath: As I said, if you try a cast it will throw an exception if the Source is not of that type. BitmapFrameDecode was probably created by the XAML-parser, your cast is invalid.

Read the article about casting that I linked to in my answer if you do not understand how this works. – H.B. Sep 22 at 0:19.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions