Click here to Skip to main content
15,885,937 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
C++
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:

C++
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

1 solution

Try this (warning: will block the UI!):
C#
pictureBox1.WaitOnLoad = true;
pictureBox1.Load ("...");
/ravi
 
Share this answer
 
Comments
Rddezh 24-Dec-12 13:00pm    
It still takes a few moments to load but it is much better. Thank you, very much!
Ravi Bhavnani 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>

/// <returns>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 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900