예제 #1
0
        /// <summary>
        /// 調用WebService (post方式)
        /// </summary>
        /// <param name="strURL"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public string GetWebServicePost(string strURL, string data, PlatformTask onUpload)
        {
            var    client = new WebClient();
            string result = null;

            byte[] sendData = Encoding.GetEncoding("UTF-8").GetBytes(data);

            //byte[] sendData = Season.Current.ReadAllBytes(@"C:\Projects\a.mp4");

            client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            client.Headers.Add("ContentLength", sendData.Length.ToString());

            if (onUpload != null)
            {
                client.UploadProgressChanged += (sender, e) =>
                {
                    //UploadProgressChangedEventArgs
                    onUpload.ParamArray = new string[] { e.ProgressPercentage.ToString() };
                };
            }

            byte[] recData = client.UploadData(strURL, "POST", sendData);

            MemoryStream  stream = new MemoryStream(recData);
            XmlTextReader reader = new XmlTextReader(stream);

            reader.MoveToContent();
            result = reader.ReadInnerXml();
            reader.Close();
            stream.Close();
            //string result = Encoding.ASCII.GetString(response);
            //return result;
            result = result.Replace("&lt;", "<").Replace("&gt;", ">");
            return(result);
        }
예제 #2
0
 ///// <summary>
 ///// 保存用戶鍵值
 ///// </summary>
 ///// <param name="key"></param>
 ///// <param name="o"></param>
 //public void SaveUserValueByKey(string key, object o)
 //{
 //    string[] strings = GetUserFileString("settings.config");
 //    if (strings == null)
 //    {
 //        strings = new string[] { key + "=" + o.ToString() };
 //    }
 //    else
 //    {
 //        bool exist = false;
 //        for (int i = 0; i < strings.Length; i++)
 //        {
 //            var str = strings[i];
 //            if (str.Contains("="))
 //            {
 //                string[] data = str.Split('=');
 //                if (data[0] == key)
 //                {
 //                    exist = true;
 //                    data[1] = o.ToString();
 //                    strings[i] = key + "=" + o.ToString();
 //                }
 //            }
 //        }
 //        if (!exist)
 //        {
 //            strings = strings.Union(new string[] { key + "=" + o.ToString() }).ToArray();
 //        }
 //    }
 //    SaveUserFile("settings.config", String.Join("\r\n", strings));
 //}
 /// <summary>
 /// 保存用戶文件
 /// </summary>
 /// <param name="res"></param>
 /// <param name="bytes"></param>
 public void SaveUserFile(string res, byte[] bytes, PlatformTask action)
 {
     try
     {
         DelUserFiles(new string[] { res }, null);
         //File.WriteAllBytes(UserApplicationDataPath + res, bytes);
         lock (Platform.IoLock)
         {
             using (var isolatedFileStream = File.OpenWrite(UserApplicationDataPath + res))
             {
                 using (var fileWriter = new BinaryWriter(isolatedFileStream))
                 {
                     fileWriter.Write(bytes);
                 }
             }
         }
         if (action != null)
         {
             action.Start();
         }
     }
     catch (Exception ex)
     {
         WebTools.TakeWarnMsg("保存用户文件失败:" + res, "SaveUserFile:bytes" + bytes.Length + UserApplicationDataPath + res, ex);
     }
 }
예제 #3
0
        public override void ChoosePicture(PlatformTask action)
        {
            picStatus = "ChoosePicture";

            //初始化一个OpenFileDialog类
            OpenFileDialog fileDialog = new OpenFileDialog();

            //如果我们要为弹出的选择框中过滤文件类型,可以设置OpenFileDialog的Filter属性。比如我们只允许用户选择.xls文件,可以作如下设置:
            //fileDialog.Filter = "*.jpeg|*.jpg|*.png|*.gif|*.bmp";
            fileDialog.Filter = "JPG格式(*.jpg)|*.jpg|PNG格式(*.png)|*.png|GIF格式(*.gif)|*.gif|BMP格式(*.bmp)|*.bmp";

            //判断用户是否正确的选择了文件
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                //获取用户选择文件的后缀名
                string extension = Path.GetExtension(fileDialog.FileName);
                //声明允许的后缀名
                string[] str = new string[] { ".jpeg", ".jpg", ".png", ".gif", ".bmp" };
                if (!str.Contains(extension.ToLower()))
                {
                    MessageBox.Show("仅能上传jpg,png,gif,bmp格式的图片!");
                }
                else
                {
                    //获取用户选择的文件,并判断文件大小不能超过5000K,fileInfo.Length是以字节为单位的
                    FileInfo fileInfo = new FileInfo(fileDialog.FileName);
                    if (fileInfo.Length > 5000 * 1024)
                    {
                        MessageBox.Show("上传的图片不能大于5000K");
                    }
                    else
                    {
                        byte[] bytes = null;

                        lock (Platform.IoLock)
                        {
                            bytes = File.ReadAllBytes(fileDialog.FileName);
                        }

                        var task = new PlatformTask(() => { });
                        task.OnStartFinish += new AsyncCallback((result) =>
                        {
                            if (action != null)
                            {
                                string avatar = "";
                                if (action.ParamArray != null && action.ParamArray.Length > 0)
                                {
                                    avatar = action.ParamArray[0];
                                }
                                action.ParamArrayResult      = new string[] { avatar + extension };
                                action.ParamArrayResultBytes = task.ParamArrayResultBytes;
                                action.Start();
                            }
                        });

                        ResizeImageFile(bytes, 540 - 6, 540 - 6, true, task); //270 - 6);  //, 76 * 2, 91 * 2);
                    }
                }
            }
        }
