开发者

Silverlight image Uploader -- help!

开发者 https://www.devze.com 2022-12-18 20:16 出处:网络
The following is the main body of code for an image resizer/compressor/uploader. This is my first silverlight project, and I am having trouble with the threading and gui updating as well as the webcli

The following is the main body of code for an image resizer/compressor/uploader. This is my first silverlight project, and I am having trouble with the threading and gui updating as well as the webclient part, which does not function.The resizing and resampling seem to perform to my knowledge. The problem is the gui updating and uploading. Hopefully I have provided enough information for you to understand what is happening.

If you would like to help me, and require further info, I can email you the project in a zip at your request, minus the sensitive code.

Your help is appreciated.

 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();
    }


As pointed out in your other question you really don't want to be using the ManualResetEvent stuff. I'll update this answer with a refactoring of your code when I get a chance. However the key item to a solution is the BackgroundWorker class. This will help you get all this work off of the main UI thread.

0

精彩评论

暂无评论...
验证码 换一张
取 消