In depth analysis of WPF client to read the hd picture card and thumbnail solution

  • 2020-05-10 18:43:02
  • OfStack

On Ftp uploads, someone has uploaded hd images, each of which is about 2M.
If you use the traditional BitmapImage class and then bind the Source property, some computers will compare CARDS for the first time, one computer for 10 seconds, and four big appointment CARDS for 40 seconds.

So I download the image asynchronously, get the downloadFileStream object, and then bind to the BitmapImage class. Such as:
System.Windows.Controls.Image photo = new Image
{
      Width = 100,
      Height = 100,
      Margin = new Thickness(2),
      Stretch = Stretch.Uniform
};

BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = downloadFileStream;
bitmap.EndInit();

photo.Source = bitmap;

ListBoxItem lbi = new ListBoxItem()
{
      DataContext = pvo,
      Content = photo
};

this.lbPhotoes.Items.Add(lbi);

Because bitmap's StreamSource is larger, the lbi object is larger, so the lbPhotoes.Items.Add method will get stuck for about 30 seconds after adding two images.

So try to make BitmapImage's objects smaller using thumbnails, which are used here because the client needs the image to be roughly the same size
(100100).

The complete code is as follows:
System.Windows.Controls.Image photo = new Image
{
      Width = 100,
      Height = 100,
      Margin = new Thickness(2),
      Stretch = Stretch.Uniform
};

using (System.Drawing.Image drawingImage = System.Drawing.Image.FromStream(downloadFileStream))
{
using (System.Drawing.Image thumbImage =
drawingImage.GetThumbnailImage(100, 100, () = > { return true; }, IntPtr.Zero))
      {
              MemoryStream ms = new MemoryStream();
              thumbImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

              BitmapFrame bf = BitmapFrame.Create(ms);
              photo.Source = bf;
      }
}

ListBoxItem lbi = new ListBoxItem()
{
      DataContext = pvo,
      Content = photo
};

this.lbPhotoes.Items.Add(lbi);

In this case, System.Drawing.dll. thumbImage is obtained using the GetThumbnailImage method of System.Drawing.Image class, MemoryStream is used to save stream of the thumbnail, and stream of the thumbnail is used to generate the image.

 
Although this problem has been solved, it takes a lot of time to download the hd image and generate the thumbnail every time, so it is time to generate the thumbnail when uploading the image and save the thumbnail. Because in the local area network, the network speed is fast, this kind of way also can satisfy the requirement basically.

Related articles: