Silverlight 图像上传器——救命!
以下是图像调整器/压缩器/上传器的代码主体。这是我的第一个 silverlight 项目,我在线程和 gui 更新以及 webclient 部分方面遇到了麻烦,该部分不起作用。据我所知,调整大小和重新采样似乎执行得很好。问题是gui更新和上传。希望我已经提供了足够的信息让您了解正在发生的事情。
如果您想帮助我,并需要更多信息,我可以根据您的要求通过电子邮件将项目以 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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您在其他问题中所指出的,您确实不想使用
ManualResetEvent
东西。当我有机会时,我会通过重构您的代码来更新这个答案。然而,解决方案的关键项是BackgroundWorker
类。这将帮助您从主 UI 线程中完成所有这些工作。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 theBackgroundWorker
class. This will help you get all this work off of the main UI thread.