[Mono-list] Download and display PNG in Cairo

Jonathan Pobst monkey at jpobst.com
Thu Apr 23 14:52:15 EDT 2009


I don't know about Cairo, but the easy way to download a file is:

WebClient wc = new WebClient ();
wc.DownloadFile ("http://www.blah.com/myimage.png", "my.png");

If you want the bytes instead of writing to disk, you can use:
byte[] bytes = wc.DownloadData ("http://www.blah.com/myimage.png");

Jonathan

Shean Paule wrote:
> Hi. I'm trying to download a PNG file into a buffer and display it via Cairo.
> I decided to do this via "Systems.Net" and "Cairo" itself.
> 
> I'm using C#, Gtk, Cairo on MonoDevelop VMWare.
> 
> 
> This is the closest I have come:
> 
>                 string picUri = "http://blabla.com/my.png";
>                 // Create the requests.
>                 WebRequest requestPic = WebRequest.Create(picUri);
>                 WebResponse responsePic = requestPic.GetResponse();
>                 System.IO.Stream test = responsePic.GetResponseStream();
>                 Surface MySurface = new
> ImageSurface(StreamToArray(test),Format.ARGB32,640,640,0); // CRASH
> 
> static protected byte[] StreamToArray(System.IO.Stream st) {
>                 // Source:
> http://pupeno.com/blog/svgs-in-.net-using-cairo-and-gtk-and-c
>                 if(st.Length > int.MaxValue) {
>                         // TODO exception or something.
>                         return null;
>                 } else {
>                         // TODO exception
>                         System.IO.StreamReader str = new
> System.IO.StreamReader(st);
>                         System.Text.UTF8Encoding encoder = new
> System.Text.UTF8Encoding();
>                         return encoder.GetBytes(str.ReadToEnd());
>                 }
> }
> 
> I just can't find a proper way to do this and have run out of sources on
> this problem. Any help would be greatly appreciated.
> 
> I'm also happy about workarounds (e.g. download the file to a .png - I know
> how to load those into Cairo)



More information about the Mono-list mailing list