IT tech corner

IT articles from blog.area23.at

C# fetch a text from a website and render it as image

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(Image image)
    {
        byte[] byteArray = new byte[0];
        using (MemoryStream stream = new MemoryStream())
        {
            image.Save(stream, Imaging.ImageFormat.Jpeg);
            stream.Close();

            byteArray = stream.ToArray();
        }
        return byteArray;
    }

      protected void SetImage()
    {
        string xpr = "<span class=\"money\" id=\"debt\">";
        char[] trimchars = "\r\n".ToCharArray();        
        string uriResponse = this.FetchFromUri(urlToFetch, "");
        int schuldidx = uriResponse.IndexOf(xpr);
        string rest = uriResponse.Substring(schuldidx+xpr.Length);
        int endschuld = rest.IndexOf("</span>");
        rest = rest.Substring(0, endschuld).Trim(trimchars);
        Bitmap b = this.CreateImage(rest);
        // b.Save(Server.MapPath("schulden.jpeg"), 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, FontStyle.Bold,
            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);
    }
}

a simple url encoder/decoder in javascript

<script type=“text/javascript” language=“javascript”>
function urlencode(s) {
if (s == null || s.value == null || s.value == ”) {
alert(“Url to encode is null or empty!”);
return ;
}
var uri = encodeURIComponent(s.value)
document.getElementById(‘output’).value = uri;
}
function urldecode(s) {
if (s == null || s.value == null || s.value == ”) {
alert(“Url to decode is null or empty!”);
return ;
}
var uri = decodeURIComponent(s.value)
document.getElementById(‘output’).value = uri;
}
</script>
<form action=“” id=“urlform”>
<table cellspacing=“1” cellpadding=“1”>
<tr>
<td>
<input type=“text” id=“url” name=“url” size=“64” maxlength=“256”
value=“http%3A%2F%2Fblog.area23.at%2F” />
</td><td>
<input type=“button” id=“deocde” name=“deocde” value=“deocde”
onclick=“urldecode(document.getElementById(‘url’))” />
</td><td align=“right”>
<input type=“button” id=“encode” name=“encode” value=“encode”
onclick=“urlencode(document.getElementById(‘url’))” />
</td><td align=“right”>
&nbsp;a simple url encoder/decoder
</td>
</tr><tr>
<td colspan=“3”>
<input type=“text” id=“output” name=“output” size=“90” maxlength=“256” readonly=“readonly” />
</td><td align=“right”>
&nbsp;<a href=http://blog.area23.at&#8221;>http://blog.area23.at</a&gt;
</td>
</tr>
</table>
</form>    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s