Symmetri Developer Blog

December 21, 2007

Dynamically loading image from the web [C#]

General, .NET/C# - By Shourov Bhattacharya

A common scenario for a web-based developer is the following: your client-side application within the browser (either Javascript or Flash application) needs to reach across the web and pull an image from another domain. But the sandbox security model within the browser will almost always stop your client-side application from doing the cross-domain request. So what do you do?

The easiest (and best) solution is to create a server-side page on your own server or website which takes a URL and loads the image (or other file) for you and returns it in the response. Most hosting scenarios will allow these kinds of requests across the Internet. A bonus is that you can do some caching or other processing on the server before returning to the client - for example, adding a watermark or standardizing the image size.

I am building an application which does exactly this, getting thumbnails of websites from a remote site. I load the image by passing the URL to an ASPX page:
http://server/LoadImage.aspx?url={URL}

where {URL} is the (encoded) web address of the image.

Below is the C# code I use to implement.

using System;
using System.Data;
using System.Net;
using System.Text;
using System.IO;
 public partial class LoadImage : System.Web.UI.Page
  {
   protected void Page_Load(object sender, EventArgs e)
   {
    if ((Request.QueryString[”url”] != null) && (Request.QueryString[”url”] != “”))
    {
     string url = Request.QueryString[”url”].ToString();
     byte[] _data;
    _data = this.LoadThumbshotFromWeb(url);

     Response.ContentType = “image/jpeg”;
     Response.BinaryWrite(_data);
    }
   }

   // load image
   protected byte[] LoadThumbshotFromWeb(string url)
   {
    // create a request for the URL
    string _url = url;
    WebRequest wr = WebRequest.Create(_url);

    // if required by the server, set the credentials.
    wr.Credentials = CredentialCache.DefaultCredentials;

    byte[] result;
    byte[] buffer = new byte[4096];

    // get the response and buffer
    using (WebResponse response = wr.GetResponse())
    {
     using (Stream responseStream = response.GetResponseStream())
     {
      using (MemoryStream memoryStream = new MemoryStream())
       {
       int count = 0;
       do
        {
        count = responseStream.Read(buffer, 0, buffer.Length);
        memoryStream.Write(buffer, 0, count);
       } while (count != 0);
       result = memoryStream.ToArray();
     }
    }
   }
   return result;
   }
  }

December 20, 2007

SharpDevelop

General, .NET/C# - By Shourov Bhattacharya

Most .NET developers use Microsoft Visual Studio as their IDE, and despite its (many) flaws we’ve mostly come to love it, in a way - or at least feel comfortable with it. I had never really thought about alternatives, but I recently downloaded SharpDevelop, an open-source IDE for developing .NET applications. I am now developing my first ASP.NET web application free of Visual Studio - and it’s liberating. The SharpDevelop team have done the right thing and kept the interface very familiar - menus and shortcuts almost exactly mirror VS - while cutting out some of the fat and keeping to the basics. Overall, it’s easy to use and it works.

Given that Microsoft still insists on hefty licence fees for Visual Studio - more than $1000 for VS 2008 Pro, for example - the open-source alternative is certainly attractive for smaller companies and stand-alone commercial developers. SharpDevelop looks like it might exactly fit my requirements. Another step forward for open source software!

Download SharpDevelop from ic#code here.

December 8, 2007

Interacting with Sandy 3D shapes

Flash/Flex - By Shourov Bhattacharya

Well, I finally got user interaction working with Sandy 3D objects. It took a bit of fiddling around, but it works. It’s fiddly because there is no obvious way to send the calling object to the event handler, so you end up responding to a mouse click without knowing where it came from. The trick in the event handler is to use event.target which will give you the container (but not the shape itself) where the container is of type Sprite. Then look through the Sandy static enumeration world.root.children to find the original Shape3D by comparing each shape’s container to the one you have.

Anyway, here is a quick example of interaction below. Click on the shape to get a random movement effect - animations are done with Tweener.



December 4, 2007

Print to a PC network printer from a MacBook

General - By Shourov Bhattacharya

Connecting your Mac to a PC network printer … you might think that would be easy. But apparently not. Turns out you have to emulate a PostScript printer on your Window host and then redirect print requests through that. It’s all laid on this excellent how-to page.  

December 2, 2007

jQuery radio button bug

Javascript - By Shourov Bhattacharya

This is especially relevant if you use the excellent jQuery treeview code to create client-side tree controls. I’ve been using it to create an input control that uses radio buttons, and I have found a weird bug - opening and closing the tree clears all radio button selections inside that branch of the tree! I tore my hair out for a while trying to work it out, then hunted it down to a known bug on the jQuery site http://dev.jquery.com/browser/trunk/jquery/src/core.js?rev=3839. Either get the new version of jQuery or copy the code into your existing version (I had to do the latter, as the latest version of jQuery breaks the treeview code which is yet to be updated).

Get free blog up and running in minutes with Blogsome
Theme designed by Janis Joseph