Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have to probably wait one UI loop cycle to make sure Silverlight has a chance to update layouts. Also you have to be sure that image is actually visible. I actually wrote a small routine which get the image size from image binary. Downside is you'll have to download image explicitly via WebClient but the good thing is you'll know dimensions without actually displaying anything:</p> <pre><code>namespace StatMap.Silverlight.Services.UIHelpers { public static class ImageSizeExtractor { public static bool TryGetImageSize(byte[] buf, out int width, out int height) { return TryGetPngSize(buf, out width, out height) || TryGetJpegSize(buf, out width, out height); } private static bool IsPng(byte[] buf) { return (buf.Length &gt; 8)&amp;&amp;(buf[0] == 137) &amp;&amp; (buf[1] == 80) &amp;&amp; (buf[2] == 78) &amp;&amp; (buf[3] == 71) &amp;&amp; (buf[4]==13)&amp;&amp;(buf[5]== 10)&amp;&amp;(buf[6]== 26)&amp;&amp;(buf[7]== 10); } private static bool TryGetPngSize(byte[] buf, out int width, out int height) { width = 0; height = 0; if (IsPng(buf)) { int index = -1; for (int i = 8; i &lt; buf.Length - 12;i++) { if ((buf[i]==0x49)&amp;&amp;(buf[i+1]==0x48)&amp;&amp;(buf[i+2]==0x44)&amp;&amp;(buf[i+3]==0x52)) { index = i + 4; break; } } if (index&lt;0) { return false; } width = buf[index + 3] + buf[index + 2]*256; height = buf[index + 7] + buf[index + 6]*256; return true; } return false; } private static bool TryGetJpegSize(byte[] buf, out int width, out int height) { //static char get_jpeg_size(unsigned char* data, unsigned int data_size, unsigned short *width, unsigned short *height) { //Check for valid JPEG image int i = 0; // Keeps track of the position within the file width = 0; height = 0; if (buf[i] == 0xFF &amp;&amp; buf[i + 1] == 0xD8 &amp;&amp; buf[i + 2] == 0xFF &amp;&amp; buf[i + 3] == 0xE0) { i += 4; // Check for valid JPEG header (null terminated JFIF) if (buf[i + 2] == 'J' &amp;&amp; buf[i + 3] == 'F' &amp;&amp; buf[i + 4] == 'I' &amp;&amp; buf[i + 5] == 'F' &amp;&amp; buf[i + 6] == 0x00) { //Retrieve the block length of the first block since the first block will not contain the size of file int blockLength = buf[i]*256 + buf[i + 1]; while (i &lt; buf.Length) { i += blockLength; //Increase the file index to get to the next block if (i &gt;= buf.Length) return false; //Check to protect against segmentation faults if (buf[i] != 0xFF) return false; //Check that we are truly at the start of another block if (buf[i + 1] == 0xC0) { //0xFFC0 is the "Start of frame" marker which contains the file size //The structure of the 0xFFC0 block is quite simple [0xFFC0][ushort length][uchar precision][ushort x][ushort y] width = buf[i + 5]*256 + buf[i + 6]; height = buf[i + 7]*256 + buf[i + 8]; return true; } i += 2; //Skip the block marker blockLength = buf[i]*256 + buf[i + 1]; //Go to the next block } return false; //If this point is reached then no size was found } return false; } return false; } } } </code></pre> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      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