2013-04-26 56 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。但那是另一个帖子...

谢谢!