Change Image size different for landscape/portrait

4 posts, 0 answers
  1. ILHO KIM
    ILHO KIM avatar
    4 posts
    Member since:
    Jul 2012

    Posted 08 Aug 2010 Link to this post

    I want display binaryimage in radlistview with xml datasouce.
    Each listview item height is 300px.
    If Image data's height is greater than width (portrait ) then I want image height set to 300px.
    If Image data's width is greater than height (landscape) then I want image width set to 300px.
    so I want every image set to 300 x 300 square box.

    I set to binaryimage set to width = 300px, height = 300px then all Image set by 300 by 300 size with distortion ratio!
    I want portrait, landscape image to display square box without distort it's raito.

    So I coded below but It does'nt work.

        protected void RadListView1_ItemDataBound(object sender, RadListViewItemEventArgs e)
        {
     

                RadListViewDataItem li = (RadListViewDataItem)e.Item;
                RadBinaryImage PhotoImage = (RadBinaryImage)e.Item.FindControl("RadBinaryImage1");
                if (PhotoImage.Width.Value > PhotoImage.Height.Value)
                {
                    PhotoImage.Width = 300;
                }
                else
                {
                    PhotoImage.Height = 300;
                }
                    

        }


  2. Cori
    Cori avatar
    562 posts
    Member since:
    Jul 2010

    Posted 10 Aug 2010 Link to this post

    Hello ILHO,

    The reason it doesn't work is because the RadBinaryImage doesn't know the actual size of image it's binding to, so the values you're retrieving for with and height are the values you had set of 300px by 300px. You need to actually load the image into an Image object and then check the width/height. Like so:

    RadListViewDataItem li = (RadListViewDataItem)e.Item;
            RadBinaryImage PhotoImage = (RadBinaryImage)e.Item.FindControl("RadBinaryImage1");
      
            Byte[] imgBytes = imageBytes;
      
            MemoryStream ms = new MemoryStream();
            ms.Write(imgBytes, 0, imgBytes.Length);
            System.Drawing.Image img = System.Drawing.Image.FromStream(ms);        
              
            if (img.Width > img.Height)
            {
                PhotoImage.Width = 300;
            }
            else
            {
                PhotoImage.Height = 300;
            }

    The imageBytes variable should be retrieved from the RadListViewDataItem.DataItem and then accessing the image column/field.

    I hope that helps.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. ILHO KIM
    ILHO KIM avatar
    4 posts
    Member since:
    Jul 2012

    Posted 11 Aug 2010 Link to this post

    Thanks for your help.
    I can't understand your help.
    The imageBytes variable should be retrieved from the RadListViewDataItem.DataItem and then accessing the image column/field.

    It's my aspx code.

             <ItemTemplate>
                
                 <tr class="rlvI">
        
                    <td style="display: none; visibility: hidden;"><%#XPath("id")%></td>
                    <td>
                    <telerik:RadBinaryImage Style="cursor: pointer;" runat="server" ID="RadBinaryImage1"
                               
                            ImageUrl='<%#"http://xxxx.org/img/w800/" + XPath("image/id") +".jpg" %>' 
                                AlternateText="Click to view larger image" AutoAdjustImageControlSize= "False"
                             ResizeMode="Fit"  EnableViewState="False" EnableTheming="False" ImageAlign="Left" ClientIDMode="Static" />
                    </td>
                    <td><span style="font-weight:bold;font-size:1.5em"><a href="sadfsadf"><%#XPath("name")%></a></span></td>
                   
                    <td><%#XPath("status")%></td>
     <td><%#XPath("loan_amount")%></td>
      <td><%#XPath("funded_amount")%></td>
                 </tr>
             </ItemTemplate>
  5. Cori
    Cori avatar
    562 posts
    Member since:
    Jul 2010

    Posted 11 Aug 2010 Link to this post

    I thought you were using the RadBinaryImage control to load an image from the database, thus the reason I used the imageBytes technique. If you want to know the width/height of a file on the file system or a url you could use the Image.FromFile method if the image is on the same server as your site or download the image and load the bytes into the MemoryStream object that I posted earlier.

    I hope that helps.
Back to Top