Note that there are some explanatory texts on larger screens.

plurals
  1. POWindows Phone 7.1: Split byte[] image and convert to BitmapImage
    primarykey
    data
    text
    <p>Edit: To those who need to blit an image(i.e. copy a part of a WriteableBitmap to another), you can use Buffer.BlockCopy and use WriteableBitmaps' Pixels int[] arrays as argument.</p> <p>I asked this question before: <a href="https://stackoverflow.com/questions/8754527/image-wont-load-completely-on-windows-phone-7-5">Image won&#39;t load completely on Windows Phone 7.5</a></p> <p>I've been working on this issue for several hours now and I've tried several things. I'm not familiar with image types etc. so it's possible that I'm missing a basic theory (like I cannot split a byte[] image and convert them into BitmapImage).</p> <p>What I'm trying to do is:</p> <ol> <li>Download an image(JPEG) from Web using WebClient.</li> <li>Decode the JPEG from the stream that WebClient returned to me using PictureDecoder.DecodeJpeg and get a WriteableBitmap object</li> <li>Convert WriteableBitmap.Pixels array from int[] to byte[]</li> <li>Split the byte[] array into several pieces, so they won't exceed 2000x2000 size limit</li> <li>Convert each piece to BitmapImage so I can use them in my WP7.1 Silverlight app.</li> </ol> <p>But I get an System.Exception with Unspecified error in System.Windows.dll in those lines:</p> <pre><code> firstImg.SetSource(ms); newImg.SetSource(ms2); </code></pre> <p>BTW, the jpeg I'm downloading is a valid jpeg, I can display it without trying to split it.</p> <p>Edit: Jpeg that I download is smaller than 2000 in width and bigger than 2000 in height.</p> <p>Here's my code:</p> <pre><code>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 &lt; 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 &gt; 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 &lt; len; i++, j += 4) { int color = p[i]; result[j + 0] = (byte)(color &gt;&gt; 24); result[j + 1] = (byte)(color &gt;&gt; 16); result[j + 2] = (byte)(color &gt;&gt; 8); result[j + 3] = (byte)(color); } return result; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload