Skip to content

Instantly share code, notes, and snippets.

@iley
Created May 16, 2011 10:38
Show Gist options
  • Save iley/974215 to your computer and use it in GitHub Desktop.
Save iley/974215 to your computer and use it in GitHub Desktop.
thumbnail.aspx
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script language="C#" runat="server">
public bool ThumbnailCallback() {
return false;
}
private void Page_Load(object sender, System.EventArgs e) {
int width = 600;
string imageFile = Request.PhysicalApplicationPath + "\\" + Request.QueryString["image"].ToString();
string thumbFile = Path.GetDirectoryName(imageFile) + "\\" +
"thumb_" +
Path.ChangeExtension(Path.GetFileName(imageFile), ".jpg");
System.Drawing.Image thumb;
if(!File.Exists(thumbFile) ||
DateTime.Compare(File.GetLastWriteTime(imageFile),
File.GetLastWriteTime(thumbFile)) >= 0) {
System.Drawing.Image img = System.Drawing.Image.FromFile(imageFile);
int height = img.Height * width / img.Width;
thumb = img.GetThumbnailImage(width, height, ThumbnailCallback, IntPtr.Zero);
thumb.Save(thumbFile, ImageFormat.Jpeg);
} else {
thumb = System.Drawing.Image.FromFile(thumbFile);
}
thumb.Save(Response.OutputStream, ImageFormat.Jpeg);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment