在Windows Phone 7中将文件转换为字节

时间:2013-09-30 08:40:57

标签: windows-phone-7

我是Windows手机开发者。在我的应用程序中,我有一个文件“A.img”,已经被XOR算法加密,如何将该文件(A.img)转换为byte []数组进行解密。我试过但没有成功。

2 个答案:

答案 0 :(得分:1)

public static byte[] ConvertToBytes(this BitmapImage bitmapImage)
{
    using (MemoryStream ms = new MemoryStream())
    {
        WriteableBitmap btmMap = new WriteableBitmap
            (bitmapImage.PixelWidth, bitmapImage.PixelHeight);

        // write an image into the stream
        Extensions.SaveJpeg(btmMap, ms,
            bitmapImage.PixelWidth, bitmapImage.PixelHeight, 0, 100);

        return ms.ToArray();
    }
}

答案 1 :(得分:0)

试试这个

 public static byte[] ToByteArray(this WriteableBitmap bmp) 
 { 
    // Init buffer 
    int w = bmp.PixelWidth; 
    int h = bmp.PixelHeight;
    int[] p = bmp.Pixels; 
    int len = p.Length; 
    byte[] result = new byte[4 * w * h];     

   // Copy pixels to buffer 

   for (int i = 0, j = 0; i < len; i++, j += 4) 
   { 
       int color = p[i]; 
       result[j + 0] = (byte)(color >> 24); // A 
       result[j + 1] = (byte)(color >> 16); // R 
       result[j + 2] = (byte)(color >> 8);  // G 
       result[j + 3] = (byte)(color);       // B 
   } 
   return result; 
 }