1

以下是圖像縮放器/壓縮器/上傳器的主體代碼。這是我的第一個Silverlight項目,我在線程和GUI更新以及webclient部分中都遇到了問題,該部分無法正常工作。調整大小和重新採樣似乎在我的知識中執行。問題是gui更新和上傳。希望我已經提供了足夠的信息,讓您瞭解正在發生的事情。Silverlight圖片上傳器 - 幫助!

如果您想幫助我,並且需要更多信息,我可以根據您的要求以zip的形式通過電子郵件將您的項目發送給您,減去敏感代碼。

您的幫助表示讚賞。

namespace ulx 
{ 
    public partial class Page : UserControl 
{ 

    public Page() 
    { 
     InitializeComponent(); 
     //Import images from file browse dialog 
     this.Browse.Click += ImportFiles; 
     this.thw.Text = "0"; 
     this.thh.Text = "0"; 
     this.rh.Text = "0"; 
     this.rw.Text = "0"; 
     this.Exec.IsEnabled = false; 
     //Invoke the resize/compressions/uploader sequence 
     this.Exec.Click += ExecP; 
     this.mProgressBar.Visibility = Visibility.Collapsed; 
     this.transc.Visibility = Visibility.Collapsed; 
    } 

    public FileInfo [] Files; 


    //select jpegs  
    public void ImportFiles(object sender,RoutedEventArgs e) 
    { 
     OpenFileDialog openDialog = new OpenFileDialog(); 
     openDialog.Filter = "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg"; 
     openDialog.Multiselect = true; 
     if (openDialog.ShowDialog().GetValueOrDefault(false)) 
     { 
      this.Files = (FileInfo [])openDialog.Files;    
      this.Imgcount.Text = "Image Count: "+this.Files.Length; 

      this.Exec.IsEnabled = true; 
     } 
    } 


    public void ExecP(object Sender, RoutedEventArgs e) 
    { 
     //the main process 
     this.Process(); 

    } 

