2012-06-24 42 views
2

我嘗試全功能的桌面應用程序的一個轉換到Windows手機應用程序,我是成功地轉化大部分things..but一個功能不工作,由於一些語法error.anyone知道Windows Phone中的位圖操作:如何處理GetPixel,SetPixel,PixelFormat等?

替代GetPixel, FromArgb, SetPixel, MemoryStream的, 元帥, ImageLockMode, 的PixelFormat

的代碼是這個

private void tresh() 
    { 
     int hight = image1.Source.Height; 
     int width = image1.Source.Width; 
     BitmapImage img = new BitmapImage(image1.Source); 
     BitmapImage newImg = new BitmapImage(width, hight); 
     int threshold = 0; 

     for (int i = 0; i < hight; i++) 
     { 
      for (int j = 0; j < width; j++) 
      { 

       int grayScale = (int)((img.GetPixel(j, i).R * 0.3) + (img.GetPixel(j, i).G * 0.59) + (img.GetPixel(j, i).B * 0.11)); 
       Color nc = Color.FromArgb(grayScale, grayScale, grayScale); 
       newImg.SetPixel(j, i, nc); 

      } 
     } 

     image1.Source = newImg; 

     MemoryStream ms = new MemoryStream(); 
     newImg.Save(ms, ImageFormat.Bmp); 

     byte[] bmpBytes = ms.GetBuffer(); 

     threshold = getOtsuNumber(bmpBytes); 





     byte[] newBytArr = new byte[bmpBytes.Length]; 




     for (int i = 0; i < bmpBytes.Length; i++) 
     { 


      if ((0xFF & bmpBytes[i]) >= threshold) 
      { 
       newBytArr[i] = ((byte)255); 

      } 
      else 
      { 
       newBytArr[i] = ((byte)0); 
      } 
     } 


     BitmapImage oldBmp = newImg; 
     width = oldBmp.Width; 
     int height = oldBmp.Height; 
     BitmapData oldData = oldBmp.LockBits(
       new Rectangle(0, 0, oldBmp.Width, oldBmp.Height), 
         ImageLockMode.WriteOnly, 
         oldBmp.PixelFormat); 
     int length = oldData.Stride * oldBmp.Height; 
     byte[] stream = new byte[length]; 
     Marshal.Copy(oldData.Scan0, stream, 0, length); 
     oldBmp.UnlockBits(oldData); 


     BitmapImage bmp = new Bitmap(width, height, oldBmp.PixelFormat); 
     BitmapData bmpData = bmp.LockBits(
       new Rectangle(0, 0, width, height), 
       ImageLockMode.WriteOnly, 
       bmp.PixelFormat); 


     for (int n = 0; n < length; n++) 
     { 
      if ((0xFF & stream[n]) >= 57) 
      { 
       Marshal.WriteByte(bmpData.Scan0, n, ((byte)255)); 

      } 
      else 
      { 
       Marshal.WriteByte(bmpData.Scan0, n, ((byte)0)); 
      } 

     } 

     bmp.UnlockBits(bmpData); 
     image1.Source = bmp; 


    } 

回答

3

你必須改變類型intbyte

相關問題