2012-09-14 110 views
0

我想調整我一般的處理程序中的圖像服務器側將其轉換回從數據庫中的BLOB的圖像之後...這是我處理代碼:如何在從BLOB轉換圖像後調整圖像大小?

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

using System; 
using System.Web; 
using System.Data; 
using System.Data.SqlClient; 
using System.IO; 

public class Image : IHttpHandler { 

    public void ProcessRequest (HttpContext context) { 
     Guid id = new Guid(context.Request.QueryString["Id"]); 
     int column = 7; 

     if (context.Request.QueryString["img"] == "tbn") 
     { 
      column = 6; 
     } 

     context.Response.ContentType = "image/png"; 
     MemoryStream strm = new MemoryStream(returnImage(id, column)); 
     byte[] buffer = new byte[4096]; 
     int byteSeq = strm.Read(buffer, 0, 4096); 
     while (byteSeq > 0) 
     { 
      context.Response.OutputStream.Write(buffer, 0, byteSeq); 
      byteSeq = strm.Read(buffer, 0, 4096); 
     } 
    } 

    public Byte[] returnImage(Guid id, int column) 
    { 
     SqlConnection sqlCn = new SqlConnection("Data Source=localhost;Initial Catalog=database;User ID=user;Password=password"); 

     string qry = "SELECT * FROM Project WHERE [email protected]"; 
     SqlCommand cmd = new SqlCommand(qry, sqlCn); 
     cmd.Parameters.Add("@id", SqlDbType.UniqueIdentifier).Value = id; 
     sqlCn.Open(); 
     SqlDataReader dr = cmd.ExecuteReader(); 
     dr.Read(); 
     Byte[] ar = (Byte[])(dr[column]); 
     dr.Close(); 
     cmd.Dispose(); 
     sqlCn.Close(); 
     return ar; 
    } 

    public bool IsReusable { 
     get { 
      return false; 
     } 
    } 

} 

看起來應該wheter圖像寬度高於其高度,相反,根據設置的高度或寬度和其他值(高度/寬度)應按比例設置,因此它不會被拉伸。

我發現這是什麼:http://www.codeproject.com/Articles/25838/A-Simple-Image-Handler

但我真的不知道如何使用它...有什麼建議?感謝所有提前的幫助!

回答

0

這可能有點冗長,但它適用於我。給定blob中的內存流,再加上目標寬度和高度尺寸,它將返回一個新的流,其中包含調整大小的圖像副本。

public static Stream CreateThumbnail(Stream input, Int32 targetWidth, Int32 targetHeight) 
    { 
     output = new MemoryStream(); 
      using (Bitmap bitmap = new Bitmap(input)) 
      { 
       ImageFormat format = bitmap.RawFormat; 
       Boolean isJpeg = (format.Equals(ImageFormat.Jpeg)); 
       Boolean isPng = (format.Equals(ImageFormat.Png)); 
       Int32 width = bitmap.Width; 
       Int32 height = bitmap.Height; 
       getTargetSizes(out width, out height, bitmap, targetWidth, targetHeight); 
       using (Bitmap thumbnailBitmap = new Bitmap(width, height)) 
       { 
        Graphics G = Graphics.FromImage(thumbnailBitmap); 
        G.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
        G.DrawImage(bitmap, 0, 0, width, height); 
        thumbnailBitmap.SetResolution(72, 72); 
        if (isJpeg) 
        { 
         var codecParams = new EncoderParameters(1); 
         codecParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80L); 
         ImageCodecInfo[] arrayICI; 
         ImageCodecInfo jpegICI = null; 
         arrayICI = ImageCodecInfo.GetImageEncoders(); 
         for (int i = 0; i < arrayICI.Length; i++) 
         { 
          if (arrayICI[i].FormatDescription.Equals("JPEG")) 
          { 
           jpegICI = arrayICI[i]; 
           break; 
          } 
         } 
         thumbnailBitmap.Save(output, jpegICI, codecParams); 
        } 
        else 
        { 
         thumbnailBitmap.Save(output, ImageFormat.Png); 
        } 
       } 
      } 
     return output; 
    } 

    private static void getTargetSizes(out Int32 targetWidth, out Int32 targetHeight, Bitmap BM, Int32 maxWidth = 150, Int32 maxHeight = 150) 
    { 
     Int32 startWidth = BM.Width; 
     Int32 startHeight = BM.Height; 
     targetWidth = startWidth; 
     targetHeight = startHeight; 
     Boolean resizeByWidth = false; 
     Boolean resizeByHeight = false; 
     if ((maxWidth > 0) && (maxHeight > 0)) 
     { 
      if ((startWidth > maxWidth) || (startHeight > maxHeight)) 
      { 
       if (startHeight <= startWidth) 
       { 
        if(targetWidth > maxWidth) resizeByWidth = true; 
       } 
       else 
       { 
        if(targetHeight > maxHeight) resizeByHeight = true; 
       } 
      } 
     } 
     else if (maxWidth > 0) 
     { 
      // Resize within width only 
      if (startWidth > maxWidth) 
      { 
       if (targetWidth > maxWidth) resizeByWidth = true; 
      } 
     } 
     else if (maxHeight > 0) 
     { 
      // Resize by height only 
      if (startHeight > maxHeight) 
      { 
       if (targetHeight > maxHeight) resizeByHeight = true; 
      } 
     } 
     if (resizeByWidth) 
     { 
      targetWidth = maxWidth; 
      targetHeight = (Int32)(startHeight * ((Decimal)targetWidth/(Decimal)startWidth)); 
     } 
     if (resizeByHeight) 
     { 
      targetHeight = maxHeight; 
      targetWidth = (Int32)(startWidth * ((Decimal)targetHeight/(Decimal)startHeight)); 
     } 
    } 
} 

並且用類似稱呼它:

MemoryStream strm = new MemoryStream(returnImage(id, column)); 
strm = CreateThumbnail(strm, 100, 100); 
+0

嗯...這個代碼是巨大的:)和非常混亂......我怎麼會用這個跟我的代碼? – webster69

+0

代碼可以變得更緊湊,我會授予你 - 我只是從一箇舊項目中抓取它,我沒有時間爲你重寫它。你可以通過調用第一個函數,將它傳遞給你的內存流,以及想要調整圖像大小的寬度和高度,並將結果放回到內存流中來使用它。我會添加一個例子。 – JcFx