2010-01-06 44 views
11

這個問題的各種口味已被問到,但我還沒有找到正確的答案。ASP.NET:獲取圖像的高度和寬度

假設我在文件服務器上有一個.jpg圖像,並且需要獲取它的高度和寬度。我如何在asp.net中做到這一點?

我見過這表明做一些像這樣的幾個答案:

System.Drawing.Image image=System.Drawing.Image.FromFile(PicturePath); 
int ActualWidth=image.Width; 
int ActualHeight=image.Height; 
image.Dispose(); 

這會工作得很好,除了classes within the System.Drawing namespace are not supported for use within an ASP.NET service

那麼,你如何得到在ASP.net中圖像的實際高度和寬度?

+1

相關,但一個不同的問題:http://stackoverflow.com/questions/390532/system-drawing-in-windows-or-asp-net-services – jball 2010-01-06 21:12:30

+1

Image.FromStream(stream,false)將加載寬度和高度,而不分析所有圖像數據。確保你放棄圖片後面的流,你會沒事的。這個警告是因爲一般的程序員懶得無法正確處理手動內存管理。 – 2011-07-30 07:05:45

回答

0

這就是說服務,而不是應用程序。這將工作得很好。

+0

ASP工作進程不作爲服務運行? – jball 2010-01-06 21:03:41

+0

http://dotnet.org.za/eduard/archive/2004/09/23/4226.aspx – jball 2010-01-06 21:09:12

7

在ASPX

<asp:image ID="img1" runat="server" src="" /> 

和代碼添加一個服務器側圖像控制背後給它一個src

img1.src = System.Drawing.Image.FromFile(PicturePath); 

int ActualWidth = img1.Width; 
int ActualHeight = img1.Height; 
img1.src = ""; 
+0

根據問題中提到的MSDN頁面,不應在ASP.net中使用System.Drawing。 – Anthony 2010-01-06 21:06:09

+2

img1.src無法正常工作。你可能意思是img1.ImageUrl – Anthony 2010-01-06 21:19:29

+0

是的確切, – Hiyasat 2010-01-07 11:15:31

1

爲了避免使用System.Drawing命名空間:

對於GIF,高度和寬度是文件頭中的4個字節的整數。 (地址0×12的寬度,0x16爲高)

爲JPG格式,你可以嘗試寫一個函數喜歡在這裏找到了一個:http://www.64lines.com/jpeg-width-height 它通過在JPG作爲數據的數組,它得到的高度和寬度。

+0

謝謝,但你的鏈接不起作用。 – Anthony 2010-01-09 12:21:06

+0

抱歉Anothony,修正了它。 – 2010-01-11 01:59:56

1

願這幫助

string lPath = Server.MapPath("~\\Images1\\") + dsProd.Tables[0].Rows[i]["Image1"].ToString(); 

Image1.ImageUrl = "Images1\\" + dsProd.Tables[0].Rows[i]["Image1"].ToString(); 
Image2.ImageUrl = "Images1\\" + dsProd.Tables[0].Rows[i]["Image2"].ToString(); 


string currentImagePath = lPath.ToString();// Session["FullImagePath"] + "\\" + GetCurrentFileName(); 
Bitmap bmp = new Bitmap(currentImagePath); 



int iActualWidth=0,iActualHeight=0; 
for (int j = 1; j <= 100; j++) 
{ 
    if ((bmp.Width/j) > 150) 
    { 
      iActualWidth = bmp.Width/j; 
      iActualHeight = bmp.Height/j; 
    } 
    else 
    { 
     break; 
    } 
} 

Image1.Height = new Unit(iActualHeight); 
Image1.Width = new Unit(iActualWidth); 
-1

進口System.Drawing.Image對象,System.IO

Dim image As System.Drawing.Image 

image = image.FromFile([filepath]) 

If image.Width > 440 Or image.Height > 440 Then 
'show resized 
else 
'leave as is 
end if 
+1

問題中的代碼有什麼區別? – Anthony 2011-10-06 18:49:42

0

我已經轉換C++代碼,C#以供將來參考:

