Saturday, February 10, 2007

WPF: loading images, audio, video

hmmm. strange that loading images more ma2 fan2 than loading video/audio
must set width/height. tried without and no image : (

loading images:
Image LoadImage(String imageFileName)
{
// Create source
BitmapImage myBitmapImage = new BitmapImage();

// BitmapImage.UriSource must be in a BeginInit/EndInit block
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri("pack://siteoforigin:,,,/" + imageFileName);

// To save significant application memory, set the DecodePixelWidth or
// DecodePixelHeight of the BitmapImage value of the image source to the desired
// height or width of the rendered image. If you don't do this, the application will
// cache the image as though it were rendered as its normal size rather then just
// the size that is displayed.
// Note: In order to preserve aspect ratio, set DecodePixelWidth
// or DecodePixelHeight but not both.
myBitmapImage.EndInit();
Image i = new Image();
i.Source = myBitmapImage;
i.Width = myBitmapImage.Width;
i.Height = myBitmapImage.Height;
return i;
}

loading audio/video:
xaml:
<MediaElement Grid.Row="2" Name="myMedia" LoadedBehavior="Manual"/>

cs:
myMedia.Source = new Uri("mymediafile.wmv", UriKind.Relative);
myMedia.Play();

No comments: