Resizing Images Without Components

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eglons
    New Member
    • Apr 2006
    • 1

    Resizing Images Without Components

    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.
  • menon
    New Member
    • Aug 2006
    • 4

    #2
    Hi Guys!

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

    <%@ Page Language="C#" ContentType="te xt/html" ResponseEncodin g="iso-8859-1" %>
    <script language="C#" runat="server">
    void Page_Load(Objec t sender, EventArgs e)
    {
    try{
    Response.Cache. VaryByParams["Image;Width;He ight;ForceAspec t"] = true;
    Response.Conten tType = "image/jpeg";
    System.Collecti ons.Hashtable imageOutputForm atsTable = new System.Collecti ons.Hashtable() ;
    imageOutputForm atsTable.Add(Sy stem.Drawing.Im aging.ImageForm at.Gif.Guid, System.Drawing. Imaging.ImageFo rmat.Gif);
    imageOutputForm atsTable.Add(Sy stem.Drawing.Im aging.ImageForm at.Jpeg.Guid, System.Drawing. Imaging.ImageFo rmat.Jpeg);
    imageOutputForm atsTable.Add(Sy stem.Drawing.Im aging.ImageForm at.Bmp.Guid, System.Drawing. Imaging.ImageFo rmat.Gif);
    imageOutputForm atsTable.Add(Sy stem.Drawing.Im aging.ImageForm at.Tiff.Guid, System.Drawing. Imaging.ImageFo rmat.Jpeg);
    imageOutputForm atsTable.Add(Sy stem.Drawing.Im aging.ImageForm at.Png.Guid, System.Drawing. Imaging.ImageFo rmat.Jpeg);

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

    imageLocation = Server.MapPath( Request.QuerySt ring["img"]);
    reqHeight = Convert.ToInt32 ("59"); //
    reqWidth = Convert.ToInt32 ("100");
    forceaspect = Convert.ToBoole an("False");


    System.Drawing. Bitmap origBitmap = new System.Drawing. Bitmap(imageLoc ation);
    origHeight = origBitmap.Heig ht;
    origWidth = origBitmap.Widt h;

    if (forceaspect){
    //Force Aspect Change
    newHeight = reqHeight;
    newWidth = reqWidth;
    }
    else if (origBitmap.Hei ght >= origBitmap.Widt h){
    //Portrait
    newHeight = reqHeight;
    newWidth = (int)(((double) origBitmap.Widt h / (double)origBit map.Height) * reqHeight);
    }
    else{
    //Landscape
    newWidth = reqWidth;
    newHeight = (int)(((double) origBitmap.Heig ht / (double)origBit map.Width) * reqWidth);
    }

    System.Drawing. Bitmap outputImage = new System.Drawing. Bitmap(origBitm ap, newWidth, newHeight);
    //outputImage.Set Resolution(24, 24); //use this line for lower resolution thumbnails
    outputImage.Set Resolution(72, 72);

    //outputImage.Int erpolationMode = InterpolationMo de.HighQualityB icubic;
    System.Drawing. Imaging.ImageFo rmat outputFormat = (System.Drawing .Imaging.ImageF ormat)imageOutp utFormatsTable[origBitmap.RawF ormat.Guid];

    outputImage.Sav e(Response.Outp utStream, outputFormat);
    outputImage.Dis pose();
    origBitmap.Disp ose();
    }
    catch (Exception ex){
    //log error so we may know the problem. you need to have write permits, of course on log path
    System.IO.Strea mWriter sw=null;
    try{
    sw=new System.IO.Strea mWriter(Server. MapPath("error. txt"),true);
    sw.WriteLine("E rror : " + ex.Message + " processing " + Request.QuerySt ring["img"]);
    }
    catch{}
    finally{sw.Clos e();}
    //now display the error image
    Response.Redire ct("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

    Comment

    Working...