473,396 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Resizing Images Without Components

1
Hi,

I've put a site together for a client, who now wants to Content Manage both the text and images. Text isn't a problem and I've implemented this, image replacement again isn't a problem, but I'm worried that they will simply upload 1Mb photos from their camera and wonder why the performance goes down the pan. I've used ASPJpeg on another project, but this site is on shared hosting anf they wont install components, so does anyone have a direct ASP code way of resizing and optimising images ?

Thanks in advance.
Apr 25 '06 #1
1 3652
menon
4
Hi Guys!

create a file 'thumbnail.aspx' and insert the code

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
try{
Response.Cache.VaryByParams["Image;Width;Height;ForceAspect"] = true;
Response.ContentType = "image/jpeg";
System.Collections.Hashtable imageOutputFormatsTable = new System.Collections.Hashtable();
imageOutputFormatsTable.Add(System.Drawing.Imaging .ImageFormat.Gif.Guid, System.Drawing.Imaging.ImageFormat.Gif);
imageOutputFormatsTable.Add(System.Drawing.Imaging .ImageFormat.Jpeg.Guid, System.Drawing.Imaging.ImageFormat.Jpeg);
imageOutputFormatsTable.Add(System.Drawing.Imaging .ImageFormat.Bmp.Guid, System.Drawing.Imaging.ImageFormat.Gif);
imageOutputFormatsTable.Add(System.Drawing.Imaging .ImageFormat.Tiff.Guid, System.Drawing.Imaging.ImageFormat.Jpeg);
imageOutputFormatsTable.Add(System.Drawing.Imaging .ImageFormat.Png.Guid, System.Drawing.Imaging.ImageFormat.Jpeg);

string imageLocation;
bool forceaspect = true;
int newHeight;
int newWidth;
int reqHeight = 100;
int reqWidth = 100;
int origHeight;
int origWidth;

imageLocation = Server.MapPath(Request.QueryString["img"]);
reqHeight = Convert.ToInt32("59"); //
reqWidth = Convert.ToInt32("100");
forceaspect = Convert.ToBoolean("False");


System.Drawing.Bitmap origBitmap = new System.Drawing.Bitmap(imageLocation);
origHeight = origBitmap.Height;
origWidth = origBitmap.Width;

if (forceaspect){
//Force Aspect Change
newHeight = reqHeight;
newWidth = reqWidth;
}
else if (origBitmap.Height >= origBitmap.Width){
//Portrait
newHeight = reqHeight;
newWidth = (int)(((double)origBitmap.Width / (double)origBitmap.Height) * reqHeight);
}
else{
//Landscape
newWidth = reqWidth;
newHeight = (int)(((double)origBitmap.Height / (double)origBitmap.Width) * reqWidth);
}

System.Drawing.Bitmap outputImage = new System.Drawing.Bitmap(origBitmap, newWidth, newHeight);
//outputImage.SetResolution(24, 24); //use this line for lower resolution thumbnails
outputImage.SetResolution(72, 72);

//outputImage.InterpolationMode = InterpolationMode.HighQualityBicubic;
System.Drawing.Imaging.ImageFormat outputFormat = (System.Drawing.Imaging.ImageFormat)imageOutputFor matsTable[origBitmap.RawFormat.Guid];

outputImage.Save(Response.OutputStream, outputFormat);
outputImage.Dispose();
origBitmap.Dispose();
}
catch (Exception ex){
//log error so we may know the problem. you need to have write permits, of course on log path
System.IO.StreamWriter sw=null;
try{
sw=new System.IO.StreamWriter(Server.MapPath("error.txt") ,true);
sw.WriteLine("Error : " + ex.Message + " processing " + Request.QueryString["img"]);
}
catch{}
finally{sw.Close();}
//now display the error image
Response.Redirect("images/thumberror.gif");
}
}
</script>


and you call call the thumbnail image by using this tag
<img border="0" src="thumbnail.aspx?img=/images/photo.jpg">

best regards,
Kalipurayath Sanal Menon
Aug 8 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Alex Hopson | last post by:
I'm using the code below to loop through some images and resize each image twice, once to create a thumbnail and once to create a small image. The page stops loading around the 38th image out of...
0
by: Leonard Challis | last post by:
Good morning everyone I am currently doing quite a large project in VB6 and ACCESS for college, for a Photographers company assignment. I am posting here to see if anyone has any advice on what...
2
by: Clyde Ellul | last post by:
Hi there. I need to write a simple program that reads a GIF image from an input stream, resizes it, then writes it back to an output stream in the same format (GIF). (JPEG input/output is good...
10
by: riki | last post by:
Hi, i have a big problem...i'm using one jscript for resizing of all of my pics in popUp...in main html i'm having many little pics and clicking on them they open in popUp and resize to larger...
9
by: Bryan R. Meyer | last post by:
Hello Everyone, The problem of browser resizing has become an issue for me. While redesigning my webpage, I set the left and right margins to be auto so that my content would be centered. ...
13
by: Jon Yeager | last post by:
I need to display a bunch of pictures that are all of various dimensions into a fixed dimension space, like MSN Messenger does with its user photos in the chat windows. Forcing image dimensions...
6
by: Robert J. O'Hara | last post by:
I'm one of those people who practices what some consider "dull" and others consider "elegantly conservative" page design. I appreciate good traditional typography and standards-compliant liquid...
5
by: Jim | last post by:
I've heard that resizing images through PHP (either GD2 or ImageMagick) is a processor intensive exercise. I'm setting up a site where users will be uploading up to 10 images along with the details...
10
by: mishrarajesh44 | last post by:
hii all, I am facing a problem currently.. i have a script for image uploading and resizing.. the image uploading takes place properly for every size images.. but, the resizing works for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.