JSN ImageShow - Joomla 1.5 extension (component, module) by JoomlaShine.com
Home arrow Blog arrow ASP.Net handler for serving MultiScaleImage tiles (DeepZoom)
ASP.Net handler for serving MultiScaleImage tiles (DeepZoom) Stampa E-mail
Scritto da Gene   
domenica 28 settembre 2008

Couple of weeks ago I was playing with MultiScaleImage and DeepZoom . I tryied DeepZoom Composer though I wanted to try another approach also. A server side (on the fly) tile processing one. I mean: what if I just want to put on my web site a very big high resolution image with no other worries about composing tiles but just linking it in the xaml code?

So I wrote an ASP.Net web handler (ashx) that automatically crops tiles requested by MultiScaleImage class.

Open the full article to see the source code.

ImageTileSource.cs

(the ImageTileSource class that is meant to be set as Source property of the MultiScaleImage class)


public class ImageTileSource : MultiScaleTileSource
{
	int _tileSizeX;
	int _tileSizeY;
	int _imageWidth;
	int _imageHeight;
	string _path;

	public ImageTileSource(string imagePath, int imageWith, int imageHeight, int tileSizeX, int tileSizeY) :
							base(imageWith, imageHeight, tileSizeX, tileSizeY, 0)
	{
		_path = imagePath;
		_tileSizeX = tileSizeX;
		_tileSizeY = tileSizeY;
		_imageWidth = imageWith;
		_imageHeight = imageHeight;
	}

	protected override void GetTileLayers(int tileLevel, int tilePositionX, int tilePositionY,
											IList tileImageLayerSources)
	{
		string source = string.Format(
		  Page.GetBasePath() + "/Handler.ashx" +
		  "?tileLevel={0}&tilePositionX={1}&tilePositionY={2}&tileSizeX={3}&tileSizeY={4}" +
		  "&imageWidth={5}&imageHeight={6}&path={7}",
		  tileLevel, tilePositionX, tilePositionY, _tileSizeX, _tileSizeY,
		  _imageWidth, _imageHeight, _path);

		Uri uri = new Uri(source, UriKind.Absolute);

		tileImageLayerSources.Add(uri);
	}
}
 

 

Handler.ashx

(the web handler for serving tiles. you can also add a caching mechanism by saving each tiles and serving cached file if present. -- highly recommended =) )


using System;
using System.Web;
using System.Drawing;

public class Handler : IHttpHandler {
        
    public void ProcessRequest (HttpContext context) {

        if (context.Request.QueryString.HasKeys())
        {
            string path = context.Server.MapPath(context.Request.QueryString.Get("path"));
			int tileLevel = int.Parse(context.Request.QueryString.Get("tileLevel"));
			int tilePosX = int.Parse(context.Request.QueryString.Get("tilePositionX"));
			int tilePosY = int.Parse(context.Request.QueryString.Get("tilePositionY"));
			int tileSizeX = int.Parse(context.Request.QueryString.Get("tileSizeX"));
			int tileSizeY = int.Parse(context.Request.QueryString.Get("tileSizeY"));
			int imageWidth = int.Parse(context.Request.QueryString.Get("imageWidth"));
			int imageHeight = int.Parse(context.Request.QueryString.Get("imageHeight"));
			//
			Bitmap bitmap = DrawTile(path, tileLevel, tilePosX, tilePosY, tileSizeX, tileSizeY,
				imageWidth, imageHeight);
			if (bitmap != null)
			{
				context.Response.ContentType = "img/png";
				System.IO.MemoryStream ms = new System.IO.MemoryStream();
				bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
				ms.WriteTo(context.Response.OutputStream);
				// here we should cache the tile
			}
        }
    }

    private Bitmap DrawTile(string path, int tileLevel, int tilePosX, int tilePosY,
        int tileSizeX, int tileSizeY, int imageWidth, int imageHeight)
    {
        Bitmap bmpImage = new System.Drawing.Bitmap(path);

        int maxTileLevel = (int)Math.Max(
            Math.Ceiling(Math.Log(imageWidth, 2)), Math.Ceiling(Math.Log(imageHeight, 2)));

        int currentImageDivisor = maxTileLevel - tileLevel;

        int naturalWidth =
            Math.Max((int)(imageWidth / Math.Pow(2, currentImageDivisor)), tileSizeX);

        int naturalHeight =
            Math.Max((int)(imageHeight / Math.Pow(2, currentImageDivisor)), tileSizeY);

        Bitmap bb = new Bitmap(naturalWidth, naturalHeight);
        Graphics g = Graphics.FromImage((Image)bb);
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        g.DrawImage(bmpImage, 0, 0, naturalWidth, naturalHeight);
        g.Dispose();

        Bitmap b = new Bitmap(tileSizeX, tileSizeY);
        try
        {
            int dx = tileSizeX;
            int dy = tileSizeY;
            if ((tilePosX * tileSizeX) + tileSizeX > naturalWidth)
				dx = tileSizeX - ((tilePosX * tileSizeX) + tileSizeX - naturalWidth);
            if ((tilePosY * tileSizeY) + tileSizeY > naturalHeight)
				dy = tileSizeY - ((tilePosY * tileSizeY) + tileSizeY - naturalHeight);
            b = bb.Clone(new System.Drawing.Rectangle(new Point(tilePosX * tileSizeX,tilePosY * tileSizeY),
														new Size(dx, dy)),
														bmpImage.PixelFormat);
        }
        catch (Exception e)
        {
        }

        return (b);
    }
    
        
    public bool IsReusable {
        get {
            return true;
        }
    }

}
 

Commenti
Nuovo Cerca RSS
Commenta
Nome:
Email:
 
Website:
Titolo:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Ultimo aggiornamento ( domenica 28 settembre 2008 )
 
< Prec.   Pros. >
Free Joomla Templates