Exemplo n.º 1
0
        private async void get_file()
        {
            var picker = new Windows.Storage.Pickers.FileOpenPicker();

            picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
            picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
            picker.FileTypeFilter.Add(".jpg");
            picker.FileTypeFilter.Add(".jpeg");
            picker.FileTypeFilter.Add(".png");

            StorageFile file = await picker.PickSingleFileAsync();

            if (file != null)
            {
                var inputStream = await file.OpenSequentialReadAsync();

                var readStream = inputStream.AsStreamForRead();
                var byteArray  = new byte[readStream.Length];
                await readStream.ReadAsync(byteArray, 0, byteArray.Length);

                cloureImage    = new CloureImage(file.Name, byteArray);
                imgLogo.Source = await cloureImage.GetBitmapImage();
            }
        }
        private async void get_file()
        {
            var picker = new Windows.Storage.Pickers.FileOpenPicker();

            picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
            picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
            picker.FileTypeFilter.Add(".jpg");
            picker.FileTypeFilter.Add(".jpeg");
            picker.FileTypeFilter.Add(".png");

            if (CloureManager.getAccountType() == "free" || CloureManager.getAccountType() == "test_free")
            {
                StorageFile file = await picker.PickSingleFileAsync();

                if (file != null)
                {
                    images = new List <CloureImage>();
                    var inputStream = await file.OpenSequentialReadAsync();

                    var readStream = inputStream.AsStreamForRead();
                    var byteArray  = new byte[readStream.Length];
                    await readStream.ReadAsync(byteArray, 0, byteArray.Length);

                    CloureImage cloureImage = new CloureImage(file.Name, byteArray);

                    IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);

                    BitmapImage image = new BitmapImage();
                    image.SetSource(stream);
                    cloureImage.ImageSrc = image;

                    images.Add(cloureImage);

                    lstImages.ItemsSource = null;
                    lstImages.ItemsSource = images;
                }
            }
            else
            {
                IReadOnlyList <StorageFile> files = await picker.PickMultipleFilesAsync();

                foreach (StorageFile file in files)
                {
                    if (file != null)
                    {
                        var inputStream = await file.OpenSequentialReadAsync();

                        var readStream = inputStream.AsStreamForRead();
                        var byteArray  = new byte[readStream.Length];
                        await readStream.ReadAsync(byteArray, 0, byteArray.Length);

                        CloureImage cloureImage = new CloureImage(file.Name, byteArray);

                        IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);

                        BitmapImage image = new BitmapImage();
                        image.SetSource(stream);
                        cloureImage.ImageSrc = image;

                        images.Add(cloureImage);

                        //lstImages.ItemsSource = null;
                        //lstImages.ItemsSource = images;
                    }
                }
            }

            lstImages.ItemsSource = null;
            lstImages.ItemsSource = images;
        }
Exemplo n.º 3
0
        public static async Task <string> ExecuteAsync(List <CloureParam> cparams)
        {
            string Response = "";

            //string url_str = "https://cloure.com/api/v1/";
            string url_str = "http://cloure.test/api/v1/";
            MultipartFormDataContent dataContent = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture));
            HttpClient httpClient = new HttpClient();

            httpClient.BaseAddress = new Uri(url_str);
            httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("utf-8"));

            if (appToken != null)
            {
                if (appToken.Length == 32)
                {
                    cparams.Add(new CloureParam("app_token", appToken));
                }
            }
            if (userToken != null)
            {
                if (userToken.Length == 32)
                {
                    cparams.Add(new CloureParam("user_token", userToken));
                }
            }

            cparams.Add(new CloureParam("language", lang));
            cparams.Add(new CloureParam("referer", "uwp"));
            cparams.Add(new CloureParam("referer_version", cloureClient.BuildVersion));

            try
            {
                foreach (CloureParam param in cparams)
                {
                    if (param != null)
                    {
                        if (param.name == "module")
                        {
                            url_str += param.value + "/";
                        }
                        if (param.name == "topic")
                        {
                            url_str += param.value;
                        }

                        if (param.value.GetType() == typeof(string))
                        {
                            HttpContent httpContent = new StringContent((string)param.value);
                            dataContent.Add(httpContent, param.name);
                        }
                        else if (param.value.GetType() == typeof(double))
                        {
                            HttpContent httpContent = new StringContent(((double)param.value).ToString("F2", CultureInfo.InvariantCulture));
                            dataContent.Add(httpContent, param.name);
                        }
                        else if (param.value.GetType() == typeof(int))
                        {
                            HttpContent httpContent = new StringContent(((int)param.value).ToString());
                            dataContent.Add(httpContent, param.name);
                        }
                        else if (param.value.GetType() == typeof(CloureImage))
                        {
                            string      option      = "api_image_object";
                            CloureImage cloureImage = (CloureImage)param.value;
                            HttpContent httpContent = new StringContent("");

                            if (option == "byte")
                            {
                                httpContent = new ByteArrayContent(cloureImage.GetBytes());
                                dataContent.Add(httpContent, param.name, "image");
                            }
                            else if (option == "base64")
                            {
                                string base64String = Convert.ToBase64String(cloureImage.GetBytes());
                                httpContent = new StringContent(base64String);
                                dataContent.Add(httpContent, param.name);
                            }
                            else if (option == "api_image_object")
                            {
                                httpContent = new StringContent(cloureImage.ToJSONString());
                                dataContent.Add(httpContent, param.name);
                            }
                        }
                        else if (param.value.GetType() == typeof(List <CloureImage>))
                        {
                            List <CloureImage> cloureImages = (List <CloureImage>)param.value;
                            string             str_content  = "[";
                            foreach (CloureImage cloureImage in cloureImages)
                            {
                                str_content += cloureImage.ToJSONString() + ",";
                            }
                            str_content  = str_content.TrimEnd(',');
                            str_content += "]";
                            HttpContent httpContent = new StringContent(str_content);
                            dataContent.Add(httpContent, param.name);
                        }
                        else if (param.value.GetType() == typeof(bool))
                        {
                            bool value = (bool)param.value;
                            if (value)
                            {
                                HttpContent httpContent = new StringContent("1");
                                dataContent.Add(httpContent, param.name);
                            }
                            else
                            {
                                HttpContent httpContent = new StringContent("0");
                                dataContent.Add(httpContent, param.name);
                            }
                        }
                    }
                }

                HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(url_str, dataContent);

                if (httpResponseMessage.IsSuccessStatusCode)
                {
                    Response = await httpResponseMessage.Content.ReadAsStringAsync();
                }
            }
            catch (Exception e)
            {
                Response = e.Message.ToString();
                var dialog = new MessageDialog(Response);
                await dialog.ShowAsync();
            }

            return(Response);
        }