c# image showing issue.

  • Thread starter Thread starter elfenliedtopfan55
  • Start date Start date
E

elfenliedtopfan55

Guest
ok so i downloading images of a site got the download with progress bar working to some what sometimes it loads blank. but current issue i am having now is that when i try and load the latest downloaded image it loads blank.

cNMfROJ.png

and this is the code i have for downloading images and displaying it


public partial class Download : XtraForm
{
Image elfenimages = new Image();
public Download()
{
InitializeComponent();
}

private void Download_Load(object sender, EventArgs e)
{
downloadurls();
}


public void downloadurls()
{
backgroundWorker1.RunWorkerAsync();
labelControl2.Text = "Currently Downloading.... " + Sachiko_Res.Name;
labelControl3.Text = Sachiko_Res.Width;
labelControl4.Text = Sachiko_Res.Hight;
DownloadFileWithProgress(Sachiko_Res.Url, Sachiko_Res.elfenliedtopfan5progsettings.Read("DOWNLOADSPATH") + "/" + Sachiko_Res.displayname + "/" + Sachiko_Res.Name, progressBarControl1, labelControl1);


}


private void backgroundWorker1_DoWork_1(object sender, DoWorkEventArgs e)
{
DownloadFileWithProgress(Sachiko_Res.Url, Sachiko_Res.elfenliedtopfan5progsettings.Read("DOWNLOADSPATH") + "/" + Sachiko_Res.displayname + "/" + Sachiko_Res.Name, progressBarControl1, labelControl1);
}

private void DownloadFileWithProgress(string DownloadLink, string TargetPath, DevExpress.XtraEditors.ProgressBarControl progress, DevExpress.XtraEditors.LabelControl labelProgress)
{
//Start Download
// Function will return the number of bytes processed
// to the caller. Initialize to 0 here.
int bytesProcessed = 0;

// Assign values to these objects here so that they can
// be referenced in the finally block
Stream remoteStream = null;
Stream localStream = null;
WebResponse response = null;

// Use a try/catch/finally block as both the WebRequest and Stream
// classes throw exceptions upon error
try
{
// Create a request for the specified remote file name
WebRequest request = WebRequest.Create(DownloadLink);
if (request != null)
{
// Send the request to the server and retrieve the
// WebResponse object

// Get the Full size of the File
double TotalBytesToReceive = 0;
var SizewebRequest = HttpWebRequest.Create(new Uri(DownloadLink));
SizewebRequest.Method = "HEAD";

using (var webResponse = SizewebRequest.GetResponse())
{
var fileSize = webResponse.Headers.Get("Content-Length");
TotalBytesToReceive = Convert.ToDouble(fileSize);
}

response = request.GetResponse();
if (response != null)
{
// Once the WebResponse object has been retrieved,
// get the stream object associated with the response's data
remoteStream = response.GetResponseStream();

// Create the local file

string filePath = TargetPath;


localStream = File.Create(filePath);

// Allocate a 1k buffer
byte[] buffer = new byte[1024];
int bytesRead = 0;

// Simple do/while loop to read from stream until
// no bytes are returned
do
{

// Read data (up to 1k) from the stream
bytesRead = remoteStream.Read(buffer, 0, buffer.Length);

// Write the data to the local file
localStream.Write(buffer, 0, bytesRead);

// Increment total bytes processed
bytesProcessed += bytesRead;


double bytesIn = double.Parse(bytesProcessed.ToString());
double percentage = bytesIn / TotalBytesToReceive * 100;
percentage = Math.Round(percentage, 0);


// Safe Update
//Increment the progress bar
if (progress.InvokeRequired)
{
//progress.PerformStep();
progress.Invoke(new Action(() => UpdateProgressBar(int.Parse(Math.Truncate(percentage).ToString()))));
}
else
{
//progress.PerformStep();
UpdateProgressBar(int.Parse(Math.Truncate(percentage).ToString()));
progress.PerformStep();
}

//Set the label progress Text
if (labelProgress.InvokeRequired)
{
labelProgress.Invoke(new Action(() => labelProgress.Text = int.Parse(Math.Truncate(percentage).ToString()).ToString()));
}
else
{
labelProgress.Text = int.Parse(Math.Truncate(percentage).ToString()).ToString();
}



} while (bytesRead > 0);
}
}
}
catch (Exception ex)
{
// Catch any errors
}
finally
{
// Close the response and streams objects here
// to make sure they're closed even if an exception
// is thrown at some point
if (response != null) response.Close();
if (remoteStream != null) remoteStream.Close();
if (localStream != null) localStream.Close();
}
}
private void progressBarControl1_EditValueChanged(object sender, EventArgs e)
{

}


private void UpdateProgressBar(int value)
{
progressBarControl1.EditValue = value;
}


private void backgroundWorker1_RunWorkerCompleted_1(object sender, RunWorkerCompletedEventArgs e)
{
if (Name.Contains(".png"))
{
elfenimages.setup(Sachiko_Res.Name, Sachiko_Res.Width, Sachiko_Res.Hight, "*.png");
}
else
{
if (Name.Contains(".jpeg"))
{
elfenimages.setup(Sachiko_Res.Name, Sachiko_Res.Width, Sachiko_Res.Hight, "*.jpeg");
}
else
{
if (Name.Contains(".jpg"))
{
elfenimages.setup(Sachiko_Res.Name, Sachiko_Res.Width, Sachiko_Res.Hight, "*.jpg");
}
}
}
elfenimages.Show();

Thread.Sleep(2000);



}


and to load the images

public partial class Image : XtraForm
{

public static string imagetoload { get; set; }
public Image()
{
InitializeComponent();
}

public void setup(string name, string width , string hight , string pattern)
{
this.Text = "Image Downloaded" + name;
this.Width = int.Parse(width);
this.Height = int.Parse(hight);

var dirInfo = new DirectoryInfo(Sachiko_Res.elfenliedtopfan5progsettings.Read("DOWNLOADSPATH") + "/" + Sachiko_Res.displayname + "/");
var image = (from f in dirInfo.GetFiles(pattern) orderby f.LastWriteTime descending select f).First();
imagetoload = image.ToString();

string full = Sachiko_Res.elfenliedtopfan5progsettings.Read("DOWNLOADSPATH") + "/" + Sachiko_Res.displayname + "/" + imagetoload;

pictureBox1.LoadAsync(full);
//cleanup();
//pictureBox1.Image = new Bitmap(full);
}


private void Image_Load(object sender, EventArgs e)
{


//this.Text = "Image Downloaded " + Sachiko_Res.Name;
//this.Width = int.Parse(Sachiko_Res.Width);
//this.Height = int.Parse(Sachiko_Res.Hight);

//string pattern = "*.png";
//var dirInfo = new DirectoryInfo(Sachiko_Res.elfenliedtopfan5progsettings.Read("DOWNLOADSPATH") + "/" + Sachiko_Res.displayname + "/");
//var image = (from f in dirInfo.GetFiles(pattern) orderby f.LastWriteTime descending select f).First();
//imagetoload = image.ToString();
//showimagedownloaded();

}


public void testme()
{
pictureBox1.Image = new Bitmap(Sachiko_Res.elfenliedtopfan5progsettings.Read("DOWNLOADSPATH") + "/" + Sachiko_Res.displayname + "/" + imagetoload);
}

public void showimagedownloaded()
{
string testme = Sachiko_Res.elfenliedtopfan5progsettings.Read("DOWNLOADSPATH") + "/" + Sachiko_Res.displayname + "/" + imagetoload;
pictureBox1.Image = new Bitmap(testme);
//pictureBox1.Load(Sachiko_Res.elfenliedtopfan5progsettings.Read("DOWNLOADSPATH") + "/" + Sachiko_Res.displayname + "/" + imagetoload);
Thread.Sleep(3000);
cleanup();
}
public void cleanup()
{
Thread.Sleep(4000);
pictureBox1.Dispose();
this.Close();
}


but as you can see i cant seem to get the images to work it finds the lasest downloaded image and display it then closes the form after it displays it for 4 seconds but its still not working the way it should it just loads up as blank.


thank you in advance elfenliedtopfan5

Continue reading...
 

Similar threads

Back
Top