【问题标题】:How can I generate a thumbnail from an image in server folder?如何从服务器文件夹中的图像生成缩略图?
【发布时间】:2012-12-08 22:09:40
【问题描述】:

我一直在试图解决这个问题,但我没有得到任何结果。我要做的是:我有一个 aspx 页面,您可以在其中上传图像(它们存储在服务器上的文件夹中),在一个页面上您可以看到所有上传的图像并生成链接(标签)参考了这些图像,但直到现在它将完整图像加载为“缩略图”并且它们的尺寸太大(1920x1200px),所以我用通用处理程序替换了图像 src,它应该从文件夹,然后将其返回,调整大小为 209x133 像素。

但我不知道从哪里开始,我将不胜感激,也许有人曾经做过类似的事情。

无论如何,提前谢谢

这就是我使用转发器创建链接和图像的方式:

protected void repImages_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.AlternatingItem ||
        e.Item.ItemType == ListItemType.Item)
    {
        string sFile = e.Item.DataItem as string;

        //Create the thumblink
        HyperLink hlWhat = e.Item.FindControl("hlWhat") as HyperLink;
        hlWhat.NavigateUrl = ResolveUrl("~/_img/_upload/" + sFile);
        hlWhat.ToolTip = System.IO.Path.GetFileNameWithoutExtension(sFile);
        hlWhat.Attributes["rel"] = "imagebox-bw";
        hlWhat.Attributes["target"] = "_blank";

        Image oImg = e.Item.FindControl("imgTheImage") as Image;
        oImg.ImageUrl = ResolveUrl("Thumbnail.ashx?img=" + sFile);
        oImg.Width = 203;
        oImg.CssClass = "galleryImgs";

    }

}

现在,我的处理程序如下所示:

<%@ WebHandler Language="C#" Class="Thumbnail" %>

using System;
using System.Web;

public class Thumbnail : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        if (!string.IsNullOrEmpty(context.Request.QueryString["img"]))
        {
            string fileName = context.Request.QueryString["img"];

        }

        else
        {

        }

    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

【问题讨论】:

标签: c# asp.net thumbnails image-resizing generic-handler


【解决方案1】:

添加 System.DrawingSystem.Drawing.Drawing2D 命名空间 你可以调整你的形象 代码隐藏

public static System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxHeight)
  {
      var ratio = (double)maxHeight / image.Height;
      var newWidth = (int)(image.Width * ratio);
      var newHeight = (int)(image.Height * ratio);
      var newImage = new Bitmap(newWidth, newHeight);
      using (var g = Graphics.FromImage(newImage))
      {
          g.DrawImage(image, 0, 0, newWidth, newHeight);
      }
      return newImage;
  }

【讨论】:

    【解决方案2】:

    这里有一些代码可能需要一些调整,但可以帮助您继续前进。

    // 1x1 transparent GIF
    private readonly byte[] GifData = {
        0x47, 0x49, 0x46, 0x38, 0x39, 0x61,
        0x01, 0x00, 0x01, 0x00, 0x80, 0xff,
        0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
        0x00, 0x2c, 0x00, 0x00, 0x00, 0x00,
        0x01, 0x00, 0x01, 0x00, 0x00, 0x02,
        0x02, 0x44, 0x01, 0x00, 0x3b
    };
    
    public void ProcessRequest(HttpContext context)
    {
        // render direct
        context.Response.BufferOutput = false;
    
        bool fFail = true;
    
        try
        {
          if (!string.IsNullOrEmpty(context.Request.QueryString["img"]))
          {
            string fileName = context.Request.QueryString["img"];         
    
            using( var inputImage = new Bitmap(fileName))
            {   
                // create the thubnail
                FinalImage = CreateThubNain();          
    
                // send it to browser
                FinalImage.Save(context.Response.OutputStream, ImageFormat.Jpeg);           
                // flag tha all ends up well
                fFail = false;
            }          
          }
        }
        catch(Exception x)
        {
            // log the error
            Debug.Fail("Check why is fail - error:" + x.ToString());
        }
    
        if(fFail)
        {
            // send something anyway
            context.Response.ContentType = "image/gif";
            context.Response.OutputStream.Write(GifData, 0, GifData.Length);
        }
        else
        {
            // this is a header that you can get when you read the image
            context.Response.ContentType = "image/jpeg";
    
            // the size of the image, saves from load the image, and send it here
            // context.Response.AddHeader("Content-Length", imageData.Length.ToString());
    
            // cache the image - 24h example
            context.Response.Cache.SetExpires(DateTime.Now.AddHours(24));
            context.Response.Cache.SetMaxAge(new TimeSpan(24, 0, 0));   
        }
    }
    

    还有一个关于如何制作缩略图的问题:make thumbnail from database image while keeping aspect ratio

    一些cmets。如果您使用处理程序制作缩略图,您将花费大量的处理时间来制作相同的和相同的。我建议跟踪缩略图并将它们保存在磁盘上,然后直接使用磁盘中的文件。

    【讨论】:

      【解决方案3】:

      我们使用这样的方法:

      private Image ScaleFreeHeight(string imagePath, int newWidth)
      {
          var byteArray = new StreamReader(imagePath).BaseStream;        
          var image = Image.FromStream(byteArray);
          var newHeight2 = Convert.ToInt32(newWidth * (1.0000000 * image.Height / image.Width));
          var thumbnail = new Bitmap(newWidth, newHeight2);
          var graphic = Graphics.FromImage(thumbnail);
          graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
          graphic.SmoothingMode = SmoothingMode.HighQuality;
          graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
          graphic.CompositingQuality = CompositingQuality.HighQuality
      
          graphic.DrawImage(image, 0, 0, newWidth, newHeight2);
      
          return thumbnail;
      }
      

      【讨论】:

        猜你喜欢
        • 2017-05-18
        • 2015-04-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-28
        • 2011-12-21
        • 2016-04-09
        • 2019-10-14
        • 2023-03-28
        相关资源
        最近更新 更多