    public void Process() 
    { 
      Configuration.thw = Convert.ToInt32(this.thw.Text); 
      Configuration.thh = Convert.ToInt32(this.thh.Text); 
      Configuration.rh = Convert.ToInt32(this.rh.Text); 
      Configuration.rw = Convert.ToInt32(this.rw.Text); 
      Configuration.dest = this.Dest.Text; 
      if (Configuration.dest == "") 
      { 
       System.Windows.Browser.HtmlPage.Window.Alert("Invalid Dest:"); 
      } 
      if (Configuration.thw == 0 || Configuration.thh == 0 || Configuration.rw == 0 || Configuration.rh == 0) 
      { 
       System.Windows.Browser.HtmlPage.Window.Alert("Invalid Resize Definitions"); 
       return; 
      } 
      //hide and disable a bunch of gui controls 
      this.Browse.IsEnabled = false; 
      this.thw.Visibility = Visibility.Collapsed; 
      this.thh.Visibility = Visibility.Collapsed; 
      this.rw.Visibility = Visibility.Collapsed; 
      this.rh.Visibility = Visibility.Collapsed; 
      this.c1.Visibility = Visibility.Collapsed; 
      this.c2.Visibility = Visibility.Collapsed; 
      this.c3.Visibility = Visibility.Collapsed; 
      this.c4.Visibility = Visibility.Collapsed; 
      this.Dest.Visibility = Visibility.Collapsed; 
      this.transc.Visibility = Visibility.Visible; 
      //enable and show progressbar 
      this.mProgressBar.Visibility = Visibility.Visible; 
      this.mProgressBar.Minimum = 0; 
      this.mProgressBar.Maximum = this.Files.Length * 2; 
      int i = 0; 
      foreach (FileInfo file in this.Files) 
      { 
       //Resize thumb 
      WriteableBitmap wb = null; 
       this.transc.Text = "Creating Thumb (" + Configuration.thw + "," + Configuration.thh + ") :" + file.Name; 

       using (FileStream stream = file.OpenRead()) 
       { 
        wb = ulxConverter.Imager.GetImageSource(stream, Configuration.thw, Configuration.thh); 
       } 
       string text = ""; 
       // compress thumb 
       using (Stream Source = ulxUtil.JpgEncoder.Encode(wb, 50)) 
       { 
        StreamReader reader = new StreamReader(Source); 
        text = reader.ReadToEnd(); 
       } 
       //upload thumb 
       Thread.Sleep(1); 
this.transc.Text = "Uploading Thumb: " + file.Name; 
       Uploader tu = new Uploader("/cgi-bin/SIDES/ulx.cgi", "user", "pass", Configuration.dest, "" + file.Name, 1, text); 
       Thread myThread = new Thread(new ThreadStart(tu.doit)); 
       myThread.Start(); 
this.mProgressBar.Value = i++; 
this.transc.Text = "Creating Resample (" + Configuration.rw + "," + Configuration.rh + ") :" + file.Name; 
       wb = null; 
       //create resample 
       using (FileStream stream = file.OpenRead()) 
       { 
        wb = ulxConverter.Imager.GetImageSource(stream, Configuration.rw, Configuration.rh); 
       } 
       Thread.Sleep(1); 
       text = ""; 
       //compress resample 
       using (Stream Source = ulxUtil.JpgEncoder.Encode(wb, 20)) 
       { 
        StreamReader reader = new StreamReader(Source); 
        text = reader.ReadToEnd(); 
       } 
       Thread.Sleep(1); 
       //uploud resample 
       this.transc.Text = "Uploading Resample: " + file.Name; 
       Uploader ru = new Uploader("/cgi-bin/SIDES/ulx.cgi", "user", "pass", Configuration.dest, file.Name, 0, text); 

       Thread myThreadT = new Thread(new ThreadStart(ru.doit)); 
       myThreadT.Start(); 
       this.mProgressBar.Value = i++; 
       //end of loop 
      } 

      //ok, we are done. hide progress and show hidden controls 
     this.mProgressBar.Visibility = Visibility.Collapsed; 
      this.transc.Visibility = Visibility.Collapsed; 
      this.Browse.IsEnabled = true; 
      this.thw.Visibility = Visibility.Visible; 
      this.thh.Visibility = Visibility.Visible; 
      this.rw.Visibility = Visibility.Visible; 
      this.rh.Visibility = Visibility.Visible; 
      this.c1.Visibility = Visibility.Visible; 
      this.c2.Visibility = Visibility.Visible; 
      this.c3.Visibility = Visibility.Visible; 
      this.c4.Visibility = Visibility.Visible; 
      this.Dest.Visibility = Visibility.Visible; 
    } 
    } 

public static class Configuration 
{ 
    public static int thw; 
    public static int thh; 
    public static int rw; 
    public static int rh; 
    public static string src; 
    public static string dest; 
} 



public class Uploader 
{ 
    public string url; 
    public string user; 
    public string pass; 
    public string path; 
    public string source;  
    public int MODE; 
    public string fn;  
    public string imgdata; 
    private bool is_mac_unix = false; 

    public Uploader(string url,string user,string pass,string path,string fn,int MODE,string imgdata) 
    { 
     this.url = url; 
     this.user = user; 
     this.pass = pass; 
     this.path = path; 
     this.fn = fn; 
     this.MODE = MODE; 
     this.imgdata = imgdata; 

     string t = ""+Environment.OSVersion; 
     if(t.IndexOf("x") != -1) 
     { 
      this.is_mac_unix = true; 
     } 

    } 

    private ManualResetEvent mre = new ManualResetEvent(false); 

    public void doit() 
    { 
     //string BODY = "datastring"; 
     WebClient sender = new WebClient(); 
     sender.OpenReadCompleted += new OpenReadCompletedEventHandler(this.ReadComplete); 
     sender.OpenReadAsync(new Uri(this.url + "?blob=" + this.imgdata,UriKind.Absolute)); 
     mre.WaitOne(); 
    } 
    public bool T = false; 


    public void ReadComplete(object sender, OpenReadCompletedEventArgs e) 
    { 
     if (e.Error == null) 
     { 
      this.T = true; 
     } 
     mre.Set(); 
    } 
+0

評論代碼請提供。 – 2010-02-03 19:56:50

+0

提示:你應該考慮使用string.IsNullOrEmpty(....)而不是mystring ==「」 – 2010-02-03 20:00:38

+0

提示:你應該考慮使用mystring = string.Empty代替mystring =「」 – 2010-02-03 20:01:26

回答

1

正如你在其他問題中指出的,你真的不想使用ManualResetEvent的東西。當我有機會時,我會通過重構代碼來更新這個答案。然而,解決方案的關鍵是BackgroundWorker類。這將幫助您從主UI線程中獲得所有這些工作。