[英]Windows Phone 7.1: Split byte[] image and convert to BitmapImage
編輯:對於需要塗抹圖像的人(即,將WriteableBitmap的一部分復制到另一個),可以使用Buffer.BlockCopy並使用WriteableBitmaps的Pixels int []數組作為參數。
我之前曾問過這個問題: Windows Phone 7.5無法完全加載圖像
我已經在這個問題上工作了幾個小時,並且已經嘗試了幾件事。 我不熟悉圖像類型等,因此很可能缺少基本理論(例如我無法分割byte []圖像並將其轉換為BitmapImage)。
我想做的是:
但是在這些行中,我在System.Windows.dll中得到了帶有未指定錯誤的System.Exception:
firstImg.SetSource(ms);
newImg.SetSource(ms2);
順便說一句,我正在下載的jpeg是有效的jpeg,我可以顯示它而無需嘗試對其進行拆分。
編輯:我下載的Jpeg的寬度小於2000,高度大於2000。
這是我的代碼:
private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
WriteableBitmap rawImg = PictureDecoder.DecodeJpeg(e.Result);
byte[] arr;
int height = rawImg.PixelHeight;
int count = 0;
if (height < 2000)
images.Add(new MyImage(rawImg));
else
{
arr = ConvertToByte(rawImg);
MemoryStream ms = new MemoryStream();
ms.Write(arr, 0, 4 * rawImg.PixelWidth * 2000);
count++;
BitmapImage firstImg = new BitmapImage();
firstImg.SetSource(ms);
images.Add(new MyImage(firstImg));
while (height > 2000)
{
MemoryStream ms2 = new MemoryStream();
ms2.Write(arr, count*2000, 4 * rawImg.PixelWidth * Math.Min(arr.Length - count*2000, 2000));
count++;
height -= 2000;
BitmapImage newImg = new BitmapImage();
newImg.SetSource(ms2);
images.Add(new MyImage(newImg));
}
}
}
private byte[] ConvertToByte(WriteableBitmap wb)
{
int w = wb.PixelWidth;
int h = wb.PixelHeight;
int[] p = wb.Pixels;
int len = p.Length;
byte[] result = new byte[4 * w * h];
for (int i = 0, j = 0; i < len; i++, j += 4)
{
int color = p[i];
result[j + 0] = (byte)(color >> 24);
result[j + 1] = (byte)(color >> 16);
result[j + 2] = (byte)(color >> 8);
result[j + 3] = (byte)(color);
}
return result;
}
寫入MemoryStream
,該位置將前進。 設置信號源之前,請重設位置。
ms.Position = 0;
編輯-您可以使用WriteableBitmapEx 。 這是一個非常快的庫,可以執行與WriteableBitmaps之間的字節轉換。 您還可以使用blitting
功能通過復制較大圖像的各部分來創建新的WriteableBitmap。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.