2013-04-26 106 views
1

我導入了灰度16位圖像。我把它作爲一個BitmapSource和一個Image(控件命名空間)。我怎樣才能訪問個人像素? CopyPixels是我讀過關於唯一還是最好的方法?如果是這樣,我不知道如何設置步幅,以及哪個像素包含像素強度值。16位TIFF中的訪問像素

方法一:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Threading; 
using System.IO; 
using Nikon; 
using WindowsFormsApplication1; 
using System.Windows.Media.Imaging; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Windows.Controls; 

namespace Map 
{ 
    class OpenTIFF 
    { 
     static void OpenOne() 
     { 
     // Open a Stream and decode a TIFF image 
     Stream imageStreamSource = new FileStream("C:\\Users\\Me\\Desktop\\"MyTif.tif", FileMode.Open, FileAccess.Read, FileShare.Read); 
     TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); 
     BitmapSource bitmapSource = decoder.Frames[0]; 
     System.Windows.Controls.Image image = new System.Windows.Controls.Image(); 
     image.Source = bitmapSource; 
     } 
    } 
} 

我認爲這可能是簡單的(發現here),但後來我很清楚如何訪問單個像素在一維字節數組。

方法2:

static void OpenTwo() 
     { 
      System.Drawing.Image imageToConvert = System.Drawing.Image.FromFile("aa.tif", true); 
      byte[] Ret = new byte[0]; 
      using (MemoryStream ms = new MemoryStream()) 
      { 
       imageToConvert.Save(ms, ImageFormat.Tiff); 
       Ret = ms.ToArray(); 
      } 
     } 

感謝, 丹

回答

2

你可能要檢查出免費的圖片庫。 它有一個C#包裝。 http://freeimage.sourceforge.net/index.html

爲了讓您一開始,給一個簡單的例子:

  1. 下載二進制分發
  2. 在Visual Studio開放FreeImage.NET.sln(我用2010)
  3. 建庫項目
  4. 如果您收到XML註釋中的錯誤,請執行以下操作:在項目屬性中,在build下,將「將警告視爲錯誤」從「all」更改爲「none」
  5. 創建新的控制檯項目。
  6. 添加引用您在步驟建組裝3
    FreeImage3154Win32 \的FreeImage \包裝\ FreeImage.NET \ CS \圖書館\ BIN \調試\ FreeImageNET.dll
  7. 下面的代碼添加到Program.cs中

    using FreeImageAPI; 
    
    namespace ConsoleApplication1 
    { 
        class Program 
        { 
         static void Main(string[] args) 
         { 
          // load image, this can by your 16-bit tif 
          FIBITMAP dib = FreeImage.LoadEx("myfile.tif"); 
          // convert to 8-bits 
          dib = FreeImage.ConvertToGreyscale(dib); 
          // rotate image by 90 degrees 
          dib = FreeImage.Rotate(dib, 90); 
          // save image 
          FreeImage.SaveEx(dib, "newfile.png"); 
          // unload bitmap 
          FreeImage.UnloadEx(ref dib); 
         } 
        } 
    

    }

  8. 副本,你下載到bin目錄中的二進制FreeImage的DLL。
    FreeImage3154Win32 \的FreeImage \ DIST \ FreeImage.dll
  9. 運行應用程序

還有就是在包含該庫項目的解決方案的例子一個很好的選擇。

+0

我正在尋找它......看起來很有希望!我會發布如何去。謝謝,丹佛。 – DanG 2013-04-26 18:35:54

+0

我現在一整天都在ban and,無法在Visual Studio中工作。任何幫助?我需要特定的步驟來將FreeImage.NET解決方案和C++ DLL加載到我現有的項目中。我是一名初學者,並且會喜歡跳躍的開始...... – DanG 2013-04-27 01:52:30

+0

@DanG我用一個示例更新了我的帖子,以幫助您開始。 – denver 2013-04-27 03:11:18

2

您的詳細步驟非常多,我需要...非常感謝。

我遵循了與您的安裝相似的方向(您的步驟1-3)在相關的thread中。這是成功的,正如羅穆盧斯所說。

下面是一些額外的細節需要和我碰到的錯誤(我希望它沒有太多的細節......希望能夠幫助任何遇到此問題的人)。

我跟着你從第4步起(除了我的exisitng窗口應用程序中添加它)的方向。我將FreeImage.dll(步驟8)複製到我的項目的bin目錄中。

我運行代碼,並得到這個錯誤:

An unhandled exception of type 'System.BadImageFormatException' occurred in WindowsFormsApplication1.exe

Additional information: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

我有兩個錯誤。首先,我沒有將FreeImage二進制dll(上面的第8步)複製到項目的正確bin目錄中(我將它複製到了bin而不是bin \ Debug 8-)。我把它複製到:

\WindowsFormsApplication1\bin\Debug

其次,如果我現在運行它,我得到一個錯誤:

An unhandled exception of type 'System.TypeInitializationException' occurred in WindowsFormsApplication1.exe

Additional information: The type initializer for 'WindowsFormsApplication1.Form1' threw an exception.

這裏的解決方案是構建平臺更改爲86。我發現要麼選擇x86,要麼選擇帶有首選32位框的Any CPU檢查工作。針對像我這樣的初學者的提示:通過右鍵單擊項目名稱(不是解決方案)並選擇屬性來訪問構建平臺。它也可以從DEBUG菜單訪問,其底部將是「MyProject屬性」。 Build x86 without 32-bit preferred checked 或者這也適用: Build with Any CPU selected AND Prefer 32-bit checked

這兩個補丁

所以,代碼運行正常。但是,文件輸出是0KB。但那是另一個帖子...

謝謝!