title: Bitmap manipulation in Windows Phone: How to deal with GetPixel, SetPixel, PixelFormat, etc?
body: <p>im trying to convert one of fully functional desktop apps to a Windows phone app, i was managed to convert most of the things..but one function is not working due to some syntax error.anyone know replacements for </p>
  
  <p><strong>GetPixel,
  FromArgb,
  SetPixel,
  MemoryStream,
  Marshal,
  ImageLockMode,
  PixelFormat</strong></p>
  
  <p>the code is this </p>
  
  <pre><code>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 &lt; hight; i++)
          {
              for (int j = 0; j &lt; 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 &lt; bmpBytes.Length; i++)
          {
  
  
              if ((0xFF &amp; bmpBytes[i]) &gt;= 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 &lt; length; n++)
          {
              if ((0xFF &amp; stream[n]) &gt;= 57)
              {
                  Marshal.WriteByte(bmpData.Scan0, n, ((byte)255));
  
              }
              else
              {
                  Marshal.WriteByte(bmpData.Scan0, n, ((byte)0));
              }
  
          }
  
          bmp.UnlockBits(bmpData);
          image1.Source = bmp;
  
  
      }
  </code></pre>