问题
I have imported a greyscale 16-bit image. I have it bot as a BitmapSource and an Image (of Controls namespace). How can i access the individual pixels? Is the CopyPixels i have read about the only or best way? If so, i don't get how to set the stride, and which pixel contains the pixel intensity value.
Method1:
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;
}
}
}
I think this might be simpler (found here), but then i am unclear how to access individual pixels in a 1D byte array.
Method 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();
}
}
Thanks, Dan
回答1:
You might want to check out the free image library. It has a C# wrapper. http://freeimage.sourceforge.net/index.html
To get you started and give a quick example:
- download the binary distribution
- open FreeImage.NET.sln in visual studio (I used 2010)
- build the Library project
- if you receive errors in the XML comments do the following: go in the project properties, under build, change "treat warnings as errors" from "all" to "none"
- Create new console project.
- Add reference to the assembly you built in step 3.
FreeImage3154Win32\FreeImage\Wrapper\FreeImage.NET\cs\Library\bin\Debug\FreeImageNET.dll Add the following code to 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); } }
}
- copy the FreeImage binary dll that you downloaded into the bin directory.
FreeImage3154Win32\FreeImage\Dist\FreeImage.dll - Run the application
There is a good selection of examples in the solution that contained the Library project.
回答2:
Your detailed steps are very much what i needed...much thanks.
I followed directions similar to yours for installation (your steps 1-3) in a related thread. This was successful, and exactly as Romulus said.
Below are some additional details needed and errors I ran into (i hope its not too much detail...hope to help anyone else who runs into this).
I followed your directions from step 4 onwards (except i added it into my exisitng windows application). I copied the FreeImage.dll (step 8) into the bin directory of my project.
I run the code, and get this error:
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)
I had two errors. First was that I had not copied to FreeImage binary dll (step 8 above) into the correct bin directory of the project (I had copied it to bin and not bin\Debug 8-). I copied it to:
\WindowsFormsApplication1\bin\Debug
Second, if I run it now, I get an error:
An unhandled exception of type 'System.TypeInitializationException' occurred in WindowsFormsApplication1.exe
Additional information: The type initializer for 'WindowsFormsApplication1.Form1' threw an exception.
The solution here is to change the Build platform to x86. I found either selecting x86, or Any CPU with the Prefer 32-bit box checked work. Tip for beginners like myself: The Build platform is accessed by right-clicking on the Project name (not the Solution) and selecting Properties. It can also be accessed from the DEBUG menu, where at the bottom will be "MyProject Properties".
or this also works:So with these two fixes, the code runs fine. However, the file output is 0KB. But that's another post...
Thanks!
Dan
来源:https://stackoverflow.com/questions/16226743/access-pixels-in-a-16-bit-tiff