Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Compressing image in .Net core Blazor before upload

$
0
0

I have a requirement to upload image and then store it on db. I want to compress or resize the image before uploading it to server. I am trying magick.Net but it is not working for me now. Any other method would be really helpful.

My code is as below.

Index.razor page

<FormItem><Template><label for="Remarks" class="k-label k-form-label">@MOM.Alias.Common.DTO.Icon</label><CardImage Src="@ImageSrc" Height="100px" Width="100px" /><TelerikButton Id="btnTicketCategoryRemove" ButtonType="@Telerik.Blazor.ButtonType.Button" Visible="@(ImageSrc!= MOM.UI.Client.Constants.Common.NoImageIcon)" Icon="FontIcon.Trash" Title=@MOM.UI.Client.Constants.Label.Remove ThemeColor="@ThemeConstants.Button.ThemeColor.Error" OnClick="@RemoveIcon" Size="@size" Rounded="@size"></TelerikButton><TelerikFileSelect AllowedExtensions="@Common.AllowAllowedFileTypes"                           OnSelect="@OnSelect" OnRemove="@OnRemove" Multiple=false                           MinFileSize="Common.MinFileSize" MaxFileSize="Common.MaxFileSize" @ref="@telerikFileSelectRef"></TelerikFileSelect><div class="k-form-hint">            @Constants.Label.ExpectedFiles <strong>@Constants.Label.AllowedFileTypes</strong><br />            @Constants.Label.Between <strong>&nbsp;@Constants.Label.MinSizeText</strong>&nbsp; @Constants.Label.And <strong>@Constants.Label.MaxSizeText</strong></div></Template></FormItem>

@code snippet

public async Task OnSelect(FileSelectEventArgs args){    try    {        FileSelectFileInfo file = args.Files[0];        if (!file.InvalidExtension && !file.InvalidMaxFileSize && !file.InvalidMinFileSize)        {            var byteArray = new byte[file.Stream.Length];            var stream = new MemoryStream(byteArray);            await file.Stream.CopyToAsync(stream);            dto.Icon = stream.ToArray();            ImageSrc = CommonHelper.GetImageFromByteArray(dto.Icon);        }                                dialogRef?.Refresh();    }    catch (Exception ex)    {        throw ex;    }}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>