【问题标题】:Display thumbnails in C#在 C# 中显示缩略图
【发布时间】:2011-07-01 06:32:36
【问题描述】:

我正在使用 C# 开发专辑查看器。作为应用程序的一部分,我需要将所选文件夹的所有图像显示为缩略图。我不知道如何创建缩略图。

完成该功能的任何代码、链接或建议都会非常有帮助。谢谢。

【问题讨论】:

  • 你卡在哪里了?获取文件夹的文件列表、从文件中读取图像、创建该图像的缩略图版本、显示该缩略图,还是其他?
  • @hans : 创建图片的缩略图版本

标签: c# image thumbnails


【解决方案1】:
public System.Drawing.Image GetThumbnailImage(int thumbWidth, int thumbHeight, System.Drawing.Image.GetThumbnailImageAbort callback, System.IntPtr callbackData)
    Member of System.Drawing.Image

Summary:
Returns a thumbnail for this System.Drawing.Image.

Parameters:
thumbWidth: The width, in pixels, of the requested thumbnail image.
thumbHeight: The height, in pixels, of the requested thumbnail image.
callback: A System.Drawing.Image.GetThumbnailImageAbort delegate. In GDI+ version 1.0, the delegate is not used. Even so, you must create a delegate and pass a reference to that delegate in this parameter.
callbackData: Must be System.IntPtr.Zero.

Returns:
An System.Drawing.Image that represents the thumbnail.

