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
main code
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();
}