Click here to Skip to main content
11,709,128 members (58,169 online)
Rate this: bad
good
Please Sign up or sign in to vote.
I wrote a program, that shows my images and their data (date, camera model, ISO, ...). If there is a GPS tag it also show's the map, where the picture was taken.

The problem:

The picture show's up after all data has been loaded, including the map.

But I want to get the picture as soon as possible and the data can show's up later.
I tried to solve the problem with backgroundWorker, but it doesn' work.

Here is the code for loading the picture:
pictureBox1.ImageLocation = photosPath + "\\" + listView1.Items[CurPic-1].Text;
this.Refresh();        //I also tried to refresh the form, picture box, ...
pictureBox1.Refresh();
Application.DoEvents();
lblName.Text = listView1.Items[CurPic - 1].Text; //the name of the current pic
lblGPS.Text = "/";          //In case, there is no GPS tag - write /
loadData.RunWorkerAsync();

The background worker calls two methods:

private void loadData_DoWork(object sender, DoWorkEventArgs e)
        {
            show_info(pictureBox1.ImageLocation);   //loading data
            showlocation(getlocation(pictureBox1.ImageLocation));   //getting location and showing map
        }

This code works, just the same as without background worker. Is there any other way to solve this. Please help.
Posted 24-Dec-12 6:23am
Rddezh406

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this (warning: will block the UI!):
pictureBox1.WaitOnLoad = true;
pictureBox1.Load ("...");
/ravi
  Permalink  
Comments
Rddezh at 24-Dec-12 13:00pm
   
It still takes a few moments to load but it is much better. Thank you, very much!
Ravi Bhavnani at 24-Dec-12 13:11pm
   
You may want to consider having a background thread load images (into Image objects), then set the loaded Image into the PictureBox. This has 2 advantages: (1) your UI will not be locked and (2) the image file will not be locked by the .NET PictureBox. See this method:

///

/// Loads an image from a file without locking the file.

///


/// <param name="filename">The filename.</param>

/// The image.

public static Image ImageFromFile (string filename)

{

using (FileStream stream = new FileStream (filename, FileMode.Open, FileAccess.Read)) {

return Image.FromStream (stream); }

}/ravi

PS: Please rate my answer if you find it useful. Thanks.
Rddezh at 24-Dec-12 13:17pm
   
Right now, I'm making a class Photo. In this class I have all data about image and the image itself.

Then I will create an array of this class. It will hold 3 images - current one, the next one and the previous one. After moving to another picture I will be able to load the image directly from the class and load the next one in the background.

So thanks for all help.
Ravi Bhavnani at 24-Dec-12 13:22pm
   
Cool! I've been building something similar (a basic photo cataloger). Will post an article on CP one of these days.

/ravi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 580
1 OriginalGriff 254
2 Maciej Los 235
3 Peter Leow 219
4 Mika Wendelius 160
0 OriginalGriff 9,348
1 Sergey Alexandrovich Kryukov 8,727
2 CPallini 5,189
3 Maciej Los 4,991
4 Mika Wendelius 3,856


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 24 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100