예제 #4
0
        public override void MirrorPicture(byte[] image, PlatformTask action)
        {
            RotateFlipType flip = RotateFlipType.RotateNoneFlipX;

            using (MemoryStream ms = new MemoryStream(image))
            {
                Image img = Image.FromStream(ms);

                var bmp = new Bitmap(img);

                using (Graphics gfx = Graphics.FromImage(bmp))
                {
                    gfx.Clear(System.Drawing.Color.White);
                    gfx.DrawImage(img, 0, 0, img.Width, img.Height);
                }

                bmp.RotateFlip(flip);

                var bytes = SavePngFromBitmap(bmp);

                if (action != null)
                {
                    action.ParamArrayResultBytes = bytes;
                    action.Start();
                }
            }
        }
예제 #5
0
        public void DownloadWebData(string file, PlatformTask action)
        {
            var client = new WebClient();

            byte[] result = client.DownloadData(new Uri(file));
            if (action != null)
            {
                action.ParamArrayResultBytes = result;
                action.Start();
            }
        }
예제 #6
0
 /// <summary>
 /// 刪除用戶文件
 /// </summary>
 /// <param name="files"></param>
 public void DelUserFiles(string[] files, PlatformTask action)
 {
     foreach (string file in files)
     {
         if (UserFileExist(new string[] { file.NullToString().Trim() })[0])
         {
             try
             {
                 lock (Platform.IoLock)
                 {
                     File.Delete(UserApplicationDataPath + file.Trim());
                 }
             }
             catch (Exception ex)
             {
                 WebTools.TakeWarnMsg("删除用户文件失败:" + file, "File.Delete:" + UserApplicationDataPath + file, ex);
             }
         }
         if (action != null)
         {
             action.Start();
         }
     }
 }
예제 #7
0
 public virtual void CropResizePicture(byte[] image, int x, int y, int width, int height, int targetSizeWidth, int targetSizeHeight, bool sameRatio, PlatformTask action)
 {
 }
예제 #8
0
 public virtual void ResizeImageFile(byte[] image, int targetSizeWidth, int targetSizeHeight, bool sameRatio, PlatformTask action)
 {
 }
예제 #9
0
 public virtual void CropPicture(byte[] image, int x, int y, int width, int height, PlatformTask action)
 {
 }
예제 #10
0
 public virtual void RotatePicture(byte[] image, int rotate, PlatformTask action)
 {
 }
예제 #11
0
 public virtual void MirrorPicture(byte[] image, PlatformTask action)
 {
 }
예제 #12
0
 public virtual void ChoosePicture(PlatformTask action)
 {
 }
예제 #13
0
        //public byte[] ProcessPictureBytes = null;
        //public SeasonTask ProcessPictureTask = null;

        public virtual void TakePhoto(PlatformTask action)
        {
        }
예제 #14
0
        public override void CropPicture(byte[] image, int x, int y, int width, int height, PlatformTask action)
        {
            // Get your texture
            //Texture2D texture = Content.Load<Texture2D>("myTexture");

            // Calculate the cropped boundary
            Microsoft.Xna.Framework.Rectangle newBounds = new Microsoft.Xna.Framework.Rectangle(x, y, width, height); // avatar.Bounds;
            //newBounds.X -= Convert.ToInt32(posExts.X);
            //newBounds.Y -= Convert.ToInt32(posExts.Y);
            //const int resizeBy = 20;
            //newBounds.X += resizeBy;
            //newBounds.Y += resizeBy;
            //newBounds.Width -= resizeBy * 2;
            //newBounds.Height -= resizeBy * 2;

            // Create a new texture of the desired size
            var croppedPicture = new Texture2D(Platform.GraphicsDevice, newBounds.Width, newBounds.Height);

            // Copy the data from the cropped region into a buffer, then into the new texture
            var data = new Microsoft.Xna.Framework.Color[newBounds.Width * newBounds.Height];

            using (var memo = new MemoryStream(image))
            {
                var tex = Texture2D.FromStream(GraphicsDevice, memo);
                tex.GetData(0, newBounds, data, 0, newBounds.Width * newBounds.Height);
                croppedPicture.SetData(data);
                var memo2 = new MemoryStream();
                croppedPicture.SaveAsPng(memo2, width, height);
                var bytes = memo2.ToArray();
                if (action != null)
                {
                    action.ParamArrayResultBytes = bytes;
                    action.Start();
                }
                //Color[] colors = new Color[] { Color.White };
                //texture = new Texture2D(GraphicsDevice, 1, 1);
                //texture.SetData<Color>(colors);
            }
        }
