Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Loading an Image via a URL
  

  Recently I needed to grab images via a URL for our project here at work.  We basically have a web server the has tons of images for our various products in many different sizes and I wanted to be able to grab the images via a URL.  I thought I would throw the code snippets up that are needed to do this.

The code to perform this is pretty straight forward.  You basically need to create the URI object from the URL and them open up a new web request in order to pull the image.

You will notice that we catch and swallow both the IOExcpetion and the WebException.  This is don't with intent <slapping hands> because if we cannot grab the image or it fails for any reason we want to return a null.  This is a business requirement because when this happens we will display a 'place holder' image.  Normally I would never advocate swalling excpetions, but sometimes you have to do what you have to do.


Posted 06-12-2007 8:27 AM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

pepe wrote re: Loading an Image via a URL
on 06-13-2007 2:58 AM

I would rather user a HttpHandler in that case. I think it is more scalable because you can retrieve images from different sources depending on the need. A sample code in the handler for an image stored in a database:

public void ProcessRequest(HttpContext context) {

      MemoryStream pictureStr = new MemoryStream(databaseObj.Picture);

      img = Image.FromStream(pictureStr);

  // Write the image to the response stream in JPEG format.

      img.Save(context.Response.OutputStream, ImageFormat.Jpeg);

      img.Dispose();

      context.Response.End();

}

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)