Library code snippets
Creating a dynamic graphic which returns a .jpg
This code allows an .aspx file to receive parameters (file name and width of picture) and return a .jpg so that in you HTML code you can use access an image like this: src="SmartPicture.aspx?f=Employee878.jpg&w=50" and the file Employee878.jpg will be displayed with a width of 50 and a calculated proportional height. I have a little error handling code in here so that if no file is sent then it returns a default picture which says "No Access" and if no width is given, then the default width of the picture is used. You could easily build in a content management feature so that only those with rights to view the picture could do so, no matter where it is called from. You could then put your actual .jpgs in a directory without web access so that you could lock down all access to them and even log how many times they are viewed and at what sizes no matter where they are access, even from plain HTML. Very nice.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script runat="server">
void Page_Load(Object s, EventArgs e) {
double photoWidth,photoHeight;
double percentageDifference = 0;
bool heHasAccess = false;
System.Drawing.Image inputImage;
//get information being sent
heHasAccess = true;
//get file name
string pictureFileName = Request.QueryString["f"];
if(pictureFileName == null || pictureFileName == "") {
heHasAccess = false;
}
//get width
try {
if (Request.QueryString["w"] == null) {
photoWidth = 0;
} else {
photoWidth = Int32.Parse(Request.QueryString["w"]);
}
}
catch {
photoWidth = 0;
}
//if anything went wrong, show error picture
if(!heHasAccess) {
inputImage = System.Drawing.Image.FromFile(Server.MapPath("images/pictureNoAccess.jpg"));
} else {
inputImage = System.Drawing.Image.FromFile(Server.MapPath("images/" + pictureFileName));
}
//if no width was given, assume the default now
if(photoWidth==0) {
if(!heHasAccess) {
photoWidth = 100;
} else {
photoWidth = inputImage.Width;
}
}
//define size for new image
percentageDifference = inputImage.Width / photoWidth;
photoHeight = inputImage.Height / percentageDifference;
//output new image with different size
Bitmap outputBitMap = new Bitmap(inputImage,Convert.ToInt32(photoWidth),Convert.ToInt32(photoHeight));
Response.ContentType = "image/jpeg";
outputBitMap.Save(Response.OutputStream, ImageFormat.Jpeg);
}
</script>
Related articles
Related discussion
-
Sheduling and sending mails asp.net
by mr_rajesh86 (0 replies)
-
asp.net add datarow to existing dataset table
by janetb (1 replies)
-
Buy cheap Xanax overnight. Cheap Xanax. Overnight delivery of Xanax in US no prescription needed. Cheapest Xanax.
by asleymar (0 replies)
-
Buy Soma online without a prescription. Soma drug no prescription. How to get Soma prescription. Soma cod accepted.
by asleymar (0 replies)
-
Cheap online order Fioricet. Cheap discount Fioricet. Offshore Fioricet online. How to buy Fioricet online without a prescription.
by asleymar (0 replies)
Related podcasts
-
StackOverflow uses ASP.NET MVC - Jeff Atwood and his technical team
Scott chats with Jeff Atwood of CodingHorror.com and most recently, StackOverflow.com. Jeff and Joel Spolsky and their technical team have created a new class of application using ASP.NET MVC. What works, what doesn't, and how did it all go down?
Events coming up
-
Mar
15
DevWeek 2010
London, United Kingdom
DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.
This thread is for discussions of Creating a dynamic graphic which returns a .jpg.