using System;
using System.IO;
using System.Web.UI.WebControls;
using System.Collections;

public partial class Bilder : System.Web.UI.Page
{
    string[] strImages = new string[] { ".gif", ".jpg", ".png", ".bmp" };
    const string strImageDirectory = "images";
    const int Cols = 4;

    class MyImage
    {
        const bool DrawBorder = true;
        const bool DrawPictureName = true;
        const bool DrawPictureProperties = true;
        const int BorderWidth = 1;
        System.Drawing.Color BorderColor = System.Drawing.Color.Maroon;
        const int PictureSizeThumb = 100;
        const int PictureSizeView = 600;
        const int PictureBorderWidth = 5;
        const int FontSizeName = 10;
        const int FontSizeDescription = 8;

        Image _imgWeb;
        string _Description;
        string _Name;
        string _Extension;
        int _PictureSizeThumb;

        public MyImage(FileInfo ImageFile, string strImageDirectory)
        {
            // Bestimmen der Abmessungen des Bildes 
            _PictureSizeThumb = PictureSizeThumb;
            System.Drawing.Image imgFile = System.Drawing.Image.FromFile(ImageFile.FullName);
            int Height = imgFile.Height;
            int Width = imgFile.Width;
            _Name = ImageFile.Name.Substring(0, ImageFile.Name.IndexOf("."));
            _Extension = ImageFile.Extension.Substring(1).ToUpper();
            _Description = _Extension + " " +
                imgFile.PhysicalDimension.Width + "x" + imgFile.PhysicalDimension.Height + "px " +
                imgFile.HorizontalResolution + "x" + imgFile.VerticalResolution + "dpi " +
                imgFile.PixelFormat + " " +
                "Erstellt:"+ImageFile.CreationTime.ToString("d") + " " +
                "Zugriff:" + ImageFile.LastAccessTime.ToString("d") + " " +
                "Geändert:" + ImageFile.LastWriteTime.ToString("d");
            _imgWeb = new Image();
            _imgWeb.ImageUrl = "GetImage.aspx?fi=" + strImageDirectory + "/" + ImageFile.Name + "&res=" + PictureSizeThumb; 
        }
        public Table BuildImageTable()
        {
            Table tImg = new Table();
            // Bildzeile
            TableRow tImgRow = new TableRow();
            TableCell tImgCell = new TableCell();
            HyperLink hl = new HyperLink();
            hl.NavigateUrl = "javascript:PIC('GetImage.aspx?fi=" + strImageDirectory + "/" + _Name + "." + _Extension + "&res=" + PictureSizeView + "')";
            hl.Controls.Add(_imgWeb);
            tImgCell.Controls.Add(hl);
            tImgCell.HorizontalAlign = HorizontalAlign.Center;
            tImgCell.VerticalAlign = VerticalAlign.Middle;
            tImgCell.Width = _PictureSizeThumb;
            tImgCell.Height = _PictureSizeThumb;
            if (DrawBorder)
            {
                tImgCell.BorderColor = BorderColor;
                tImgCell.BorderWidth = BorderWidth;
            }
            tImgRow.Cells.Add(tImgCell);
            tImg.Rows.Add(tImgRow);
            if (DrawPictureName) // Bildname
            {
                tImgRow = new TableRow();
                tImgCell = new TableCell();
                Label l = new Label();
                l.Text = _Name;
                l.Width = _PictureSizeThumb;
                l.Font.Size = FontSizeName;
                tImgCell.Controls.Add(l);
                tImgCell.HorizontalAlign = HorizontalAlign.Center;
                tImgCell.VerticalAlign = VerticalAlign.Top;
                tImgRow.Cells.Add(tImgCell);
                tImg.Rows.Add(tImgRow);
            }
            if (DrawPictureProperties) // Bildbeschreibung
            {
                tImgRow = new TableRow();
                tImgCell = new TableCell();
                Label l = new Label();
                l.Text = _Description;
                l.Width = _PictureSizeThumb;
                l.Font.Size = FontSizeDescription;
                tImgCell.Controls.Add(l);
                tImgCell.HorizontalAlign = HorizontalAlign.Center;
                tImgCell.VerticalAlign = VerticalAlign.Top;
                tImgRow.Cells.Add(tImgCell);
                tImg.Rows.Add(tImgRow);
            }
            tImg.BorderWidth = PictureBorderWidth;
            tImg.BorderColor = System.Drawing.Color.Transparent;
            return tImg;
        }
    }
    private bool IsImage(String s)
    {
        for (int i = 0; i < strImages.Length; i++)
        {
            if (s == strImages[i])
                return true;
        }
        return false;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        ContentPlaceHolder mpContentPlaceHolder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder_Content");
        PlaceHolder phImages = (PlaceHolder)mpContentPlaceHolder.FindControl("PlaceHolder_Images");
        if (phImages == null) return;
        
        string strApplPath = Request.PhysicalApplicationPath;
        string strImgPath = strApplPath+strImageDirectory+"\\";
        DirectoryInfo di = new DirectoryInfo(strImgPath);
        FileInfo[] fiFiles = di.GetFiles();
        Table t = new Table();
        TableRow tr = new TableRow();
        int imgNumber = 0;
        for (int i = 0; i < fiFiles.Length; i++)
        {
            if (!IsImage(fiFiles[i].Extension.ToLower()))
                continue;
            if (imgNumber % Cols == 0)
            {
                if (imgNumber != 0)
                    t.Rows.Add(tr);
                tr = new TableRow();
            }
            imgNumber++;
            TableCell tc = new TableCell();
            tc.VerticalAlign = VerticalAlign.Top;
            // Response.Write(strImageDirectory + "<br/>"); Response.Flush();
            MyImage img = new MyImage(fiFiles[i], strImageDirectory);
            tc.Controls.Add(img.BuildImageTable());
            tr.Cells.Add(tc);
        }
        t.Rows.Add(tr);
        phImages.Controls.Add(t);
    }
}