【讨论】:

    【解决方案2】:

    检查this article。它可能会对您有所帮助... 代码从文本框中提供的图像创建一个 Image 对象。然后使用 Image.GetThumbnailImage(),代码创建一个大小为 100*100 的缩略图。

    【讨论】:

    • 你能告诉 zip 包含什么,这样人们就不必盲目下载了吗?
    • 我已将链接更改为指向包含 zip 文件的实际文章。这似乎更合适。
    • @Yoko:没问题。一个 zip 文件的盲链接似乎会让一些人不高兴。这样他们就可以查看文章,如果他们有兴趣,可以获取包含完整源代码的 zip 文件。
    【解决方案3】:
    [HttpPost]
        public PartialViewResult DisplayGalleryThumbs(string galleryId)
        {
            int pageNumber = Convert.ToInt32(galleryId.Split('_')[2]);
            Guid feedId = new Guid(galleryId.Split('_')[1]);
            var images = _feedDomain.ShowPostById(feedId.ToString()).Images;
    
            int totalNumberOfPages = CountNumberOfPages(images);
    
            string action = galleryId.Split('_')[3];
    
            var dict = new Dictionary<int, List<FeedImage>>();
            var pagedImages = new List<FeedImage>();
            int i = 1;
            int a = 1;
            foreach (FeedImage image in images)
            {
                pagedImages.Add(image);
                if (i % NumberOfImagesPerPage == 0)
                {
                    dict.Add(a, pagedImages);
                    a++;
                    pagedImages=new List<FeedImage>();
                }
                if (i >= images.Count()) dict.Add(a, pagedImages);
                i++;
            }
            if (action=="next")
            {
                pageNumber += 1;
            }
            else
            {
                pageNumber -= 1;
            }
    
    
            var galleryModel = new FeedThumbGalleryModel
                                   {
                                       Images = dict.FirstOrDefault(c => c.Key == pageNumber).Value,
                                       FeedId = feedId,
                                       PageNumber = pageNumber,
                                       NumberOfPages = totalNumberOfPages,
                                       NumberOfImagesPerPage = NumberOfImagesPerPage,
                                       TotalNumberOfImages = images.Count()
                                   };
    
            return PartialView("~/Views/Feed/_DisplayGalleryThumbs.cshtml", galleryModel);
        }
    
    
    
    
    @helper Render(FeedThumbGalleryModel model)
    

    {

    if (model.Images != null)
    {
    
        string linkNext = "linkNext_" + model.FeedId + "_" + model.PageNumber + "_next";
        string linkPrev = "linkPrev_" + model.FeedId + "_" + model.PageNumber + "_prev";
    
        string thumbFeedId = "feedThumbDiv_" + model.FeedId;
        <div id="@thumbFeedId">
            <table>
                <tr>
                    <td style="width: 30px;vertical-align: top;">
                        @if (model.NumberOfPages > 1)
                        {
                            if (model.PageNumber <= model.NumberOfPages && model.PageNumber > 1 || model.PageNumber == model.NumberOfPages)
                            {
                                <a class="page-action" href="#" id="@linkPrev">Prev</a>
                            }  
                        }
                    </td>
                    <td style="width: 120px;vertical-align: top">
                        @if (model.NumberOfPages>1)
                        {
                             for (int a = 1; a <= model.NumberOfPages; a++)
                             {
                                 int currentPage = a - 1;
                                 string pageLink = "linkNext_" + model.FeedId + "_" + currentPage + "_next";
                                 string classname = a==model.PageNumber ? "page-number-list" : "page-number-list1";
    
                                 <a href="#" id="@pageLink" class="@classname">@a</a>
                            }
                        }
    
                    </td>
                    <td style="width: 220px;vertical-align: top;">
                            @if (model.PageNumber < model.NumberOfPages)
                            {
                                <a class="page-action" href="#" id="@linkNext">Next</a>
                            }
                    </td>
                    <td style="width: 150px;vertical-align: top;">
                        @{var thisId = "SeeAll_" + model.FeedId;}
                        @if (model.TotalNumberOfImages > 12)
                        {
                            <a id="@thisId" class="view-all">View All</a>
                        }
    
                    </td>
                    <td style="vertical-align: top">
                        <div class="number-of-images">
                           @model.TotalNumberOfImages images  
                        </div>
    
                    </td>
                </tr>
    
                <tr>
                    <td colspan="5">
                        <div style="width: 660px;padding-top: 10px">
                            @{int i = 1;}
                            @foreach (FeedImage m in model.Images)
                            {
                                string thumbThumb = ConfigurationManager.AppSettings["DisplayThumbPath"] + m.ImageId;
                                string imageUrl = "/feed/DisplayImage/" + model.FeedId + "_" + m.ImageId;
    
                                <div class="thumbnail image">
                                    <a class='show-image' href="@imageUrl">
                                        <img border="0" class="image"  src="@thumbThumb" width="125" height="125" alt="" />
                                    </a>
                                </div>
    
                                if (i % model.NumberOfImagesPerPage == 0)
                                {
                                    break;
                                }
                                i++;
                            }
                            <br/>
                        </div>
                    </td>
    
                </tr>
            </table>
    
    
        </div>
    
    }
    

    }

    public class FeedImage
    {
    
        public Guid ImageId { get; set; }
        public string ImageUrl { get; set; }
        public string ImageName { get; set; }
        public double ImageSize { get; set; }
        public string FileExtension { get; set; }
        public string ImageType { get; set; }
        public FeedPost Post { get; set; }
        public List<FeedLike> Likes { get; set; }
        public List<FeedComment> Comments { get; set; }
        public DateTime DateCreated { get; set; }
        public string DateCreatedIso8601 { get; set; }
    }
    
    private int CountNumberOfPages(List<FeedImage> images)
        {
            if (images == null) return 0;
    
            return (images.Count() + NumberOfImagesPerPage - 1) / NumberOfImagesPerPage;
        }
    
    $("[class='page-action']").live('click', function () {
        var feedArr = $(this).attr('id').split('_');
        var thisDivId = '#feedThumbDiv_' + feedArr[1];
    
        $.post('/Feed/DisplayGalleryThumbs', { galleryId: $(this).attr('id') },
                function (html) {
                    $(thisDivId).replaceWith(html);
                });
        return false;
    });
    

    这是一个使用 MVC3 进行缩略图分页预览的 ajax 解决方案。我将其用于我的新闻提要以通过图片库列出。不要介意在视图中大量使用表格,我的 UI 有点受限。

    【讨论】:

      【解决方案4】:

      我向你推荐我过去使用的一种方式:使用 ListView 并动态加载关联的 ImageList 中的图像。您可以使用 rdkleine 提出的策略以缩略图形式加载图像。您必须遵循的重要原则是不要加载所有图像,如果用户真的看不到它们。使用列表视图,您可以通过设置 VirtualMode = true 来实现此目的,并且仅在实际显示时才提供图像。

      【讨论】:

        猜你喜欢
        • 2017-06-04
        • 1970-01-01
        • 1970-01-01
        • 2011-05-07
        • 1970-01-01
        • 2015-09-13
        • 2019-07-11
        • 2011-02-21
        • 1970-01-01
        相关资源
        最近更新 更多