简体   繁体   English

无法在 C# 中使用 SixLabors.ImageSharp 处理 .jpg 图像 - 版本 1.0.0-beta0007

[英]unable to process .jpg images using SixLabors.ImageSharp in c# - Version 1.0.0-beta0007

I am unable to save the jpg image using SixLabors.ImageSharp version - 1.0.0-beta0007 in c#, It throws error.我无法在 C# 中使用 SixLabors.ImageSharp 版本 - 1.0.0-beta0007 保存 jpg 图像,它会抛出错误。 Is there any solution to fix the issue beside GIF,PNG,JPEG,BMP format images.除了 GIF、PNG、JPEG、BMP 格式图像之外,是否有任何解决方案可以解决此问题。

Throws Exception:抛出异常:

Image cannot be loaded.无法加载图像。 Available decoders:可用解码器:

  • GIF: GifDecoder GIF:Gif解码器
  • PNG: PngDecoder PNG:PNG解码器
  • JPEG: JpegDecoder JPEG:Jpeg解码器
  • BMP: BmpDecoder BMP:Bmp解码器

Code:代码:

    public string ResizeImage(byte[] imageBytes, int height, int width)
    {
        byte[] image = new byte[] { };

        using (MemoryStream inStream = new MemoryStream(imageBytes))
        {
            using (MemoryStream outStream = new MemoryStream())
            {
                using (Image imageSharp = Image.Load(inStream))
                {
                    imageSharp.Mutate(x => x.Resize(width, height));
                    imageSharp.SaveAsJpeg(outStream);
                    imageSharp.Dispose();
                }

                image = outStream.ToArray();
                outStream.Flush();
                inStream.Flush();
            }
        }
        return Convert.ToBase64String(image);
    }

Make sure the stream position is zero or try to copy the stream to new stream then set the position to zero because sometimes azure blob service close the returned stream确保流位置为零或尝试将流复制到新流然后将位置设置为零,因为有时 azure blob 服务会关闭返回的流

using (MemoryStream stream = new MemoryStream())
using (var ms = new MemoryStream())                                    
await largeBlob.DownloadToAsync(ms);
      ms.Position = 0;
      ms.CopyTo(stream);
      stream.Position = 0;
     var image = Image.Load(stream, out IImageFormat format)

This will be because of a strange thing the AWS does with file uploads you will find the byte[] isn't actually a valid image file (testable by saving the raw byte array out).这是因为 AWS 对文件上传做了一件奇怪的事情,你会发现 byte[] 实际上不是一个有效的图像文件(可以通过保存原始字节数组进行测试)。

This answer from another question should help you reconfigure AWS to allow your code to work.这个来自另一个问题的答案应该可以帮助您重新配置 AWS 以允许您的代码工作。

https://stackoverflow.com/a/56695747/234855 https://stackoverflow.com/a/56695747/234855

I also came across this issue.我也遇到了这个问题。 The code is shown below.代码如下所示。

    byte[] pixelData = new byte[16] { 0, 0 ,0,0,
                                      1,1,1,1,
                                      2,2,2,2,
                                      3,3,3,3};
    Image image = Image.Load(pixelData);
    image.SaveAsBmp("pixelData.bmp");

I don't know how to solve it,either我也不知道怎么解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用C#中的twilio发送短信 - Unable to send SMS using twilio in C# 使用 sendgrid 发送图片 C# - Send images using sendgrid C# 如何使用 C# 通过 AWS lambda 处理 S3 事件上的文件 - How to Process file on S3 event through AWS lambda using C# 错误:验证失败:无法识别“”:版本 .networking.k8s.io/v1beta1 中种类“FrontendConfig”没有匹配项” - Error: validation failed: unable to recognize "": no matches for kind "FrontendConfig" in version "networking.k8s.io/v1beta1" 无法从 function C# 中删除 SftpFile - Unable to delete the SftpFile out of the function C# C# 代码无法连接到 Azure SQL 数据库 - C# code is unable to connect to Azure SQL database 无法通过 C# 连接到 AWS SDK AmazonEC2Client - Unable to connect to AWS SDK AmazonEC2Client through C# 无法将图像文件上传到 Firebase 存储 - Unity C# - Unable to Upload Image file to Firebase Storage - Unity C# 如何使用 C# 扩展 AKS 部署 - how to scale the AKS Deployments using C# 为什么 AWS lambda function 在从 .netlify 开发服务器执行时不返回 jpg/png 图像? - Why is AWS lambda function not returning jpg/png images when executed from netlify dev server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM