10

i'm sending the 5mb base64 string to the backend api from the unity android app. while post and get the app gets slow because of heavy data is incoming. i want to compress the base64 5mb string to lesser size. can anyone help me in finding the solution

i have tried all the solutions like GZipStream and all other, but they are working fine with the normal string but not helping when trying with base64.

can anybody tell how to compress and decompress base64 string.

nithish pitla
  • 109
  • 1
  • 1
  • 3
  • 3
    The idea of using base-64 is that it can be transmitted over channels which can't carry binary data. If you can send gzipped data, you might as well gzip the data instead of converting it to base-64. – Andrew Morton Dec 23 '19 at 11:21
  • @andrewMorton if you mean sending bytes, i have tried that part also but before compressing lenght of bytes are 4496330, and after compressing using gzip it turned to be 4789390. – nithish pitla Dec 23 '19 at 11:26
  • what is it that you are trying to send over? Can you show us an example of the data and your code. It might also be better for you to be more chatty than chunky approach in what you are trying to achieve if you are having some problems. – Simon Price Dec 23 '19 at 11:28
  • @nithishpitla How are you measuring the length? How are you doing the compression? Show us some code if you want a detailed answer, we can't guess what you are doing. But really you should focus on reducing the size, compression isn't magic – Milney Dec 23 '19 at 11:28
  • 3
    If you are sending something like a jpeg-compressed image, you won't be able to compress it more, and it could even make the size larger. And if you have a 4MB jpeg file, you probably need to resize it to smaller pixel dimensions. – Andrew Morton Dec 23 '19 at 11:32
  • ok then suggest some best way to send jpeg to backend api without getting lag. – nithish pitla Dec 23 '19 at 11:33
  • 1
    @nithishpitla If you have a 4MB jpeg file, you probably need to resize it to smaller pixel dimensions. – Andrew Morton Dec 23 '19 at 11:34
  • can you share some links or codes for that please. – nithish pitla Dec 23 '19 at 11:38

1 Answers1

15

Base64 is already encoded in a way which does not suit most compression algorithms - see Why does base64-encoded data compress so poorly? for details.

You will want to compress the original binary data and then base64 the compressed data, or don't bother converting to base64 at all.

Additionally many HTTP clients/servers will automatically compress the stream, so this may be happening already implicitly.

A better question may be why you are sending such a large amount of data in a web request, perhaps you should try to reduce the size instead of attempting to compress it...

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Milney
  • 6,253
  • 2
  • 19
  • 33
  • i have to save the image as profile picture. how can i reduce the size, can you elaborate please. – nithish pitla Dec 23 '19 at 11:28
  • @nithishpitla - not without some details... I don't know what your data is, how much of it you *actually* need, and how you are compressing/sending/measuring. You will have to post your code for us to know properly – Milney Dec 23 '19 at 11:29
  • public IEnumerator DecodeImg(string photoPath) { if (string.IsNullOrEmpty(photoPath)) yield break; byte[] bytesimg = File.ReadAllBytes(photoPath); string imageStr = Convert.ToBase64String(bytesimg); // i have to store the jpeg/png in the server to show the profile image of user } – nithish pitla Dec 23 '19 at 11:34
  • 3
    Oh its an image? Image files are usually already compressed - you won't be able to reduce the size of an image file. You will increase the size converting it to base64, so just send the bytes directly will be the fastest way – Milney Dec 23 '19 at 11:35
  • 3
    Perhaps use an image manipulation library to reduce the quality and therefore size of the image before sending if you do not need it to be original quality – Milney Dec 23 '19 at 11:36
  • can you please share some links for reducing the size and quality. as i'm a beginner in this. – nithish pitla Dec 23 '19 at 11:51
  • You can try the microsoft ones: https://learn.microsoft.com/en-us/dotnet/api/system.drawing.imaging.encoderparameter?redirectedfrom=MSDN&view=netframework-4.8 Or look into a library like: https://csharp-station.com/4-best-c-libraries-for-image-processing/ – Milney Dec 23 '19 at 11:59