2013-07-01 101 views
0

我正在做一个小型项目,我必须将所有图像从数据库显示到Listview。 我传递图像ID,宽度和高度查询字符串参数。如何调整图像大小以显示数据库

<asp:Image ID="Image1" runat="server" ImageUrl='<%#"~/Handler/ImageHandler.ashx?ImgHeight=150&ImgWidth=200&ImgID="+Eval("Image_ID")%>' Height="150px" Width="200px"/> 
public void ProcessRequest (HttpContext context) 
    { 

    string imgwidth = context.Request.QueryString["ImgWidth"]; 
    string imgheight = context.Request.QueryString["ImgHeight"]; 
    string imageid = context.Request.QueryString["ImgID"]; 
    if (imgwidth != string.Empty && imgheight != string.Empty && (imgwidth != null && imgheight != null)) 
    { 
     if (!System.Web.UI.WebControls.Unit.Parse(imgwidth).IsEmpty && !System.Web.UI.WebControls.Unit.Parse(imgheight).IsEmpty) 
     { 
      //create unit object for height and width. This is to convert parameter passed in differen unit like pixel, inch into generic unit. 
      System.Web.UI.WebControls.Unit widthUnit=System.Web.UI.WebControls.Unit.Parse(imgwidth); 
      System.Web.UI.WebControls.Unit heightUnit = System.Web.UI.WebControls.Unit.Parse(imgheight); 
      //AFTER THIS ??? 
     } 

    } 
} 

,当我直接从数据库中显示图像的一些图像得到伸展和不好看,这是因为图像尺寸较大。所以我需要在图库中显示图片。

+1

待办事项你保存比例? –

+0

使用CSS将正确的大小添加到图像不是一个选项? –

+0

仅用于缩略图 –

回答

0

您可以使用此代码给图像输入新的大小,同时保留长宽比:

public static Image ResizeCanvas(Image original, Size newSize, Color background) 
    { 
     int xStart = (newSize.Width/2) - (original.Width/2); 
     int yStart = (newSize.Height/2) - (original.Height/2); 

     // Create blank canvas 
     Bitmap resizedImg = new Bitmap(newSize.Width, newSize.Height); 
     Graphics gfx = Graphics.FromImage(resizedImg); 

     // Fill canvas 
     gfx.FillRectangle(new SolidBrush(background), new Rectangle(new Point(0, 0), newSize)); 

     gfx.DrawImage(original, xStart, yStart, original.Width, original.Height); 

     return resizedImg; 
    } 
1

你可以使用GetThumbnailImage方法

参考代码

public Void GenerateImage(int iWidth,int iHeight,byte[] ImageBytes) 
{ 
System.Drawing.Image image = byteArrayToImage(ImageBytes) 

// create the actual thumbnail image 
     System.Drawing.Image thumbnailImage = image.GetThumbnailImage(iWidth, iHeight, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero); 

     // make a memory stream to work with the image bytes 
     MemoryStream imageStream = new MemoryStream(); 

     // put the image into the memory stream 
     thumbnailImage.Save(imageStream, System.Drawing.Imaging.Imageformat.Jpeg); 

     // make byte array the same size as the image 
     byte[] imageContent = new Byte[imageStream.Length]; 

     // rewind the memory stream 
     imageStream.Position = 0; 

     // load the byte array with the image 
     imageStream.Read(imageContent, 0, (int)imageStream.Length); 

     // return byte array to caller with image type 
     Response.ContentType = "image/jpeg"; 
     Response.BinaryWrite(imageContent); 
    } 

    public bool ThumbnailCallback() 
    { 
     return true; 
    } 
public Image byteArrayToImage(byte[] byteArrayIn) 
{ 
    MemoryStream ms = new MemoryStream(byteArrayIn); 
    Image returnImage = Image.FromStream(ms); 
    return returnImage; 
} 
相关问题