1

私はサーバー側のイメージコントロールを次のように持っています:

  <img id="img" runat="server" style="padding-left:20px" 
       src="~/Images/2013-02-14_225913.png" />

その高さと幅を計算したい。私はそれを次のように行いました:

int iWidth = (int)Math.Round((decimal)img.Width.Value);
int iHeight = (int)Math.Round((decimal)img.Height.Value);

これは、実際のパラメータではなく、-0を返します。

画像コントロールのH&Wを取得するにはどうすればよいですか?

4

3 に答える 3

1
Bitmap btmp;
string image = "~/Images/2013-02-14_225913.png";
myBitmap = new Bitmap(image);

int height= btmp.Height;
int weight = btmp.Width;
于 2013-02-15T09:35:22.270 に答える
0

あなたの質問はあまり明確ではないので、2 つのシナリオがあります。

  1. .aspxコントロールのプロパティを参照している場合、 /.ascxファイルでコントロールのプロパティを実際に割り当てた場合にのみ、サイズを取得できます。

    2 つの別個のクラスが使用されています。

    <img id="img1" runat="server" 
        style="padding-left: 20px"
        src="meSepiaLarge.jpg" 
        width="100" height="100" />
    <asp:Image ID="img2" runat="server" 
        ImageUrl="~/meSepiaLarge.jpg"  
        Width="100px" Height="100px" />
    

    img1System.Web.UI.HtmlControls.HtmlImageはサーバー側でとしてインスタンス化されますが、img2は になりますSystem.Web.UI.WebControls.Image。寸法を取得する方法は次のとおりです。

    int i1Width = (int)Math.Round((decimal)img1.Width);
    int i1Height = (int)Math.Round((decimal)img1.Height);
    
    int i2Width = (int)Math.Round((decimal)img2.Width.Value);
    int i2Height = (int)Math.Round((decimal)img2.Height.Value);
    
  2. ソース画像のサイズを参照している場合は、kad1r の回答を見ることができます。

于 2013-02-15T09:58:49.500 に答える
0

メモリにロードして、高さと幅を計算する必要があります

        Bitmap myBitmap = new Bitmap(Server.MapPath("~/images/lightbulb.png"));
        if (myBitmap != null)
        {
            Response.Write("Height:"+ myBitmap.Height + " & width:"+ myBitmap.Width);
        }
于 2013-02-15T10:49:30.633 に答える