2012-12-21 39 views
0

我一直在想這個,但我沒有得到任何地方。我想要做的是這樣的:我有一個aspx頁面,您可以在其中上傳圖像(它們存儲在服務器上的文件夾中),在一個頁面上可以看到所有上傳的圖像,並生成鏈接(一個標籤)參考這些圖像,但直到現在它將全部圖像作爲「縮略圖」加載,並且它們的尺寸太大(1920x1200px),所以我用通用處理程序替換了圖像src,應該從圖像中獲取圖像文件夾,然後返回它,調整大小像說209x133px。如何從服務器文件夾中的圖像生成縮略圖?

但我不知道從哪裏開始,我會感激任何舉行,也許有人曾經做過類似的事情。

無論如何,感謝提前

這是我ceate的鏈接和圖像與中繼器:

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; 
     } 
    } 

} 
+1

請參閱此問題http://stackoverflow.com/questions/2808887/create-thumbnail-image – Amitd

+0

和這篇文章http://msdn.microsoft.com/en-us/library/system.drawing .image.getthumbnailimage.aspx – Amitd

+0

選中此:http://www.dotnetspark.com/kb/1989-http-handlers-for-handling-images-on-fly.aspx – JurgenStillaert

回答

0

添加System.Drawing中System.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; 
    } 

詳細文章:How to Resize Image While Uploading in Asp.net C#

0

這裏有一些代碼可能需要一些調整,但可以幫助您繼續前進。

// 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)); 
    } 
} 

,並就如何使Thubnail一個問題:make thumbnail from database image while keeping aspect ratio

一些評論。如果您使用處理程序製作縮略圖,則需要花費大量時間重新制作相同和相同的縮略圖。我建議跟蹤縮略圖並將它們保存在磁盤上,然後使用從磁盤直接導入文件。

0

我們採用的方法是這樣的:

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; 
}