Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Wednesday, September 29, 2010 12:10:29 AM(UTC)
Benjamin

Groups: Registered
Posts: 16


I tried to cancel the load process but it does not work.
I set the cancel property to true but the Codecs.LoadImage event continue to be raise until the end of my file is loading.
I checked the CodecsLoadImageEventArgs.Cancel property at the next raise and he is set to true before seting him again to true. Why the loading process does not stop? Does it only work with multiple page file? I'am loading a one page tiff file.

Here is my class :

    internal class ImageLoader
    {
        //Codecs for loading images.
        private RasterCodecs _codecs;

        //Thread safe boolean. Allow to abort the loading process.
        private volatile bool _canceled = false;

        //Thread for loading images.
        private Thread _thread;

        //Get the state of the loading process.
        public bool Finished
        {
            get
            {
                return (this._thread == null || this._thread.ThreadState == ThreadState.Stopped);
            }
        }

        //Raise when the loading process is finished
        public event EventHandler<LoadFinishedEventArgs> LoadFinished;
       
       //Raise while the image is loading.
        public event EventHandler<LoadProgressEventArgs> LoadProgress;

        //Default constructor.
        public ImageLoader()
        {
            //Creating the codecs instance.
            this._codecs = new RasterCodecs();
            this._codecs.LoadImage += new EventHandler<CodecsLoadImageEventArgs>(_codecs_LoadImage);
        }
      
        //Call by the _thread object. Load an image from a path.
        private void LoadImageFromPath(object arg)
        {
            //Loading the image from path.
            RasterImage loadedImage = this._codecs.Load((string)arg);
           
            //If the user abort.
            if (this._codecs.LoadStatus == RasterExceptionCode.UserAbort)
            {
                return;
            }

            //the image is loaded.
            LoadFinishedEventArgs args = new LoadFinishedEventArgs()
            {
                Image = loadedImage
            };

            OnLoadFinished(args);
        }

        //Call by the Codecs object while the image is loading.
        private void _codecs_LoadImage(object sender, CodecsLoadImageEventArgs e)
        {
                if (_canceled)
                    e.Cancel = true;
            LoadProgressEventArgs args = new LoadProgressEventArgs()
            {
                Progression = ((e.Row * 100) / (e.Info.Height - 1))
            };
            this.OnLoadProgress(args);
        }
      
        //Safe raising the LoadProgress event.
        protected void OnLoadProgress(LoadProgressEventArgs args)
        {
            EventHandler<LoadProgressEventArgs> handler = this.LoadProgress;

            if (handler != null)
            {
                handler(null, args);
            }
        }

        //Safe raising the LoadFinished event.
        protected void OnLoadFinished(LoadFinishedEventArgs args)
        {
            EventHandler<LoadFinishedEventArgs> handler = this.LoadFinished;

            if (handler != null)
            {
                handler(null, args);
            }
        }

        //Load an image from a path
        public void LoadImage(string path)
        {
            //Cancel the current loading process.
             if (!this.Finished)
            {
                this.Cancel();
                this._thread.Join();
            }
           
            //Load a new image from a path.
            this._thread = new Thread(new ParameterizedThreadStart(this.LoadImageFromPath));
            this._thread.Start(path);
        }

        //Cancel the loading process.
        public void Cancel()
        {
            _canceled = true;
        }
    }

