2011-06-17 24 views
0

我想從Visual C#創建SQL CLR集成函數,現在我的要求是用戶將傳遞文件夾路徑作爲參數,並且函數應該獲取所有從文件夾中獲取圖像文件,並獲取其基本屬性,如FileSize,尺寸等。但似乎SQL項目不支持System.Drawing Namespace ...因爲我在普通項目中創建了相同的函數,所以它工作正常,因爲我能夠使用System.Drawing命名空間,但在這裏我不能使用System.Drawing Namespace ..所以有沒有其他方式來獲取圖像尺寸...如何在不使用位圖或圖形對象的情況下獲取圖像角色.net

下面是我在我的正常項目中使用的代碼。

public DataTable InsertFile(string FolderPath) 
{ 
    DataTable dt = new DataTable(); 
    DataColumn[] col = new DataColumn[] { new DataColumn("FileName", typeof(System.String)), new DataColumn("FileSize", typeof(System.Int32)), new DataColumn("FilePath", typeof(System.String)), new DataColumn("Width", typeof(System.Int32)), new DataColumn("Height", typeof(System.Int32)) }; 
    dt.Columns.AddRange(col); 
    FileInfo info= null; 
    Bitmap bmp = null; 
    foreach (String s in Directory.GetFiles(FolderPath, "*.jpg")) 
    { 
     info = new FileInfo(s); 
     bmp = new Bitmap(s); 
     DataRow dr = dt.NewRow(); 
     dr["FileName"] = Path.GetFileName(s); 
     dr["FileSize"] = info.Length/1024;  
     dr["FilePath"] = s; 
     dr["Width"] = bmp.Width; 
     dr["Height"] = bmp.Height; 
     dt.Rows.Add(dr); 
    } 
    return dt; 
} 

沒有人有任何想法如何獲得圖像維度,而無需使用System.Drawing命名空間。

+0

你在SQL項目中遇到的錯誤/異常是什麼? – ChrFin

+0

實際上在Sql Project中,沒有辦法導入(使用System.Drawing)命名空間..既不允許添加引用System.Drawing – Abbas

回答

1

哇從來沒有見過任何人嘗試這一點,但如果在一個SQL項目中使用不允許繪圖,嘗試讀取頭信息像這樣http://www.codeproject.com/KB/cs/ReadingImageHeaders.aspx

編輯包含的代碼,用變化來消除對尺寸的依賴。

while (binaryReader.ReadByte() == 0xff) 
     { 
      byte marker = binaryReader.ReadByte(); 
      ushort chunkLength = binaryReader.ReadLittleEndianInt16(); 

      if (marker == 0xc0) 
      { 
       binaryReader.ReadByte(); 

       int height = binaryReader.ReadLittleEndianInt16(); 
       int width = binaryReader.ReadLittleEndianInt16(); 
       return new int[] { width, height }; 
      } 

      binaryReader.ReadBytes(chunkLength - 2); 
     } 
+0

多數民衆贊成在偉大的文章,但最終,其也使用位圖對象..正如我所說上面,SQL項目不支持使用System.Drawing命名空間,我可以做的任何其他事情。 – Abbas

+1

@Abbas:只作爲後備 - 如果您檢查DecodeJfif方法,您會看到手動嘗試。 – ChrFin

+0

包括codez。 –

0

Image對象對你來說是否更好?

System.Drawing.Image forSize = System.Drawing.Image.FromFile(s); 
dr["Width"] = forSize.Width; 

等等。

有沒有更好或相同的問題?

相關問題