locked
Image loading slow into image control first time ? RRS feed

  • Question

  • Hello World

    I am loading images to Generic List of Images from server and display first into image control. I have two buttons below of this image Conrol Next Previous. When user click on Next button i gonna load next image from List not from server because i have all images in my List using a BackGround Thread. 

    this Problem is that when i am loading new image for first time from List it will take some time to load. Once image got loaded and i click on Previous image it loads very fast.

    Please let me know the fatest way to load images from local list...Not from Database 

    Please reply ASAP

    Wednesday, May 16, 2012 12:44 AM

All replies

  • Wednesday, May 16, 2012 3:04 PM
  • I am using Background Worker to load images from Network Path. i am adding Loaded images to a Generic List and whenever user request from a particular image i will fetch from Generic List not from Nextwork.

    here is my code....

    void StartImageAsync()
            {
                BackgroundWorker Worker = new BackgroundWorker();
                //when Worker is start working OnWork is called
                Worker.DoWork += OnWork;
                //when Worker's Progress Changed then ProgressChanged event is called
                Worker.ProgressChanged += ProgressChanged;
                //when Worker's Progress is completed then Completed is called and recall this method as per range
                Worker.RunWorkerCompleted += Completed;
                //Working starting Worker
                Worker.RunWorkerAsync();
            }
    void OnWork(object sender, DoWorkEventArgs e)
            {
                //if this.RequestIndex not equal to zero then call that event 
                if (this.RequestIndex != 0)
                {
                    //downloading image in a queue from network
                    QueueImageRequests(this.RequestIndex);
                }
            }
    
            void ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
    
            }
            /// <summary>
            /// Created By : Ravi Mittal
            /// Created Date : 28 March 2012
            /// purpose : when a single background process is complete then method is called
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void Completed(object sender, RunWorkerCompletedEventArgs e)
            {
                bool callAsync = true;
                //checking the image request generated by
                if (RequestID == RequestFrom.Next || RequestID == RequestFrom.Default)
                {
                    //Min and Max indexed set in DownloadImageAsync method
                    if (this.MinIndex < this.MaxIndex)
                    {
                        this.MinIndex += 1;
                        this.IndexFromLeft = true;  //if request gererated by Left Indexes 
                        this.RequestIndex = this.MinIndex;  //holding the current index value
                        //setting up the MaxIndexBackward and MinIndexBackward 
                        if ((this.MinIndex == this.MaxIndex) && (RequestID == RequestFrom.Default))
                        {
                            //setting the MaxIndexBackward from RightIndexes.Max();
                            this.MaxIndexBackward = this.MaxIndexBackward == 0 ? this.RightIndexes.Max() : this.MinIndexBackward;
                            //setting the MaxIndexBackward from RightIndexes.Min();
                            this.MinIndexBackward = this.RightIndexes.Min() - (defualtNumberOfImages + 1);
                        }
                    }
                    else
                    {
                        if (this.MinIndexBackward < this.MaxIndexBackward)
                        {
                            this.MinIndexBackward += 1;
                            this.IndexFromLeft = false;
                            this.RequestIndex = this.MinIndexBackward;
                            callAsync = this.MinIndexBackward != this.MaxIndexBackward;
                        }
                    }
                }
                else if (RequestID == RequestFrom.Previous)
                {
                    if (this.MinIndexBackward < this.MaxIndexBackward)
                    {
                        this.MinIndexBackward += 1;
                        this.IndexFromLeft = false; //if request gererated by Right Indexes then this is false
                        this.RequestIndex = this.MinIndexBackward;  //holding the current index value
                        callAsync = this.MinIndexBackward != this.MaxIndexBackward;
                    }
                }
                // calling StartImageAsync method to download image from server according to RequestIndex
                if (callAsync) StartImageAsync();
            }
    
            public void QueueImageRequests(int imageIndex)
            {
                //getting the Dispatcher instance from System.Windows.Deployment class
                var dispatcher = System.Windows.Deployment.Current.Dispatcher;
                //making thread to sleep 500 miliseconds
                //Thread.Sleep(500);
                BitmapImage image = null;
                if (dispatcher != null)
                {
                    //Invoking the request to download image from network *
                    dispatcher.BeginInvoke(() =>
                    {
                        //if imageIndex is greater then ImagesPathList.Count then imageIndex will be then same
                        if (imageIndex >= this.ImagesPathList.Count) imageIndex = this.ImagesPathList.Count - 1;
                        //getting image data from ImagesPathList
                        var ImageInfo = (Service.FileImageDetailInfo)ImagesPathList[imageIndex];
                        if (ImageInfo != null)  //if image information is found from list
                        {
                            //if image is avail on network then going to download image from server
                            if ((bool)this.CurrentImageInfo.ImageExists)
                            {
                                //checking if image already exist in list then no need to download it aging
                                if (this.ImageStorage.FirstOrDefault(o => o.ImageName == ImageInfo.ImageName) == null)
                                {
                                    //if imageIndex comes from LeftIndexes then its going to be add in LeftIndexes
                                    if (IndexFromLeft) this.LeftIndexes.Add(imageIndex);
                                    //if imageIndex comes from RightIndexes then its going to be add in RightIndexes
                                    if (!IndexFromLeft) this.RightIndexes.Add(imageIndex);
                                    //getting complete URL of handler 
                                    string handlerURL = this.ApplicationPath + @"Handlers/GetBitmapImage.ashx?FilePath=" + ImageInfo.ImageName + "&FileID=" + this.FileID + "&ProjectName=" + SessionManager.projectName;
                                    //setting the Uri from Bitmap image to get it downloaded
                                    image = new BitmapImage(new Uri(handlerURL, uriKind: UriKind.Absolute));
                                    if (image != null)
                                    {
                                        //if image succesfully get from server then going to add the current image and other image values to list
                                        this.ImageStorage.Add(new ImageStorage { fileID = this.FileID, ImageData = image, ImageIndex = imageIndex + 1, ImageName = ImageInfo.ImageName });
                                    }
                                }
                            }
                        }
                    });
                }
                if (this.ImageStorage != null)
                {
                    this.ImageStorage = new ObservableCollection<ImageStorage>(this.ImageStorage.OrderBy(o => o.ImageIndex).ToList());
                }
            }
    Wednesday, May 16, 2012 11:40 PM
  • Hope you understand my code....

    I am adding images to a List..I am fetching Images from list like below

    private BitmapImage DownloadImage()
            {
                try
                {
                    //makin the SelectedRotation value as zero when user going to select new image
                    this.SelectedRotation = 0;
    
                    //Gleason September.pdf
                    this.PositionContent = string.Format(Resources.Messages.ImageWaitingText, this.CurrentImageIndex, (this.ImagesPathList != null ? this.ImagesPathList.Count : 0));
                    //getting image class object from currentindex
                    this.CurrentImageInfo = (Service.FileImageDetailInfo)ImagesPathList[this.CurrentImageIndex - 1];
                    this.GoToPageIndex = this.CurrentImageIndex;
                    //if image is avail at Physical location then
                    if ((bool)this.CurrentImageInfo.ImageExists)
                    {
    
                        var fileName = this.CurrentImageInfo.ImageName;  //getting image name from ImagesObject
                        this.RotationValue = (int)this.CurrentImageInfo.Rotation; //getting image rotation
                        this.OriginalRotation = Convert.ToInt32(this.CurrentImageInfo.Rotation);
                        //checking the images into cached images list...
                        if ((this.ImageStorage != null) && (this.ImageStorage.Count > 0))
                        {
                            //finding image from list where ImageIndex == CurrentImageIndex
                            var isAvail = this.ImageStorage.FirstOrDefault(o => o.ImageName == this.CurrentImageInfo.ImageName);
                            //if image already exist the cached list then no need to recall the network
                            if (isAvail != null)
                            {
                                //returning image if founded elsewise call the handler to fethc image from network
                                return isAvail.ImageData;
                            }
                        }
                        this.IsBusy = true;
                        if (this.LeftIndexes == null) this.LeftIndexes = new List<int>();
                        if (this.RightIndexes == null)
                        {
                            this.RightIndexes = new List<int>();
                            this.RightIndexes.Add(this.ImagesPathList.Count);
                        }
                        this.LeftIndexes.Add(this.CurrentImageIndex - 1);
                        //calling handler to recieve the image content if image not exist
                        string handlerURL = this.ApplicationPath + @"Handlers/GetBitmapImage.ashx?FilePath=" + fileName + "&FileID=" + this.FileID + "&ProjectName=" + SessionManager.projectName;
                        BitmapImage Image = new BitmapImage(new Uri(handlerURL, uriKind: UriKind.Absolute));
                        return Image;
                    }
                    else
                    {
                        return null;
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
    Wednesday, May 16, 2012 11:43 PM