Пример #1
0
    public static void DeleteImage(int id, string guid, ImageType imageType)
    {
        var physPath =
            Path.Combine(Path.Combine(Path.Combine(ImageCacheDirectory, GetSubdir(imageType)), guid.Substring(0, 2)),
                         guid.Substring(2, 2));

        switch (imageType)
        {
            case ImageType.EventCommentPreview:
            case ImageType.EventCommentThumbnail:
                physPath = Path.Combine(physPath, "comments");
                break;
            case ImageType.EventPicturePreview:
            case ImageType.EventPictureThumbnail:
            case ImageType.EventPictureThumbnailSmall:
            case ImageType.EventPictureThumbnailSlideShow:
                physPath = Path.Combine(physPath, "pictures");
                break;
        }

        DirectoryInfo dir = new DirectoryInfo(physPath);
        if (!dir.Exists)
        {
            dir.Create();
        }
        var extensions = Extensions.Split(new[] {';'});
        var files = new List<FileInfo>();
        foreach (string s in extensions)
        {
            files.AddRange(dir.GetFiles(s));
        }

        ImageRepository repository = new ImageRepository();
        var imagePath = repository.GetImagePath(id, imageType);

        foreach (var file in files)
        {
            var filename = Path.GetFileNameWithoutExtension(file.Name);
            filename = filename.Substring(0, filename.Substring(0, filename.LastIndexOf("_")).LastIndexOf("_"));
            if (filename == Path.GetFileNameWithoutExtension(imagePath))
            {
                file.Delete();
            }
        }

        var imageFullPath = Path.Combine(Path.Combine(ImageCacheDirectory, GetSubdir(imageType)), imagePath);
        if (File.Exists(imageFullPath))
        {
            File.Delete(imageFullPath);
        }
    }
Пример #2
0
    /// <summary>
    /// Returns the RELATIVE path to the image on the website
    /// </summary>
    /// <param name="id">The GUID.</param>
    /// <param name="subdir">The subdir.</param>
    /// <param name="width">The width.</param>
    /// <param name="height">The height.</param>
    /// <param name="radius">The radius.</param>
    /// <param name="resultWidth">width of the resized image</param>
    /// <param name="resultHeight">Height of the resized image</param>
    /// <returns></returns>
    private static string GetRelativeImagePath(int id, ImageType imageType, string physPath, string virtPath, int width, int height, int radius, bool overrideImage, ref int resultWidth, ref int resultHeight)
    {
        switch (imageType)
        {
            case ImageType.EventCommentPreview:
            case ImageType.EventCommentThumbnail:
                physPath = Path.Combine(physPath, "comments");
                virtPath = string.Format("{0}/{1}", virtPath, "comments");
                break;
            case ImageType.EventPicturePreview:
            case ImageType.EventPictureThumbnail:
            case ImageType.EventPictureThumbnailSmall:
            case ImageType.EventPictureThumbnailSlideShow:
                physPath = Path.Combine(physPath, "pictures");
                virtPath = string.Format("{0}/{1}", virtPath, "pictures");
                break;
        }
        HttpServerUtility server = HttpContext.Current.Server;
        DirectoryInfo dir = new DirectoryInfo(physPath);
        if (!dir.Exists)
        {
            dir.Create();
        }
        var extensions = Extensions.Split(new[] {';'});
        var files = new List<FileInfo>();
        foreach (string s in extensions)
        {
            files.AddRange(dir.GetFiles(s));
        }

        var repository = new ImageRepository();
        string filename = repository.GetImagePath(id, imageType);
        string thmName = Path.GetFileNameWithoutExtension(filename) + "_" + width + "_" + height + ".jpg";
        string thmNameWithoutExtension=Path.GetFileNameWithoutExtension(thmName);

        if (!overrideImage)
        {
            foreach (var file in files)
            {

                {
                    if (Path.GetFileNameWithoutExtension(file.Name) == thmNameWithoutExtension)
                    {Image img = Image.FromFile(file.FullName);
                        resultWidth = img.Width;
                        resultHeight = img.Height;
                        return string.Format("{0}/{1}", virtPath, file.Name);
                    }
              }
            }
        }
        if (string.IsNullOrEmpty(filename))
        {
            return string.Format("{0}/{1}", virtPath, thmName);
        }
        switch (imageType)
        {
            case ImageType.UserOriginal:
            case ImageType.EventOriginal:
            case ImageType.EventCommentOriginal:
            case ImageType.EventPictureOriginal:
                return string.Format("{0}/{1}", Path.GetDirectoryName(Path.GetDirectoryName(virtPath)), filename);
        }
        string fullPath;
        if (!Path.IsPathRooted(physPath))
        {
            fullPath =
                server.MapPath("~\\" +
                               Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(physPath)), filename));
        }
        else
        {
            if (imageType == ImageType.EventCommentPreview ||
                imageType == ImageType.EventCommentThumbnail ||
                imageType == ImageType.EventPicturePreview ||
                imageType == ImageType.EventPictureThumbnail ||
                imageType == ImageType.EventPictureThumbnailSmall ||
                imageType == ImageType.EventPictureThumbnailSlideShow
                )
            {
                fullPath =
                    Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(physPath))),
                                 filename);
            }
            else
            {
                fullPath = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(physPath)), filename);
            }
        }
        if (!File.Exists(fullPath))
        {
            return string.Format("{0}/{1}", virtPath, thmName);
        }

        using (var sourceImage = Image.FromFile(fullPath))
        {
            using (var targetImage = Resize(sourceImage, width, height, radius, imageType != ImageType.EventPictureThumbnailSlideShow, imageType))
            {
                targetImage.Save(physPath + @"\" + thmName, ImageFormat.Jpeg); //save as jpg only
                resultWidth = targetImage.Width;
                resultHeight = targetImage.Height;
            }
        }
        return string.Format("{0}/{1}", virtPath, thmName);
    }