2017-03-01 82 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如果編譯不安全的代碼,你需要在項目的構建屬性中啓用允許不安全代碼選項。