using System;

public partial class GetImage : System.Web.UI.Page
{
    public bool ThumbnailCallback()
    {
        return false;
    }
    public System.Drawing.Image GetThumb(string ImagePath, int Size)
    {
        if (Size > 0)
        {
            System.Drawing.Bitmap myBitmap = new System.Drawing.Bitmap(ImagePath);
            System.Drawing.Image.GetThumbnailImageAbort myCallback =
                new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
            int ResolutionHorizontal;
            int ResolutionVertical;
            if (myBitmap.Width > myBitmap.Height) // Querformat
            {
                ResolutionHorizontal = Size;
                ResolutionVertical = (Size * myBitmap.Height) / myBitmap.Width;
            }
            else // Hochformat
            {
                ResolutionVertical = Size;
                ResolutionHorizontal = (Size * myBitmap.Width) / myBitmap.Height;
            }
            System.Drawing.Image myThumbnail = myBitmap.GetThumbnailImage(ResolutionHorizontal, ResolutionVertical, myCallback, IntPtr.Zero);
            return myThumbnail;
        }
        else
        {
            System.Drawing.Image myOriginal = System.Drawing.Image.FromFile(ImagePath);
            return myOriginal;
        }
    }
    void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.Dictionary<string, string> MimeTypes =
            new System.Collections.Generic.Dictionary<string, string>();
        MimeTypes.Add("gif", "image/gif");
        MimeTypes.Add("jpg", "image/jpeg");
        MimeTypes.Add("jpeg", "image/jpeg");
        MimeTypes.Add("png", "image/png");
        MimeTypes.Add("bmp", "image/bmp");

        string PathToFiles = Request.PhysicalApplicationPath;
        if (Request.QueryString.Count > 0)
        {
            string FileToShow = Request.QueryString["fi"];
            if (FileToShow == null)
                return;
            int Resolution = 0;
            if (Request.QueryString["res"] == null)
                Resolution = 0;
            else
                Resolution = int.Parse(Request.QueryString["res"]);
            string strExtension = FileToShow.Substring(FileToShow.IndexOf(".") + 1).ToLower();
            Response.AddHeader("Content-Type", MimeTypes[strExtension]);
            System.Drawing.Image img = GetThumb(PathToFiles+FileToShow, Resolution);
            System.Drawing.Imaging.ImageFormat imgF;
            switch (strExtension)
            {
                case "gif":
                    imgF = System.Drawing.Imaging.ImageFormat.Gif;
                    break;
                case "jpg": case "jpeg":
                    imgF = System.Drawing.Imaging.ImageFormat.Jpeg;
                    break;
                case "png":
                    imgF = System.Drawing.Imaging.ImageFormat.Png;
                    break;
                case "bmp":
                    imgF = System.Drawing.Imaging.ImageFormat.Bmp;
                    break;
                default:
                    return;
            }
            img.Save(Response.OutputStream, imgF);
        }
    }
}
