Esempio n. 1
0
        protected async Task HandleFileSelection(InputFileChangeEventArgs e)
        {
            file          = e.File;
            isFileChanged = true;
            if (file != null)
            {
                var ext = Path.GetExtension(file.Name);
                if (ext.Contains("jpg") || ext.Contains("png") || ext.Contains("jpeg") || ext.Contains("pdf"))
                {
                    msFile = file.OpenReadStream();

                    var resizedImageFile = await file.RequestImageFileAsync("image/png", 100, 100);

                    var buffer = new byte[resizedImageFile.Size];
                    await resizedImageFile.OpenReadStream().ReadAsync(buffer);

                    var imageBase64Data = Convert.ToBase64String(buffer);
                    imageDataURL      = string.Format("data:image/png;base64,{0}", imageBase64Data);
                    isInvalidFileType = false;
                }
                else
                {
                    isInvalidFileType = true;
                    imageDataURL      = string.Empty;
                }
            }
            else
            {
                isInvalidFileType = false;
            }
        }
        public static async Task <byte[]> GetResizedImageAsync(this IBrowserFile file, int size, CancellationToken cancellationToken = default)
        {
            var resizedImage = await file.RequestImageFileAsync("image/jpeg", size, size);

            var buffer = new byte[resizedImage.Size];

            using (var stream = resizedImage.OpenReadStream(MaxImageSize, cancellationToken))
            {
                await stream.ReadAsync(buffer, cancellationToken);
            }

            return(buffer);
        }
Esempio n. 3
0
        private async Task DisplayImages(IBrowserFile imageFile)
        {
            var format = "image/png";

            var resizedImageFile = await imageFile.RequestImageFileAsync(format, 100, 100);

            var buffer = new byte[resizedImageFile.Size];
            await resizedImageFile.OpenReadStream().ReadAsync(buffer);

            var imageDataUrl = $"data:{format};base64,{Convert.ToBase64String(buffer)}";

            ImageDataUrls.Add(imageDataUrl);
            await Task.Delay(1000);

            this.StateHasChanged();
        }
Esempio n. 4
0
        public async Task <ResponseModel> UploadImage(IBrowserFile file, string format)
        {
            var resizedImageFile = await file.RequestImageFileAsync(format, 500, 500);

            var buffer = new byte[resizedImageFile.Size];
            await resizedImageFile.OpenReadStream().ReadAsync(buffer);

            var imageDataUrl  = $"data:{format};base64,{Convert.ToBase64String(buffer)}";
            var serialized    = JsonConvert.SerializeObject(new { base64File = imageDataUrl, fileName = file.Name });
            var stringContent = new StringContent(serialized, Encoding.UTF8, "application/json");
            var response      = await _client.PostAsync($"{_client.BaseAddress}saveJpgImage", stringContent).ConfigureAwait(false);

            var responseData = await response.Content.ReadFromJsonAsync <ImageUrl>();

            return(ResponseUtilities.ResponseValidation(response.StatusCode, responseData.imageUrl));
        }
Esempio n. 5
0
        private static async Task <ModelFile> ConvertToModelFileAsync(IBrowserFile imageFile)
        {
            string format = "image/png";

            IBrowserFile resizedImageFile = await imageFile
                                            .RequestImageFileAsync(format, 100, 100);

            Stream       stream = resizedImageFile.OpenReadStream();
            MemoryStream ms     = new MemoryStream();
            await stream.CopyToAsync(ms);

            stream.Close();

            ModelFile modelFile = new();

            modelFile.ModelContent = ms.ToArray();
            ms.Close();

            return(modelFile);
        }