Click here to Skip to main content
15,905,010 members
Please Sign up or sign in to vote.
1.73/5 (4 votes)
See more:
i tried but not have any idea for this


my image box is 200px * 200px
==> i upload the image and its width is 100px and height is 200px but
when i load that image for review then that image wil be display as stretch in my
200px * 200px content.
pls give me the solution for this

image have to display as normal in my content box (imagebox)
no streching..
.
XML
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 400px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <table class="style1">
            <tr>
                <td width="200">
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </td>
                <td rowspan="2" width="200">
                    <asp:Image ID="Image1" runat="server" Height="200px" Width="200px" />
                </td>
            </tr>
            <tr>
                <td width="200">
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <asp:Button ID="btnupload" runat="server" onclick="btnupload_Click"
                        Text="Upload" />
                </td>
            </tr>
        </table>

    </div>
    </form>
</body>
</html>


********************************

C#
protected void btnupload_Click(object sender, EventArgs e)
    {
        int i = 1;

        if (FileUpload1.HasFile)
        {
            while (File.Exists(Server.MapPath("~/images/") + i.ToString() + ".jpg") == true)
            {
                i++;
            }
            FileUpload1.SaveAs(Server.MapPath("~/images/")+i.ToString()+".jpg");

            Image1.ImageUrl = "~/images/" + i.ToString() + ".jpg";
        }
    }


********************************
Posted
Updated 26-Jan-14 23:23pm
v2
Comments
Kornfeld Eliyahu Peter 27-Jan-14 5:19am    
Remove image box size definitions - auto...

Don't mention Height and Width like this...
ASP.NET
<asp:Image ID="Image1" runat="server" Height="200px" Width="200px" />

Do this dynamically...
C#
protected void btnupload_Click(object sender, EventArgs e)
{
    int i = 1;

    if (FileUpload1.HasFile)
    {
        while (File.Exists(Server.MapPath("~/images/") + i.ToString() + ".jpg") == true)
        {
            i++;
        }

        FileUpload1.SaveAs(Server.MapPath("~/images/")+i.ToString()+".jpg");

        System.IO.Stream s = FileUpload1.PostedFile.InputStream;
        System.Drawing.Image img = System.Drawing.Image.FromStream(s);

        int intFileWidth = 0;
        int intFileHeight = 0;

        int.TryParse(img.PhysicalDimension.Width.ToString(), out intFileWidth);
        int.TryParse(img.PhysicalDimension.Height.ToString(), out intFileHeight);

        Image1.Height = intFileHeight;
        Image1.Width = intFileWidth;

        Image1.ImageUrl = "~/images/" + i.ToString() + ".jpg";
    }
}
 
Share this answer
 
v2
Comments
Manish Dalwadi 27-Jan-14 7:24am    
its showing error :=
==>> cannot use local variable 'i' before it is used
==>> A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'parent or current' scope to denote something else
Oh. It is self explanatory. I am updating the code.
Now, follow the updated code. Just renamed "i" to "img".
Manish Dalwadi 27-Jan-14 10:39am    
thanx
Most welcome. :)
XML
<style type="text/css">
        .style1
        {
            width: 400px;
        }
        .img_style
        {
            height:200px;
            width:200px;
            border: 5px solid gray;

        }
    </style>


when u load image(profilie image and display) just change only in designing code using css

XML
<table class="style1">
            <tr>
                <td width="200">
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </td>
                <td rowspan="2" width="200">
                    <div class="img_style">
<div style="vertical-align:middle;">                        <center>
                            <asp:Image ID="Image1" runat="server" style="max-height:200px; max-width:200px; vertical-align:middle;" />
                        </center>
                    </div></div>
                </td>
            </tr>
            <tr>
                <td width="200">
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <asp:Button ID="btnupload" runat="server" OnClick="btnupload_Click" Text="Upload" />
                </td>
            </tr>
        </table>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900