static bool get_jpeg_size(byte[] data, int data_size, ref int width, ref int height) 
{ 
    //Check for valid JPEG image 
    int i = 0; // Keeps track of the position within the file 
    if (data[i] == 0xFF && data[i + 1] == 0xD8 && data[i + 2] == 0xFF && data[i + 3] == 0xE0) 
    { 
     i += 4; 
     // Check for valid JPEG header (null terminated JFIF) 
     if (data[i + 2] == 'J' && data[i + 3] == 'F' && data[i + 4] == 'I' && data[i + 5] == 'F' && data[i + 6] == 0x00) 
     { 
      //Retrieve the block length of the first block since the first block will not contain the size of file 
      var block_length = data[i] * 256 + data[i + 1]; 
      while (i < data_size) 
      { 
       i += block_length;    //Increase the file index to get to the next block 
       if (i >= data_size) return false; //Check to protect against segmentation faults 
       if (data[i] != 0xFF) return false; //Check that we are truly at the start of another block 
       if (data[i + 1] == 0xC0) 
       {   //0xFFC0 is the "Start of frame" marker which contains the file size 
        //The structure of the 0xFFC0 block is quite simple [0xFFC0][ushort length][uchar precision][ushort x][ushort y] 
        height = data[i + 5] * 256 + data[i + 6]; 
        width = data[i + 7] * 256 + data[i + 8]; 
        return true; 
       } 
       else 
       { 
        i += 2;        //Skip the block marker 
        block_length = data[i] * 256 + data[i + 1]; //Go to the next block 
       } 
      } 
      return false;      //If this point is reached then no size was found 
     } 
     else { return false; }     //Not a valid JFIF string 

    } 
    else { return false; }      //Not a valid SOI header 
} 

用法:

using (var stream = File.OpenRead(path)) 
{ 
    using (var m = new MemoryStream()) 
    { 
     stream.CopyTo(m); 
     var arr = m.ToArray(); 
     int w = 0, h = 0; 

     get_jpeg_size(arr, arr.Length, ref w, ref h); 
     Console.WriteLine(w + "x" + h); 
    } 
} 
-1
Imports System.IO 

Imports System.Drawing         

Dim sFile As Stream = fuPhoto2.PostedFile.InputStream 

Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(sFile) 

If img.PhysicalDimension.Width > 700 And img.PhysicalDimension.Height > 300 Then 

    strPhotoName = fuPhoto2.FileName 

    fuPhoto2.SaveAs(Server.MapPath("~/Images/") + 
fuPhoto2.FileName)         

Else 

    lblErrMeg2.Text = "Image size must be greater than 700 X 300!" 

    fuPhoto2.Focus() 

    Exit Sub 

End If 
+1

根據問題中提到的MSDN頁面,不應在ASP.net中使用System.Drawing。你的「img」變量是基於它的。此外,img.PhysicalDimension.Width和img.Width(在問題中使用)之間的區別是什麼? – Anthony 2012-09-06 14:33:06

1

您可以使用「位圖」類。

C#

Bitmap bitmap = new Bitmap(filepath); 

int iWidth = bitmap.Width; 
int iHeight = bitmap.Height; 

VB

Dim bitmap As New Bitmap(filepath) 

Dim iWidth As Integer = bitmap.Width 
Dim iHeight As Integer = bitmap.Height 
0

進口iTextSharp.text

剛剛創建PFD時使用的工具。

  Dim URel As String 
      URel = "https://......." 

      Dim pic As iTextSharp.text.Image 
      pic = iTextSharp.text.Image.GetInstance(URel) 
      Dim sizee As String 
      sizee = pic.Height 
      SOURR = "<img src='" & URel & "' alt='' />" 
0

我有我的圖片在ListView作爲ImageButton的,我需要他們的寬度和高度,所以我在這裏找到了解決方案: http://forums.asp.net/t/1262878.aspx?how+to+get+the+image+width+and+height+argh

和我的工作代碼:

ListViewItem item = e.Item; 
    ImageButton img = item.FindControl("img") as ImageButton; 

    FileStream fs = new FileStream(MapPath(img.ImageUrl) , FileMode.Open, FileAccess.Read, FileShare.Read); 
    System.Drawing.Image dimg = System.Drawing.Image.FromStream(fs); 
    int width = Convert.ToInt32(dimg.Width); 
    int height = Convert.ToInt32(dimg.Height); 

我希望它可以幫助你