Here is a sample form that use the ImageLoader class

    delegate void LoadFinishedDelegateHandler();

    delegate void LoadProgressDelegateHandler(int progression);

    public partial class ImageViewerForm : Form
    {
        //Load images in a thread.
        private ImageLoader _loader;

       //Current image.
       private RasterImage _image;      

        //Delegate for the end of the thread loading.
        private LoadFinishedDelegateHandler _loadFinishedDelegate;

        //Delegate for the thread load progress.
        private LoadProgressDelegateHandler _loadProgressDelegate;

       //Default constructor
        public ImageViewerForm()
        {
            InitializeComponent();

            //Leadtools Unlock
            RasterSupport.Unlock(RasterSupportType.Document, "xxxxxxxxx");
            RasterCodecs.Startup();

            //Delegate initialization.
            _loadFinishedDelegate = new LoadFinishedDelegateHandler(FillViewer);
            _loadProgressDelegate = new LoadProgressDelegateHandler(UpdatePGB);

            //Image loader
            _loader = new ImageLoader();
            _loader.LoadFinished += new EventHandler<LoadFinishedEventArgs>(_loader_LoadFinished);
            _loader.LoadProgress += new EventHandler<LoadProgressEventArgs>(_loader_LoadProgress);

            _loader.LoadImage("PATH OF AN IMAGE");
        }

        //Stop the load process if the form is closing.
       //Thread.Join() doesn't work properly so i cancel the closing, only for trying
        private void ImageViewerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!this._loader.Finished)
            {
                this._loader.Cancel();
                e.Cancel = true;
            }
        }
       
        //Raise when the image is loaded
        private void _loader_LoadFinished(object sender, LoadFinishedEventArgs e)
        {
            if (!this.IsDisposed)
            {
                this._image = e.Image;
                //Thread safe
                this.Invoke(this._loadFinishedDelegate);
            }
        }

        //Raise while the image is loading
        private void _loader_LoadProgress(object sender, LoadProgressEventArgs e)
        {
            if (!this.IsDisposed)
            {
                object[] param = new object[1];
                param[0] = e.Progression;
                //Thread safe
                this.Invoke(this._loadProgressDelegate, param);
            }
        }

        //Upadate a Progress Bar
        private void UpdatePGB(int progression)
        {
            this.PGB_loadProgress.Value = progression;
        }

        //Fill a Raster Image Viewer
        private void FillViewer()
        {
            this.RIV_viewer.Image = this._image;
        }
    }
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

#2 Posted : Wednesday, September 29, 2010 5:28:00 AM(UTC)

Adnan Ismail  
Guest

Groups: Guests
Posts: 3,022

Was thanked: 2 time(s) in 2 post(s)

I tried aborting while loading it worked for several file formats, such as JPEG, PNG, J2K and TIFF.
I'm attaching a demo that aborts at around 30%. It is a modified copy of our PaintWhileLoadDemo.
To use it, you must copy it to a folder inside "LEADTOOLS 17\Examples\DotNet\CS\"
File Attachment(s):
PaintWhileLoadDemo_Abort.zip (11kb) downloaded 25 time(s).
 
#3 Posted : Wednesday, September 29, 2010 6:06:34 AM(UTC)
Benjamin

Groups: Registered
Posts: 16


I'am working with LeadTools 16.5 so i am not able to launch your apps cause I don't have the app.config file. I don't want to download and install LeadTools 17 free trial.
I have take a look at your source code and I do exactly the same things for stoping the load process. why the RasterCodecs don't stop the loading process in my case ??? Does it's  a LeadTools V16.5 bug???
I'am running out of time for my project. Please, help me to find why this doesn't work in my case.
 
#4 Posted : Wednesday, September 29, 2010 10:10:23 PM(UTC)

Adnan Ismail  
Guest

Groups: Guests
Posts: 3,022

Was thanked: 2 time(s) in 2 post(s)

I modified the project to use LEADTOOLS v16.5. To be able to run the project, please unpack the attached file into this folder location:
[LEADTOOLS 16.5 folder]\Examples\DotNet\CS\

I am using the latest LEADTOOLS v16.5 SDK patches released on September 8. If you are using an older patch, please send an email to support@leadtools.com and mention the following detail in your email:

1- Your LEADTOOLS v16.5 SDK serial number. (Do NOT post it here)
2- A link to this forum post.
File Attachment(s):
PaintWhileLoadDemo_Abort.zip (13kb) downloaded 26 time(s).
 
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.121 seconds.