1

The Goal: I am currently taking a file from javascript, converting it into a base64 encoded string of a byte array and passing it to my .net controller. I am then processing that by converting that back into a byte array, creating a new memory stream of it, creating an Image object from the memory stream, changing what I need to change about it, and then bringing it back into a memory stream to then upload to my server.

The problem: I currently am able to take an image in from JavaScript encoded like I had mentioned earlier, and upload it to the server no problem, however the issue comes in when I am trying to convert my memory stream to an Image (so that I can resize it). I am not sure why, but on my local machine it has no problems and doesn't throw any errors and uploads perfectly fine, resized and everything. However on my linux server, it gets to the log line right before it try's to create the image from the memory stream, it doesn't actually throw an exception, but just stops working and returns to my controller.

I am not really sure what is going on but hopefully some of you fine people can help :).

The code:

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, 0, imageData.Length);

            image = Image.FromStream(inStream);
        }

        return image;
    }
PurpSchurp
  • 581
  • 5
  • 14
  • Is the file data, the string, actually being sent? How is the string encoded? – Ian H. Oct 24 '20 at 18:05
  • Yes it is encoded base64 from the javascript. If I don't do any resizing and just upload the memory stream and never convert to an image, it uploads fine – PurpSchurp Oct 24 '20 at 18:08
  • Would you like me to add the code for how that works? – PurpSchurp Oct 24 '20 at 18:11
  • Note: even on the server it works fine if I am not trying to resize the image at all, just taking the string, converting it to a memory stream, and then uploading – PurpSchurp Oct 24 '20 at 18:12
  • Take a look at this answer by Jon Skeet: https://stackoverflow.com/a/336396/9365244 – JayV Oct 24 '20 at 18:30
  • 1
    "just stops working and returns to my controller." how's that? – Evk Oct 24 '20 at 19:11
  • @Evk turns out you lead me to the right answer. I forgot to check for my dataopresult status. Turns out this whole time on my server its been throwing "The type initializer for 'Gdip' threw an exception." I think I can find the solution now that I have some more info! Thank you rubber ducky :)! – PurpSchurp Oct 24 '20 at 19:27
  • You are welcome :) maybe this image format is not supported (out of the box) on linux but is on windows, or something like that. – Evk Oct 24 '20 at 19:32
  • Yeah running "sudo apt-get install libgdiplus" on the server actually fixed it!!! there goes many hours of looking, but I appreciate you very much – PurpSchurp Oct 24 '20 at 19:37
  • I was of course checking the wrong variable so I never got the correct answer back! Such a programmer move lol – PurpSchurp Oct 24 '20 at 19:38

1 Answers1

1

Running sudo apt-get install libgdiplus on the my server ended up working!

PurpSchurp
  • 581
  • 5
  • 14