I have a PHP script which I am trying to convert to ASP.NET C#
Here is the PHP:
<?php
/* Configuration Start */
$thumb_directory = 'img/thumbs';
$orig_directory = 'img/original';
$stage_width=600; // How big is the area the images are scattered on
$stage_height=400;
/* Configuration end */
$allowed_types=array('jpg','jpeg','gif','png');
$file_parts=array();
$ext='';
$title='';
$i=0;
/* Opening the thumbnail directory and looping through all the thumbs: */
$dir_handle = @opendir($thumb_directory) or die("There is an error with your image directory!");
$i=1;
while ($file = readdir($dir_handle))
{
/* Skipping the system files: */
if($file=='.' || $file == '..') continue;
$file_parts = explode('.',$file);
$ext = strtolower(array_pop($file_parts));
/* Using the file name (withouth the extension) as a image title: */
$title = implode('.',$file_parts);
$title = htmlspecialchars($title);
/* If the file extension is allowed: */
if(in_array($ext,$allowed_types))
{
/* Generating random values for the position and rotation: */
$left=rand(0,$stage_width);
$top=rand(0,400);
$rot = rand(-40,40);
if($top>$stage_height-130 && $left > $stage_width-230)
{
/* Prevent the images from hiding the drop box */
$top-=120+130;
$left-=230;
}
/* Outputting each image: */
echo '
<div id="pic-'.($i++).'" class="pic" style="top:'.$top.'px;left:'.$left.'px;background:url('.$thumb_directory.'/'.$file.') no-repeat 50% 50%; -moz-transform:rotate('.$rot.'deg); -webkit-transform:rotate('.$rot.'deg);">
<a class="fancybox" rel="fncbx" href="'.$orig_directory.'/'.$file.'" target="_blank">'.$title.'</a>
</div>';
}
}
/* Closing the directory */
closedir($dir_handle);
?>
I am struggling to get the array part working. Is the PHP setting up an array called file_parts? Does this mean I have enumerate the directory in ASP.NET?
This is what I have tried so far, still work in progress:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class web_content_notes_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
/* Configuration Start */
string thumb_directory;
string orig_directory;
thumb_directory = ("img/thumbs");
orig_directory = ("img/original");
// How big is the area the images are scattered on
int stage_width;
int stage_height;
stage_width = 600;
stage_height = 400;
/* Configuration end */
// array of allowed file type extensions
string[] allowed_types = { "bmp", "gif", "png", "jpg", "jpeg", "doc", "xls" };
//declare the array
string[] file_parts;
string ext = "";
string title = "";
int i = 0;
/* Opening the thumbnail directory and looping through all the thumbs: */
string dir_handle;
dir_handle = thumb_directory;
string file;
while (file = dir_handle);
{
Console.WriteLine(file_parts[i]);
}
//.....
}
}
How do I loop through the array? bit confused as the array isn’t filled until the directory is read.
You can use the
System.IO.Directory‘sGetFilesmethod (MSDN) to achieve this;It’ll be worth having a look at the
System.IO.Pathclass (MSDN) as it gives you some nice methods likeGetFileNameWithoutExtension()andGetExtension().Edit: Full conversion;