Esempio n. 1
0
        private void BrowseBgImage()
        {
            try
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Title       = string.Format("Select a image as region background");
                dialog.Multiselect = false;
                dialog.Filter      = "Support Image|*.jpg;*.jpeg;*.png;*.bmp;*.gif";
                var result = dialog.ShowDialog();
                if (result != true)
                {
                    return;
                }
                string strFile = dialog.FileName;
                if (!File.Exists(strFile))
                {
                    return;
                }


                #region   背景图片

                FileInfo fileInfo = new FileInfo(strFile);
                string   strExt   = fileInfo.Extension;
                strExt = strExt.TrimStart('.');
                if (fileInfo.Length > 1024 * 1024 * 5)
                {
                    //图片文件超过5M,不允许上传
                    ShowException(string.Format("Image file too big, can not upload."));
                    return;
                }
                int           length  = (int)fileInfo.Length;
                byte[]        buffer  = File.ReadAllBytes(strFile);
                UploadRequest request = new UploadRequest();
                request.Session = CurrentApp.Session;
                request.Code    = (int)RequestCode.WSUploadFile;
                request.ListData.Add(length.ToString());
                request.ListData.Add("4412");
                request.ListData.Add("1");          //先上传到MediaData临时目录
                request.ListData.Add(string.Empty); //自动生成文件名
                request.ListData.Add(strExt);       //扩展名
                request.Content = buffer;
                Service11012Client client = new Service11012Client(
                    WebHelper.CreateBasicHttpBinding(CurrentApp.Session),
                    WebHelper.CreateEndpointAddress(CurrentApp.Session.AppServerInfo, "Service11012"));
                WebReturn webReturn = client.UploadOperation(request);
                client.Close();
                if (!webReturn.Result)
                {
                    ShowException(string.Format("WSFail.\t{0}\t{1}", webReturn.Code, webReturn.Message));
                    return;
                }
                string strTemp = webReturn.Data;
                if (string.IsNullOrEmpty(strTemp))
                {
                    ShowException(string.Format("Fail.\tFileName empty!"));
                    return;
                }
                mBgFileName = strTemp;

                CurrentApp.WriteLog("UploadBgImage", string.Format("End.\t{0}", mBgFileName));

                string strUrl = string.Format("{0}://{1}:{2}/{3}/{4}",
                                              CurrentApp.Session.AppServerInfo.Protocol,
                                              CurrentApp.Session.AppServerInfo.Address,
                                              CurrentApp.Session.AppServerInfo.Port,
                                              ConstValue.TEMP_DIR_MEDIADATA,
                                              mBgFileName);
                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.UriSource = new Uri(strUrl, UriKind.Absolute);
                bitmap.EndInit();
                ImageBgImage.Source = bitmap;

                Image imgTip = new Image();
                imgTip.Source        = new BitmapImage(new Uri(strUrl, UriKind.Absolute));
                imgTip.Stretch       = Stretch.Uniform;
                ImageBgImage.ToolTip = imgTip;

                #endregion
            }
            catch (Exception ex)
            {
                ShowException(ex.Message);
            }
        }