WPF Світлина

2075 / WPF / Світлина

 

XAML

<Image x:Name="myImage" Source="Images/1.jpg" />

// GIF - не програється

 

C#

Файл з диску

Image1.Source = new BitmapImage(new Uri("1.jpg"));
Image1.Source = null; // відсутнє

 

Файл з ресурсів

BitmapImage BitmapToImageSource(Bitmap bitmap)
{
 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();

   return bitmapimage;
 }
}

img1.Source = BitmapToImageSource(Properties.Resources._1);

 

Випадкове фото

int x = r.Next(1, 6);
img1.Source = BitmapToImageSource(
 (System.Drawing.Bitmap)Properties.Resources.ResourceManager.GetObject("_" + x)
);

 

Коли фото на формі

img1.Source = new BitmapImage(new Uri("a.png", UriKind.Relative));