This is a migrated thread and some comments may be shown as answers.

Change Image size different for landscape/portrait

3 Answers 140 Views
BinaryImage
This is a migrated thread and some comments may be shown as answers.
ILHO KIM
Top achievements
Rank 1
ILHO KIM asked on 08 Aug 2010, 05:42 PM
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;
            }
                

    }


3 Answers, 1 is accepted

Sort by
0
Cori
Top achievements
Rank 2
answered on 10 Aug 2010, 05:47 PM

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.
0
ILHO KIM
Top achievements
Rank 1
answered on 11 Aug 2010, 10:09 AM
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>
0
Cori
Top achievements
Rank 2
answered on 11 Aug 2010, 02:03 PM
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.
Tags
BinaryImage
Asked by
ILHO KIM
Top achievements
Rank 1
Answers by
Cori
Top achievements
Rank 2
ILHO KIM
Top achievements
Rank 1
Share this question
or