using System;
using System.Web;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
public partial class _Default : System.Web.UI.Page
{
private static HttpWebRequest request;
private static HttpWebResponse response;
private static
string urlToFetch
=
"http://www.staatsschulden.at/"
;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.SetImage();
}
}
protected byte[] GetImageBytes(System.Drawing.Image image)
{
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Close();
byteArray = stream.ToArray();
}
return byteArray;
}
protected void SetImage()
{
string finder = "<span class=\"money\" id=\"debt\">";
char[] trimchars = "\r\n".ToCharArray();
string uriResponse = this.FetchFromUri(
urlToFetch
, "");
int schuldidx = uriResponse.IndexOf(finder);
string rest = uriResponse.Substring(schuldidx + finder.Length);
int endschuld = rest.IndexOf("</span>");
rest = rest.Substring(0, endschuld).Trim(trimchars);
Bitmap b = this.CreateImage(rest);
// b.Save(Server.MapPath("staatsschulden.jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = GetImageBytes(b);
Response.BinaryWrite(bytes);
}
private string FetchFromUri(string requestUrl, string requestParameter)
{
string fetched = "";
request = (HttpWebRequest)WebRequest.Create(requestUrl);
byte[] parameter = Encoding.UTF8.GetBytes(requestParameter);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = parameter.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(parameter, 0, parameter.Length);
requestStream.Close();
//Holt sich die Antwort
response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
fetched = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();
return fetched;
}
private Bitmap CreateImage(string s)
{
Bitmap b = new Bitmap(1, 1);
int width = 0;
int height = 0;
Font font = new Font("Trebuchet", 16, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Pixel);
Graphics g = Graphics.FromImage(b);
width = (int)g.MeasureString(s, font).Width;
height = (int)g.MeasureString(s, font).Height;
b = new Bitmap(b, new Size(width, height));
g = Graphics.FromImage(b);
g.Clear(Color.White);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.DrawString(s, font, new SolidBrush(Color.BlueViolet), 0, 0);
g.Flush();
return (b);
}
}
Keine Kommentare:
Kommentar veröffentlichen