c#
2013-07-01 3 views 0 likes 
0

데이터베이스의 모든 이미지를 Listview에 표시해야하는 작은 프로젝트를 수행하고 있습니다. querystring 매개 변수로 이미지 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

: 당신은 GetThumbnailImage이 방법 사용할 수

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


코드

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; 
} 
관련 문제