2017-03-01 78 views
2

我正在WinForm中处理图像,当它具有位图和BitmapData时,它可以很好地工作,我可以很容易地从中获取IntPtr。但在UWP中,我无法从它们获取IntPtr。那么我们有什么办法做到这一点?从BitmapImage或UWP中的流中获取IntPtr值

UPDATE:如果我们无法获得IntPtr值,我们能得到该图像的指针地址吗?像这样在winform:

字节* SRC =(字节*)BitmapData.Scan0.ToPointer();

+1

一个'IntPtr'什么? 'BitmapImage'实例?像素数据?还有别的吗? – IInspectable

+1

我想获得一个IntPtr从指针地址。 (图像在存储器中定位的像素地址)。因此,我可以逐像素地使用它 –

回答

3

你可以通过BitmapDecoder和PixelDataProvider得到文件流pxiel数据:

Windows.Storage.Streams.IRandomAccessStream random = await Windows.Storage.Streams.RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/StoreLogo.png")).OpenReadAsync(); 
     Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(random); 
     Windows.Graphics.Imaging.PixelDataProvider pixelData = await decoder.GetPixelDataAsync(); 
     byte[] buffer = pixelData.DetachPixelData(); 

然后,你可以从字节数组通过不安全的代码

unsafe 
     { 
      fixed (byte* p = buffer) 
      { 
       IntPtr ptr = (IntPtr)p; 
       // do you stuff here 
      } 
     } 

得到的IntPtr如果编译不安全的代码,你需要在项目的构建属性中启用允许不安全代码选项。