Beispiel #1
0
        protected virtual void OnFileChanged()
        {
            controlNumber++;

            fileNameLabel.Text = DriveFile.Name;
            fileSizeLabel.Text = Common.NumberOfBytesToString(DriveFile.Size);
            lblID.Text         = DriveFile.StorageFileId;

            label3.Visible = false;
            widthAndHeightLabel.Visible = false;

            if (!DriveFile.IsImage)
            {
                lock (pictureCancellationTokenSourceLoker)
                {
                    pictureCancellationTokenSource?.Cancel();
                    pictureCancellationTokenSource = new CancellationTokenSource();
                }
                ImageLoaded?.Invoke(this, new EventArgs());
                pictureBox1.BackgroundImage = Resources.no_image;
                Picture = null;
                return;
            }

            // Image preview async loading
            ImageLoading?.Invoke(this, new EventArgs());
            Task.Run(async delegate
            {
                try
                {
                    lock (pictureCancellationTokenSourceLoker)
                    {
                        if (pictureCancellationTokenSource != null)
                        {
                            pictureCancellationTokenSource.Cancel();
                        }
                        pictureCancellationTokenSource = new CancellationTokenSource();
                    }

                    pictureBox1.BackgroundImage = Resources.loading;
                    Picture = null;
                    int cn  = controlNumber;
                    Image image;
                    try
                    {
                        image = await DriveFile.Drive.GetImageAsync(DriveFile, pictureCancellationTokenSource.Token);
                        if (cn != controlNumber)
                        {
                            return;
                        }
                        Invoke(new MethodInvoker(() =>
                        {
                            label3.Visible = true;
                            widthAndHeightLabel.Visible = true;
                            widthAndHeightLabel.Text    = string.Format("{0} x {1}", DriveFile.ImageWidth, DriveFile.ImageHeight);
                            pictureBox1.BackgroundImage = image;
                            Picture = image;
                            Invoke(new MethodInvoker(() =>
                            {
                                ImageLoaded?.Invoke(this, new EventArgs());
                            }));
                            if (widthAndHeightLabel.Text == "0 x 0")
                            {
                                widthAndHeightLabel.Text = string.Format("{0} x {1}", image.Width, image.Height);
                            }
                        }));
                    }
                    catch (System.IO.FileNotFoundException) { }
                    catch (OperationCanceledException) { }
                    catch (BadImageFormatException)
                    {
                        pictureBox1.BackgroundImage = Resources.no_image;
                        Invoke(new MethodInvoker(() =>
                        {
                            ImageLoaded?.Invoke(this, new EventArgs());
                        }));
                    }
                }
                catch (Exception ex)
                {
                    OnError(ex);
                }
            });
        }