Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Imgur image uploader

We probably had a few of these in the past but since i had some free time I thought it would be a good idea to code one :)

Use left click to open the link in the browser, right click to copy the link in the clipboard.

Screenshot (2.6mb)
Spoiler (Click to View)

VT scan
Spoiler (Click to View)

Download link
Spoiler (Click to View)

If i forgot anything let me know :)
Is there a limit to the size of the image? I was working on a screenshot program that auto uploaded to imgur. I kept getting a "URI is too long" error.
<?php echo "I'm a professional web developer, if you need something ask me."; ?>

The Mediafire link doesn't work & You don't scan the .rar file you scan the .exe :c
Hi
Is there any chance that you can C/P the sourcecode in here, so i can take a look at it and learn from it?
Thanks (:
look here.
http://api.imgur.com/examples

anyway ... it not work.
it convert all my PNG to BLACK pictures.
so i have another methood :P
[fixed and compact code now]

Helper's
Code
/// <summary>
  /// helper
  /// </summary>
  /// <param name="boundray"></param>
  /// <param name="ContentDisposition"></param>
  /// <param name="data"></param>
  /// <returns></returns>
  private string AddFormData(string boundray, string ContentDisposition, string data)
  {
    return string.Format("{0}\n{1}\n\n{2}",
    boundray,
    string.Format("Content-Disposition: form-data; name=\"{0}\"", ContentDisposition),
    data);
  }

  /// <summary>
  /// helper
  /// </summary>
  /// <param name="boundray"></param>
  /// <param name="name"></param>
  /// <param name="filename"></param>
  /// <param name="data"></param>
  /// <returns></returns>
  private string AddFormData(string boundray, string name, string filename, string data)
  {
    if (filename == null)
    {
    return string.Format("{0}\n{1}\n{2}\n",
    boundray,
    string.Format("Content-Disposition: form-data; name=\"{0}\"", name, filename),
    data);
    }
    else
    {
    return string.Format("{0}\n{1}\n{2}\n",
    boundray,
    string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"", name, filename),
    data);
    }
  }

main code
Code
string
    /* URI */
    requestUriString = string.Empty,
    /* SPLIT */
    boundray = DateTime.Now.Ticks.ToString(),
    lines = string.Empty,
    boundray_lines = string.Empty,
    /* FILE NAME */
    fileName = Path.GetRandomFileName(),
    ext = fileName.Split('.')[1],
    /* RESULT FROM SERVER */
    input = string.Empty;

    byte[]
    bytes = null,
    buffer = null,
    sBytes = null;

    StringBuilder builder = new StringBuilder();
    MemoryStream ms = new MemoryStream();
    HttpWebRequest request = null;
    Regex regex = null;

    fileName = fileName.Replace(ext, "png");
    img.Save(ms, ImageFormat.Png);

// main code now
{
string hash = string.Empty;
    string sid = string.Empty;
    using (WebClient client = new WebClient())
    {
    hash = client.DownloadString("http://www.imgur.com/");
    int index = hash.IndexOf("var sid_hash   = '") + ("var sid_hash   = '").Length;
    hash = hash.Substring(index, hash.Length - index);
    index = hash.IndexOf("';");

    hash = hash.Substring(0, index);
    sid = client.ResponseHeaders["Set-Cookie"].Split(';')[0].Split('=')[1];
    }

    lines = new string('-', 12);
    boundray_lines = lines + boundray;
    requestUriString = string.Format("https://imgur.com/upload?sid_hash={0}", hash);

    builder.AppendLine(AddFormData(boundray_lines, "Filename", fileName));
    builder.AppendLine(AddFormData(boundray_lines, "layout", "b"));
    builder.AppendLine(AddFormData(boundray_lines, "create_album", "0"));
    builder.AppendLine(AddFormData(boundray_lines, "edit", "0"));
    builder.AppendLine(AddFormData(boundray_lines, "catify", "0"));
    builder.AppendLine(AddFormData(boundray_lines, "sid", sid));
    builder.AppendLine(AddFormData(boundray_lines, "album_title", "Optional Album Title"));
    builder.AppendLine(AddFormData(boundray_lines, "Filedata", "fileName", "Content-Type: application/octet-stream"));
    bytes = Encoding.UTF8.GetBytes(builder.ToString());
    builder = new StringBuilder();

    buffer = ms.ToArray();
    ms.Dispose();

    builder.AppendLine();
    builder.AppendLine(AddFormData(boundray_lines, "Upload", "Submit Query"));
    builder.AppendLine(boundray_lines + "--");
    sBytes = Encoding.UTF8.GetBytes(builder.ToString());
    builder = new StringBuilder();

    request = (HttpWebRequest)WebRequest.Create(requestUriString);
    //request.CookieContainer = Container;
    request.Method = "POST";
    request.KeepAlive = true;
    request.ContentType = "multipart/form-data; boundary=" + lines.Remove(0, 2) + boundray;
    request.ContentLength = bytes.Length + buffer.Length + sBytes.Length;
    request.ServicePoint.Expect100Continue = false;
    request.UserAgent = "Shockwave Flash";

    using (Stream stream = request.GetRequestStream())
    {
    stream.Write(bytes, 0, bytes.Length);
    stream.Write(buffer, 0, buffer.Length);
    stream.Write(sBytes, 0, sBytes.Length);
    stream.Close();
    }

    input = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd();
    input = input.Split('\\')[1].Split('\\')[0].Remove(0, 1);

    using (WebClient client = new WebClient())
    {
    input = client.DownloadString(
    string.Format("https://imgur.com/{0}", input));
    }
    regex = new Regex("http://i(.*?)imgur.com/.*(.png)");
    result = regex.Matches(input)[0].ToString();
}
Sorry for the late reply I was away for the day. Does it do that with all the .png photos ? Since i cant duplicate the issue here (works fine with .png here).

Also are you on winxp or win vista/7 ? (Not that it should matter since it works on my VM but checking either way :p).

Edit: VT for the .exe link
New mediafire link.
awesome!! thank you so much for this!
No worries, made my life easier as well :p Too bad i cant have the a progress bar working for that without using .net framework 4. Would be nice to have an indication when the upload is going to finish, not that it takes long :p
Here's the Minus API, please check it out if anyone is interested ;)

http://minus.com/pages/api

[Image: fbCm4V8.png]
Looks pretty good, the only issue i have with it is the 30days expiration for users with no account and the need to enter an email before uploading the picture :(

Also the link under "Where can you find the API?" doesnt seem to work, https://minus.com/api/v2/ returns "Firefox can't find the file at https://minus.com/api/v2/."
Or, use gyazo (and my infinte gyazo embedding tool to avoid stupid bandwith limits)
Gyazo looks like a snipping tool with an upload feature, did I miss something ? ;p


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by C++Dec 29, 2019 06:16 PM 16 831 Dec 29, 2019 06:16 PM
Last Post: DNA
[Python] by DistrictApr 7, 2019 03:14 AM 4 230 Apr 7, 2019 03:14 AM
Last Post: lockhead
[C#] by TunabishJun 27, 2018 05:10 PM 14 610 Jun 27, 2018 05:10 PM
Last Post: gigajew