使用 Blazor WebAssembly 调整图像大小并上传图像

发布于 2025-01-10 20:37:13 字数 380 浏览 0 评论 0原文

我使用以下示例通过 Blazor WebAssembly 调整上传图像的大小 https://www.prowaretech.com/Computer/Blazor/Examples/WebApi/上传图片

我仍然需要将原始文件转换为 base64,但我不知道如何访问它...... 我试图找到文件的原始宽度和高度以将其传递给 RequestImageFileAsync 函数,但没有成功...... 我需要存储两个文件:原始文件和调整大小的文件。

你能帮我吗?

非常感谢 !

I am using the following sample to resize the uploaded images with Blazor WebAssembly
https://www.prowaretech.com/Computer/Blazor/Examples/WebApi/UploadImages .

Still I need the original file too to be converted to base64 too and I don't know how can I access it...
I tried to find the file's original width and height to pass its to RequestImageFileAsync function but no success...
I need to store both files : the original one and the resized one.

Can you help me, please ?

Thank You Very Much !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无畏 2025-01-17 20:37:13

InputFile 控件发出 IBrowserFile 类型。 RequestImageFileAsync 是 IBrowserFile 上的一种便捷方法,用于调整图像大小和转换类型。结果仍然是一个 IBrowserFile。

完成您所要求的一种方法是使用 SixLabors.ImageSharp。基于 ProWareTech 的例子,类似这样的......

async Task OnChange(InputFileChangeEventArgs e)
    {
        var files = e.GetMultipleFiles(); // get the files selected by the users
        foreach(var file in files)
        {
            //Original-sized file
            var buf1 = new byte[file.Size];
            using (var stream = file.OpenReadStream())
            {
                await stream.ReadAsync(buf1); // copy the stream to the buffer
            }
            origFilesBase64.Add(new ImageFile { base64data = Convert.ToBase64String(buf1), contentType = file.ContentType, fileName = file.Name }); // convert to a base64 string!!
            
            //Resized File
            var resizedFile = await file.RequestImageFileAsync(file.ContentType, 640, 480); // resize the image file
            var buf = new byte[resizedFile.Size]; // allocate a buffer to fill with the file's data
            using (var stream = resizedFile.OpenReadStream())
            {
                await stream.ReadAsync(buf); // copy the stream to the buffer
            }
            filesBase64.Add(new ImageFile { base64data = Convert.ToBase64String(buf), contentType = file.ContentType, fileName = file.Name }); // convert to a base64 string!!
        }
        //To get the image Sizes for first image
        ImageSharp.Image origImage = Image.Load<*imagetype*>(origFilesBase64[0])
        int origImgHeight = origImage.Height;
        int origImgWidth = origImage.Width;

        ImageSharp.Image resizedImage = Image.Load<*imagetype*>(filesBase64[0])
        int resizedImgHeight = resizedImage.Height;
        int resizedImgWidth = resizedImage.Width;
    }

The InputFile control emits an IBrowserFile type. RequestImageFileAsync is a convenience method on IBrowserFile to resize the image and convert the type. The result is still an IBrowserFile.

One way to do what you are asking is with SixLabors.ImageSharp. Based on the ProWareTech example, something like this...

async Task OnChange(InputFileChangeEventArgs e)
    {
        var files = e.GetMultipleFiles(); // get the files selected by the users
        foreach(var file in files)
        {
            //Original-sized file
            var buf1 = new byte[file.Size];
            using (var stream = file.OpenReadStream())
            {
                await stream.ReadAsync(buf1); // copy the stream to the buffer
            }
            origFilesBase64.Add(new ImageFile { base64data = Convert.ToBase64String(buf1), contentType = file.ContentType, fileName = file.Name }); // convert to a base64 string!!
            
            //Resized File
            var resizedFile = await file.RequestImageFileAsync(file.ContentType, 640, 480); // resize the image file
            var buf = new byte[resizedFile.Size]; // allocate a buffer to fill with the file's data
            using (var stream = resizedFile.OpenReadStream())
            {
                await stream.ReadAsync(buf); // copy the stream to the buffer
            }
            filesBase64.Add(new ImageFile { base64data = Convert.ToBase64String(buf), contentType = file.ContentType, fileName = file.Name }); // convert to a base64 string!!
        }
        //To get the image Sizes for first image
        ImageSharp.Image origImage = Image.Load<*imagetype*>(origFilesBase64[0])
        int origImgHeight = origImage.Height;
        int origImgWidth = origImage.Width;

        ImageSharp.Image resizedImage = Image.Load<*imagetype*>(filesBase64[0])
        int resizedImgHeight = resizedImage.Height;
        int resizedImgWidth = resizedImage.Width;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文