微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从内存流创建新的Image对象无法在生产中使用,但可以在本地使用

如何解决从内存流创建新的Image对象无法在生产中使用,但可以在本地使用

目标:我目前正在从javascript提取文件,将其转换为字节数组的base64编码字符串,并将其传递给我的.net控制器。 然后,我通过将其转换回字节数组,为其创建一个新的内存流,从该内存流创建一个Image对象,更改需要更改的内容,然后将其带回到一个内存流中来进行处理。然后上传到我的服务器。

问题: 目前,我能够像以前提到的那样从JavaScript编码中获取图像,然后将其上传到服务器上没有问题,但是当我尝试将内存流转换为图像时,问题就来了(以便我可以调整大小)它)。我不确定为什么,但是在我的本地计算机上,它没有问题,也没有抛出任何错误,并且可以完美地上传,调整大小并上传所有内容。但是,在我的linux服务器上,它在尝试从内存流创建映像之前就进入了日志行,它实际上并没有引发异常,只是停止工作并返回到我的控制器。

我不太确定发生了什么事,但希望你们中的一些好人能为您提供帮助:)。

代码

public async Task<DataOpResult<string>> UploadFile(StorageUploadFile file,string FileGuid)
        {
            Console.WriteLine("Start Upload");
            DataOpResult<string> uploadFile = new DataOpResult<string>();
            uploadFile.Status = DataOpResultStatus.Success;
            
            try
            {
                //create storage client
                StorageClient storage = await StorageClient.CreateAsync(credential);
                Console.WriteLine("Created storage");

            //convert file data and create Image Object
            byte[] dataArray = Convert.FromBase64String(file.FileData);
            Console.WriteLine("Got byte array");
            Image imageVar = CreateImage(dataArray);
            Console.WriteLine("Converted image");

            //convert with aspect ratio
            var aspectRatio = (decimal)(imageVar.Width) / (decimal)(imageVar.Height);
            var newHeight = 150;
            int newWidth = (int)(newHeight * aspectRatio);

            //Resize Image
            Console.WriteLine("Resize image");
            Image resizedImage = ResizeImage(imageVar,newWidth,newHeight);
            MemoryStream resizedStream = new MemoryStream();
            resizedImage.Save(resizedStream,imageVar.RawFormat);
            Console.WriteLine("got new memory stream");
            // IUploadProgress defined in Google.Apis.Upload namespace
            var progress = new Progress<IUploadProgress>(
                p => Console.WriteLine($"bytes: {p.BytesSent},status: {p.Status}")
            );

            // var acl = PredefinedAcl.PublicRead // public
            var acl = PredefinedobjectAcl.AuthenticatedRead; // private
            var options = new UploadobjectOptions { PredefinedAcl = acl };
            Console.WriteLine("Upload Images");
            var result = await storage.UploadobjectAsync("echochat-292801.appspot.com",$"{FileGuid}/{file.FileName}",file.FileType,resizedStream,options,progress: progress);
            Console.WriteLine(result.ToString());
            uploadFile.DataItem = result.MediaLink;                  
        } catch (Exception ex)
        {
            uploadFile.Status = DataOpResultStatus.Error;
            uploadFile.ThrownException = ex;
        }

        return uploadFile;
    }


public static Image CreateImage(byte[] imageData)
    {
        Image image;
        using (MemoryStream inStream = new MemoryStream())
        {
            inStream.Write(imageData,imageData.Length);

            image = Image.FromStream(inStream);
        }

        return image;
    }

解决方法

运行sudo apt-get install libgdiplus 在我的服务器上结束了工作!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。