2017-04-25 82 views
0

使用下面的函數,我遇到了試圖訪問WriteableBitmap.PixelBuffer屬性的問題。我得到的消息是:'using'聲明被忽略

'WriteableBitmap' does not contain a definition for 'PixelBuffer' and no extension method 'PixelBuffer' accepting a first argument of type 'WriteableBitmap' could be found (are you missing a using directive or an assembly reference?) 

我看過其他地方,我需要包括

using System.Runtime.InteropServices.WindowsRuntime; 

但是當我使用這包括,沒有在我的代碼更改。通過我的解決方案的參考看,我沒有看到任何像System.Runtime.InteropServices。我很沮喪,因爲這似乎是其他人嘗試訪問WriteableBitmap的PixelBuffer的解決方案。

private WriteableBitmap ChangeBrightness(WriteableBitmap source, byte change_value) 
    { 
     WriteableBitmap dest = new WriteableBitmap(source); 

     byte[] color = new byte[4]; 

     using (Stream s = source.PixelBuffer.AsStream()) 
     { 
      using (Stream d = dest.PixelBuffer.AsStream()) 
      { 
       // read the pixel color 
       while (s.Read(color, 0, 4) > 0) 
       { 
        // color[0] = b 
        // color[1] = g 
        // color[2] = r 
        // color[3] = a 

        // do the adding algo per byte (skip the alpha) 
        for (int i = 0; i < 4; i++) 
        { 
         if ((int)color[i] + change_value > 255) color[i] = 255; else color[i] = (byte)(color[i] + change_value); 
        } 

        // write the new pixel color 
        d.Write(color, 0, 4); 
       } 
      } 
     } 

     // return the new bitmap 
     return dest; 
    } 
+0

你的代碼的哪部分不工作? 'WriteableBitmap'不被識別或者是什麼? – MrZander

+0

'PixelBuffer'似乎是['Windows.UI.Xaml.Media.Imaging.WriteableBitmap']的成員(https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml。 media.imaging.writeablebitmap#Windows_UI_Xaml_Media_Imaging_WriteableBitmap_PixelBuffer),你用錯了嗎? – DavidG

+2

http://stackoverflow.com/questions/20478467/stream-stream-writeablebitmap-pixelbuffer-asstream-missing這似乎是相關的,特別是關於程序集/命名空間的問題的評論 – MrZander

回答

1

確保您引用的組件包屬於:

System.Runtime.WindowsRuntime裝配

+0

當我去時我找不到那個程序集添加參考。我最終以另一種方式解決了我的問題。謝謝 – Ken

-1

您已經聲明此方法爲私有。您確定在嘗試訪問該方法時沒有訪問衝突。右鍵單擊包含此類的項目,並在解決方案資源管理器中檢查項目引用,並確保爲System.Runtime.InteropServices.WindowsRuntime加載了正確的引用和程序集;和其他必要的組件。

0

我已經結束了通過其他方式解決我的原始問題。爲了調整我的圖像的亮度,我最終使用了這個功能:

public ImageSource AdjustBrightness(BitmapImage Image, int Value, int mod) 
    { 
     Bitmap TempBitmap = BitmapImage2Bitmap(Image); 

     Bitmap NewBitmap = new Bitmap(TempBitmap.Width, TempBitmap.Height); 

     Graphics NewGraphics = Graphics.FromImage(NewBitmap); 

     float FinalValue = (float)Value/255.0f; 

     float[][] FloatColorMatrix ={ 

       new float[] {1, 0, 0, 0, 0}, 

       new float[] {0, 1, 0, 0, 0}, 

       new float[] {0, 0, 1, 0, 0}, 

       new float[] {0, 0, 0, 1, 0}, 

       new float[] {mod * FinalValue, mod * FinalValue, mod * FinalValue, 1, 1} 
      }; 

     ColorMatrix NewColorMatrix = new ColorMatrix(FloatColorMatrix); 

     ImageAttributes Attributes = new ImageAttributes(); 

     Attributes.SetColorMatrix(NewColorMatrix); 

     NewGraphics.DrawImage(TempBitmap, new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), 0, 0, TempBitmap.Width, TempBitmap.Height, GraphicsUnit.Pixel, Attributes); 

     Attributes.Dispose(); 

     NewGraphics.Dispose(); 

     return Bitmap2BitmapImage(NewBitmap); 
    }