question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DownloadBinary function

See original GitHub issue

Hi ,

I tried DownloadBinary function.

However, when we convert byte to base64, Picture is not displayed properly.

byte[] imageData = TwitterAccessor.DownloadBinary("Image URL");
string base64String = Convert.ToBase64String(imageData);
Image1.ImageUrl = "data:image/jpg;base64," + base64String;

Please advise. It seems there is some problem in byte array.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
RickySan65commented, Oct 5, 2017

Never reached the office from the looks of it 😉

1reaction
pal5hahnaircommented, Jan 11, 2018

Here you go!!

Code :

   var oauth_token = "oauth_token";
            var oauth_token_secret = "oauth_token_secret";
            var oauth_consumer_key = "oauth_consumer_key";
            var oauth_consumer_secret = "oauth_consumer_secret";



            var oauth_version = "1.0";
            var oauth_signature_method = "HMAC-SHA1";


            var oauth_nonce = Convert.ToBase64String(
                new
ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
            var timeSpan = DateTime.UtcNow
                - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            var oauth_timestamp =
Convert.ToInt64(timeSpan.TotalSeconds).ToString();


            var status = "Updating status via REST API if this works";
            var resource_url = URL; // Use Image URL


            var baseFormat =
"oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
"&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}";


            var baseString = string.Format(baseFormat,
                                       oauth_consumer_key,
                                       oauth_nonce,
                                       oauth_signature_method,
                                       oauth_timestamp,
                                       oauth_token,
                                       oauth_version
                                       );

            baseString = string.Concat("GET&",
Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString));

            var compositeKey =
string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
                                    "&",
Uri.EscapeDataString(oauth_token_secret));

            string oauth_signature;
            using (HMACSHA1 hasher = new
HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
            {
                oauth_signature = Convert.ToBase64String(

hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
            }


            var headerFormat = "OAuth oauth_nonce=\"{0}\",
oauth_signature_method=\"{1}\", " +
                               "oauth_timestamp=\"{2}\",
oauth_consumer_key=\"{3}\", " +
                               "oauth_token=\"{4}\",
oauth_signature=\"{5}\", " +
                               "oauth_version=\"{6}\"";

            var authHeader = string.Format(headerFormat,
                                    Uri.EscapeDataString(oauth_nonce),

Uri.EscapeDataString(oauth_signature_method),
                                    Uri.EscapeDataString(oauth_timestamp),

Uri.EscapeDataString(oauth_consumer_key),
                                    Uri.EscapeDataString(oauth_token),
                                    Uri.EscapeDataString(oauth_signature),
                                    Uri.EscapeDataString(oauth_version)
                            );


            HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(resource_url);
            request.Headers.Add("Authorization", authHeader);
            request.Method = "GET";
            request.ContentType =
"application/x-www-form-urlencoded;charset=UTF-8";
            ServicePointManager.ServerCertificateValidationCallback =
delegate { return true; };
            WebResponse response = request.GetResponse();


            var respStream = response.GetResponseStream();
            var contentLen = response.ContentLength;
            byte[] outData;
            using (var tempMemStream = new MemoryStream())
            {
                byte[] buffer = new byte[128];
                while (true)
                {
                    int read = respStream.Read(buffer, 0, buffer.Length);
                    if (read <= 0)
                    {
                        outData = tempMemStream.ToArray();
                        break;
                    }
                    tempMemStream.Write(buffer, 0, read);
                }
            }


            Bitmap bp = new Bitmap(new MemoryStream(outData));



            bp.Save(Server.MapPath("~/Images/" + ID + ".jpg"),
ImageFormat.Jpeg);
Read more comments on GitHub >

github_iconTop Results From Across the Web

getBinaryURL: Download binary content in RCurl - rdrr.io
This function allows one to download binary content. This is a convenience function that is a call to getURL with suitable values for...
Read more >
Download Binary Files with Javascript
I want to download binary files using Javascript. I have a REST service that returns the binary data and i want to know...
Read more >
R: Download binary content
This function allows one to download binary content. This is a convenience function that is a call to getURL with suitable values for...
Read more >
How to download binary from EDR UI
Go to Binary Analysis page in the EDR UI. Click on download icon under General Info section. This will download the binary in...
Read more >
How can I download binary file from python function
How can I download binary file from python function · 1. Binary fields is created and some data is attached (pdf) · 2....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found