2014-07-22 35 views
0

现在,我保存2大小的图像在数据库中,真正的大小和缩略图,然后显示他们当我需要。显示图像处理程序与调整大小

在ashx的处理程序我设置我需要哪种类型,这里

是我的代码:

  string field = context.Request.QueryString["field"]; 
      string table = context.Request.QueryString["table"]; 
      string id = context.Request.QueryString["id"]; 

      string conn = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString; 
      SqlConnection myConnection = new SqlConnection(conn); 
      myConnection.Open(); 

      string sql = ""; 
      sql = "Select " + field + ", pictureType from " + table + " where [email protected]"; 
      SqlCommand cmd = new SqlCommand(sql, myConnection); 

      cmd.Parameters.Add("@imageId", SqlDbType.Int).Value = id; 
      cmd.Prepare(); 
      SqlDataReader dr = cmd.ExecuteReader(); 
      dr.Read(); 

      context.Response.ContentType = dr["pictureType"].ToString(); 
      context.Response.BinaryWrite((byte[])dr[field]); 
      dr.Close(); 
      myConnection.Close(); 

,我用这样的方式:

<img src="handlers/ShowPic.ashx?table=tblEnBackGrounds&field=image&id=1" alt="s" /> 

,但现在我决定要救就真实大小的图像,然后在ashx文件中重新调整大小并显示正确的类型(这里是真实或缩略图)。

现在我需要第一个到现在是好还是不好?第二我没有任何想法如何重新大小二进制数据之前显示在ashx处理程序

回答

0

使用伟大的图像调整大小库文件http://imageresizing.net/

您需要按比例调整图像大小并保持其宽高比。使用图像大小调整lib下,这样的事情可能是做这项工作的功能:

public static byte[] resizeProportionaly(byte[] imageFile, int pWidth, int pHeight, string crop = "&crop=auto") 
    { 
     MemoryStream stream = new MemoryStream(imageFile); 
     stream.Seek(0, SeekOrigin.Begin); 
     ResizeSettings resizeCropSettings = new ResizeSettings(string.Format("bgcolor=FFFFFF&width={0}&height={1}&format=jpg" + crop, pWidth, pHeight)); 
     Image originalImage = Image.FromStream(stream, true, false); 


     Image resizedImage = ImageBuilder.Current.Build(originalImage, resizeCropSettings); 
     MemoryStream ms = new MemoryStream(); 
     resizedImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 
     return ms.ToArray(); 
    } 

,并在您的处理程序是这样的:

public void ProcessRequest (HttpContext context) { 
    // some init code...take care of your query strings here etc. 

    string cropString = "x1,y1,x2,y2"; // if you need croping? 
    // change height and width based on your picture type 
    int outWidth = 640; // whatever you need here 
    int outHeight = 480; 

    System.Data.DataRow data = GetDBPhotoRecord(photoId); 
    byte[] photoStream = (byte[])data[mxmPhotos.Photo]; 
    // if you dont need cropping just remove the last cropString parameter 
    byte[] outStream = resizeProportionaly(photoStream, outWidth, outHeight, cropString); 

    // ... 

    context.Response.Buffer = true; 
    context.Response.Clear(); 
    context.Response.ContentType = "image/jpeg"; 
    context.Response.Cache.SetCacheability(HttpCacheability.Public); 
    context.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(7)); 
    context.Response.BinaryWrite(outStream); 
} 
+0

谢谢ivan.sivak,让我查一下 – user3400838

+0

你很受欢迎。这对我来说很好:) –

+0

什么ResizeSettings和ImageBuilder在你的代码?这是考虑图片方面? – user3400838

相关问题