예제 #15
0
        public override void RotatePicture(byte[] image, int rotate, PlatformTask action)
        {
            RotateFlipType flip = RotateFlipType.RotateNoneFlipNone;

            if (rotate == 0)
            {
            }
            else if (rotate == 1)
            {
                flip = RotateFlipType.Rotate90FlipNone;
            }
            else if (rotate == 2)
            {
                flip = RotateFlipType.Rotate180FlipNone;
            }
            else if (rotate == 3)
            {
                flip = RotateFlipType.Rotate270FlipNone;
            }
            using (MemoryStream ms = new MemoryStream(image))
            {
                Image img = Image.FromStream(ms);

                var bmp = new Bitmap(img);

                using (Graphics gfx = Graphics.FromImage(bmp))
                {
                    gfx.Clear(System.Drawing.Color.White);
                    gfx.DrawImage(img, 0, 0, img.Width, img.Height);
                }

                bmp.RotateFlip(flip);

                ////create an empty Bitmap image
                //Bitmap bmp = new Bitmap(img.Width, img.Height);

                ////turn the Bitmap into a Graphics object
                //Graphics gfx = Graphics.FromImage(bmp);

                ////now we set the rotation point to the center of our image
                //gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);

                ////now rotate the image
                //gfx.RotateTransform(rotationAngle);

                //gfx.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);

                ////set the InterpolationMode to HighQualityBicubic so to ensure a high
                ////quality image once it is transformed to the specified size
                //gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;

                ////now draw our new image onto the graphics object
                //gfx.DrawImage(img, new PointF(0, 0));

                //dispose of our Graphics object
                //gfx.Dispose();

                var bytes = SavePngFromBitmap(bmp);
                if (action != null)
                {
                    action.ParamArrayResultBytes = bytes;
                    action.Start();
                }
            }
        }
예제 #16
0
        /// <summary>
        /// 壓縮圖片
        /// </summary>
        /// <param name="imageFile"></param>
        /// <param name="targetSize"></param>
        /// <returns></returns>
        public override void ResizeImageFile(byte[] imageFile, int targetSizeWidth, int targetSizeHeight, bool sameRatio, PlatformTask action)
        {
            byte[] pic = null;

            try
            {
                using (System.Drawing.Image oldImage = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
                {
                    Size newSize;

                    if (sameRatio)
                    {
                        float scale = GameTools.AutoSetScale(oldImage.Width, oldImage.Height, targetSizeWidth, targetSizeHeight);
                        newSize = new Size(Convert.ToInt32(oldImage.Width * scale), Convert.ToInt32(oldImage.Height * scale));
                    }
                    else
                    {
                        newSize = new Size(Convert.ToInt32(targetSizeWidth), Convert.ToInt32(targetSizeHeight));
                    }

                    using (Bitmap newImage = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format24bppRgb))
                    {
                        using (Graphics canvas = Graphics.FromImage(newImage))
                        {
                            canvas.SmoothingMode     = SmoothingMode.AntiAlias;
                            canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            canvas.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                            canvas.DrawImage(oldImage, new RectangleF(new PointF(0, 0), newSize));
                            MemoryStream m = new MemoryStream();
                            newImage.Save(m, ImageFormat.Jpeg);
                            pic = m.GetBuffer();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                WebTools.TakeWarnMsg("缩放图像失败:" + ex.Message.NullToString(), "ResizeImageFile", ex);
            }

            if (action != null)
            {
                action.ParamArrayResultBytes = pic;
                action.Start();
            }
        }
예제 #17
0
        public override void CropResizePicture(byte[] image, int x, int y, int width, int height, int targetSizeWidth, int targetSizeHeight, bool sameRatio, PlatformTask action)
        {
            byte[] pic = null;
            try
            {
                Microsoft.Xna.Framework.Rectangle newBounds = new Microsoft.Xna.Framework.Rectangle(x, y, width, height);

                var croppedPicture = new Texture2D(Platform.GraphicsDevice, newBounds.Width, newBounds.Height);

                var data = new Microsoft.Xna.Framework.Color[newBounds.Width * newBounds.Height];

                using (var memo = new MemoryStream(image))
                {
                    var tex = Texture2D.FromStream(GraphicsDevice, memo);
                    tex.GetData(0, newBounds, data, 0, newBounds.Width * newBounds.Height);
                    croppedPicture.SetData(data);
                    var memo2 = new MemoryStream();
                    croppedPicture.SaveAsPng(memo2, width, height);
                    pic = memo2.ToArray();
                }

                ResizeImageFile(pic, targetSizeWidth, targetSizeHeight, sameRatio, action);
            }
            catch (Exception ex)
            {
                WebTools.TakeWarnMsg("裁切缩放失败:" + ex.Message.NullToString(), "ResizeImageFile", ex);
            }
        }