12 Ekim 2017 Perşembe

WPF BitmapImage Sınıfı

Giriş
Winforms'taki Bitmap ile farkının açıklaması şöyle
Image is a base abstract class representing images in GDI+. Bitmap is a concrete implementation of this base class.

BitmapImage is a way to represent an image in a vector based GUI engine like WPF and Silverlight. Contrary to a Bitmap, it is not based on GDI+. It is based on the Windows Imaging Component.

There are ways to load a BitmapImage from a Bitmap.
Constructor - Uri
Şöyle yaparız.
BitmapImage img = new BitmapImage(new Uri("http://www.a.b.c/1.jpg"));
BeginInit metodu
Asenkron yüklemek için şöyle yaparız.
public static async Task<BitmapImage> GetNewImageAsync(Uri uri)
{
  BitmapImage bitmap = null;
  var httpClient = new HttpClient();

  using (var response = await httpClient.GetAsync(uri))
  {
    if (response.IsSuccessStatusCode)
    {
      using (var stream = new MemoryStream())
      {
        await response.Content.CopyToAsync(stream);
        stream.Seek(0, SeekOrigin.Begin);

         bitmap = new BitmapImage();
         bitmap.BeginInit();
         bitmap.CacheOption = BitmapCacheOption.OnLoad;
         bitmap.StreamSource = stream;
         bitmap.EndInit();
         bitmap.Freeze();
       }
    }
  }

  return bitmap;
}

Hiç yorum yok:

Yorum Gönder