Can't get favicon from instagram - c#

I need to retrieve favicon of instagram. Now my program can parse html code and retrieve the appropriate url of the icon: http://d36xtkk24g8jdx.cloudfront.net/bluebar/d1f7ba7/images/ico/apple-touch-icon-precomposed.png.
But I can't read this icon from my program because the instagram puts some extra bytes in the beginning, the middle and the end of this file when my program tries to download it:
var wc = new WebClient();
var iconBytes = wc.DownloadData(#"http://d36xtkk24g8jdx.cloudfront.net/bluebar/d1f7ba7/images/ico/apple-touch-icon-precomposed.png");
var converter = new ImageConverter();
var image = (Image)converter.ConvertFrom(iconBytes); // Crash here 'parameter is invalid'
I tried to save the png file from the web browser directly. Then I analyzed its content and came to conclusion that the bytes array which WebClient returns is almost identical but it contains 15 extra bytes in the beginning, 8 extra bytes in the end and 5 extra bytes in the middle of the array. I can easily clean this 'salt' from the beginning and the end based on knowledge of png format, but I have no idea how to remove the garbage from the middle of the array.
Could you please help me figure out how to download and process instagram favicon?

This Google Service will return the image:
http://www.google.com/s2/favicons?domain=www.instagram.com
Example
http://www.google.com/s2/favicons?domain= *Domain*

Related

load screenshot from adb through c#

I want to get a screenshot into c# using adb without saving files to the filesystem all the time.
I'm using the SharpAdbClient to talk with the device.
I'm on a windows platform.
This is what i got so far:
AdbServer server = new AdbServer();
StartServerResult result = server.StartServer(#"path\to\adb.exe", restartServerIfNewer: false);
DeviceData device = AdbClient.Instance.GetDevices().First();
ConsoleOutputReceiver receiver = new ConsoleOutputReceiver();
AdbClient.Instance.ExecuteRemoteCommand("screencap -p", device, receiver);
string str_image = receiver.ToString().Replace("\r\r", "");
byte[] bytes = Encoding.ASCII.GetBytes(str_image);
Image image = Image.FromStream(new MemoryStream(bytes));
I can successfully load both str_image, and create the byte array but it keeps saying System.ArgumentException when trying to load it into an Image.
I also tried saving the data to a file, but the file is corrupt.
I tried both replacing "\r\r" and "\r\n", both same result.
Anyone has some insight in how to load this file?
It's actually preferred if it could be loaded into a Emgu image since i'm gonna do some CV on it later.
One possible cause is the nonprintable ASCII characters in the string.
Look at the code below
string str_image = File.ReadAllText("test.png");
byte[] bytes = Encoding.ASCII.GetBytes(str_image);
byte[] actualBytes = File.ReadAllBytes("test.png");
str_image is shown in the below screencap, note that there are some non-printable chars (displayed as question mark).
The first eight bytes of a PNG file are always
137 80 78 71 13 10 26 10
While you read the console output as a string, then use ASCII to encode the string, the first byte becomes 63 (0x3F), which is the ASCII code for a question mark.
Also note that the size of the two byte arrays vary hugely (7828/7378).
And other thing is you are replace "\r\r", while actually a new line character in Windows is "\r\n".
So my conclusion is some image data is lost or modified in the output redirection by the ConsoleOutputReceiver, and you cannot recover the original data from the output string.

How can I get the loading time and volume of a website?

I'm working on a program that should measure loading time and volume of a website that I give as an input.
Here I have some code that returns just response time of website but I want the total loading time and total volume of items such (pictures, JavaScript, HTML, etc.).
public string Loading_Time(string url)
{
Stopwatch stopwatch = new Stopwatch();
WebClient client = new WebClient();
client.Credentials = CredentialCache.DefaultCredentials;
stopwatch.Start();
string result = client.DownloadString(url);
stopwatch.Stop();
return stopwatch.Elapsed.Milliseconds.ToString();
}
How can I achieve that?
This is going to be a little bit tough. Start by using something like HTMLAgilityPack or something similar to parse the returned html from your original request (dont try to parse HTML yourself!)
Scan through the object representation of the HTML once parsed, and decide what you want to measure the size of. Typically this will be
Includes, such as CSS, or javascript
Images in IMG and BUTTON elements, as well as background images
The difficulty is that often images are specified as part of a css stylesheet - so are you going to try to parse every css file to obtain these too?
The original request you made for the HTML you could have obtained the byte size of the downloaded string. Start with this number as your "volume".
Now make a separate request for each js, css, image etc file in the same way. But all you're interested in is the byte size of each download - its readily available when you make an HTTP request. Add each item's byte size to the total.
When you're finished you will have the total byte size for all artifacts of that web page.

Base64 decoding of a image gives a red question mark

I am encoding and decoding facebook thumbnail image data using base64 strings in unity3d. When the user does not have a profile picture, the resulting image is a red question mark. Is there any way to recognise that an invalid picture is being sent so that I can replace it with a default pic of my choosing?
I am converting the string to image data using Convert.FromBase64String (string encoded) in c#.
I assume that you use some API to retrieve the base64 encoded string from an URL?
In that case, you could just dump the retrieved string once to the console and then copy it into your source code and compare this to the string you get in the future. Of course, this will break if the facebook API you use decides to deliver a different icon, in which case you would have to dump the new "unknown user" thumbnail.
string encoded = ... // however you obtain your thumbnail
print encoded; // dump the string to the console once. remove this statement later
if (encoded == "...here comes the (rather large) string you just copied")
encoded = "...here comes some other image you like to use, encoded as string";
...
Not very elegant, but at least easy to implement.
I suppose, if an error image (e.g. the "red question-mark" image) is predictably returned when no profile picture exists, you could simply test for that case and replace with another image of your choosing. You may choose to store the red question-mark image as a resource and then compare its Base64 string to the Base64 of each image returned from your requests.
In that case, the pivotal bit of code might look something like this (assuming you've already stored the image's Base64 string in a resource):
ResourceManager rm =
new ResourceManager("ExampleAppData", typeof(ExampleApp).Assembly);
String errorImageBase64 = rm.GetString("ErrorImageBase64");
// the image you get from your request
String resultImageBase64 = GetProfileImageBase64();
Image missingProfile;
if(resultImageBase64.Equals(errorImageBase64))
{
missingProfile = ImageFromBase64String(rm.GetString("MissingProfileBase64"));
}
else
{
missingProfile = ImageFromBase64String(resultImageBase64);
}
References:
http://msdn.microsoft.com/en-us/library/2xsy4hac.aspx
http://ozgur.ozcitak.com/snippets/2009/12/21/base64-encoding-an-image-with-csharp.html
In my experience it was due to a wrong request.
In my case the wrong API query returning Red Questionmark was /{fbId}/picture?g&type=square&height=128&width=128
I solved removing "?g", the working query now is
/{fbId}/picture?type=square&height=128&width=128

c# how to write a jpg image from request.binaryread

I have a flash app which sends raw data for a jpg image to a particular url Send.aspx . In Send.aspx I am using request.binaryread() to get the total request length and then read in the data to a byte array.
Then I am writing the data as jpg file to the server. The code is given below:
FileStream f = File.Create(Server.MapPath("~") + "/plugins/handwrite/uploads/" + filename);
byte[] data = Request.BinaryRead(Request.TotalBytes);
f.Write(data, 0, data.Length);
f.Close();
The file is getting created but there is no image in it. It always shows up as empty in any graphic viewer. What part am I missing. Am I supposed to use jpg encoding first before writing it to file? Thanks in advance
Well, you should use a using statement for your file stream, but other than that it looks okay to me.
A few suggestions for how to proceed...
Is it possible that the client isn't providing the data properly? Perhaps it's providing it as base64-encoded data?
Have you already read some data from the request body? (That could mess things up.)
I suggest you look closely at what you end up saving vs the original file:
Are they the same length? If not, which is longer?
If they're the same length, do their MD5 sums match?
If you look at both within a binary file editor, do they match at all? Any obvious differences?

WebClient.DownloadFile vs. WebClient.DownloadData

I am using WebClient.DownloadFile to download a small executable file from the internet. This method is working very well. However, I would now like to download this executable file into a byte array rather than onto my hard drive. I did some reading and came across the WebClient.DownloadData method. The problem that I am having with the downloadData method is that rather than downloading my file, my code is downloading the HTML data behind my file's download page.
I have tried using dozens of sites - each brings me the same issue. Below is the code I am using.
// Create a new instance of the System.Net 'WebClient'
System.Net.WebClient client = new System.Net.WebClient();
// Download URL
Uri uri = new Uri("http://www35.multiupload.com:81/files/4D7B4D2BFC3F1A9F765A433BA32ED2C5883D0CE133154A0FDB7E7786547A3165DA62393141C4AF8FF36C75222566CF3EB64AF6FBCFC02099BB209C891529CF7B90C83D9C63D39D989CBB8ECE6DE2B83B/Project1.exe");
byte[] dbytes = client.DownloadData(uri);
MessageBox.Show(dbytes.Length.ToString()); // Not the size of my file
Keep in mind that I am attempting to download the data of an executable file into a byte array.
Thank you for any help,
Evan
You are attempting to download a file using an expired token url. See below:
URL: http://www35.multiupload.com:81/files/4D7B4D2BFC3F1A9F765A433BA32ED2C5883D0CE133154A0FDB7E7786547A3165DA62393141C4AF8FF36C75222566CF3EB64AF6FBCFC02099BB209C891529CF7B90C83D9C63D39D989CBB8ECE6DE2B83B/Project1.exe`
Server: www35
Token:
4D7B4D2BFC3F1A9F765A433BA32ED2C5883D0CE133154A0FDB7E7786547A3165DA62393141C4AF8FF36C75222566CF3EB64AF6FBCFC02099BB209C891529CF7B90C83D9C63D39D989CBB8ECE6DE2B83B
You can't just download a file by waiting for the timer to end, and copy the direct link, it's a "token" link. It will only work for a specified period of time before redirecting you back to the download page (which is why you are getting HTML instead of binary data).
Workaround
You will have to download the multiupload's HTML and parse the direct download link from the HTML source code. Only this way provides a sure-fire way of getting an up-to-date token url.
How #Dark Slipstream said, you're attempting to download a file using an expired token url
look how get the new url:
System.Net.WebClient client = new System.Net.WebClient();
// Download URL
Uri uri = new Uri("http://www.multiupload.com/39QMACX7XS");
byte[] dbytes = client.DownloadData(uri);
string responseStr = System.Text.Encoding.ASCII.GetString(dbytes);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(responseStr);
string urlToDownload = doc.DocumentNode.SelectNodes("//a[contains(#href,'files/')]")[0].Attributes["href"].Value;
byte[] data = client.DownloadData(uri);
length = data.Length;
I dont parsing the exceptions

Categories