创建函数以在文件夹中创建可用图像的thambnails

时间:2009-08-28 02:53:50

标签: asp.net vb.net image directory thumbnails

我创建了一个CMS,使用

将所有图像上传到文件夹,将所有图像上传到文件夹
file.SaveAs(Server.MapPath("../images/") + advertID.ToString + "_" + i.ToString + fileExt)

现在,所有图像都已保存,我忘记了它创建缩略图。 :(

我需要一次阅读所有图片并创建缩略图,

myimg = System.Drawing.Image.FromFile(imgFileName)
myimg = myimg.GetThumbnailImage(154, 94, Nothing, IntPtr.Zero)
myimg.Save(Server.MapPath("../Content/") + "Thumb_" + imgFileName, myimg.RawFormat)

我需要这个才能快速运作。我似乎不知道如何逐个阅读这些图像名称。

提前致谢。

1 个答案:

答案 0 :(得分:1)

this的略微修改版本:

public void createThumbnails(string sourceDir) 
{
    //Process the list of files found in the directory. 
    string [] fileEntries = Directory.GetFiles(sourceDir);

    foreach(string fileName in fileEntries)
    {
        //Do resizing here.
        myimg = System.Drawing.Image.FromFile(fileName);
        myimg = myimg.GetThumbnailImage(154, 94, Nothing, IntPtr.Zero);
        myimg.Save(Server.MapPath("../Content/") + "Thumb_" + fileName, myimg.RawFormat);
    }
}

这样称呼:

createThumbnails("C:\Path\To\Images");

*编辑:使用converter工具等效的VB.NET:

Public Sub createThumbnails(ByVal sourceDir As String)
    Dim fileEntries As String() = Directory.GetFiles(sourceDir)

    For Each fileName As String In fileEntries
        myimg = System.Drawing.Image.FromFile(fileName)
        myimg = myimg.GetThumbnailImage(154, 94, [Nothing], IntPtr.Zero)
        myimg.Save((Server.MapPath("../Content/") & "Thumb_") + fileName, myimg.RawFormat)
    Next
End Sub