Note that there are some explanatory texts on larger screens.

plurals
  1. POImage stride for MSpaint image in C# is +1 byte, why?
    primarykey
    data
    text
    <p>I have a problem regarding some pixel-based operations in C#. I wrote a class that serves as an image shell around a <a href="http://msdn.microsoft.com/en-us/library/4e7y164x.aspx" rel="nofollow">Bitmap</a>. It can give you the RGB values of a pixel at a certain (x,y) location in the image much faster than the <code>Bitmap.GetRGB(x,y)</code> color object by using <a href="http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.aspx" rel="nofollow">BitmapData</a> and <a href="http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits.aspx" rel="nofollow">LockBits</a> to get direct access to the image array and read the bytes from there. I added this function to get the RGB in an <code>0x00RRGGBB</code> mask at a (x,y) pixel.</p> <pre><code>public unsafe int getPixel(int x, int y) { byte* imgPointer = (byte*)bmpData.Scan0; int pixelPos = 0; if (y &gt; 0) pixelPos += (y * bmpData.Stride); pixelPos += x * (hasAlpha ? 4 : 3); int blue = *(imgPointer + pixelPos); int green = *(imgPointer + pixelPos + 1); int red = *(imgPointer + pixelPos + 2); int rgb = red &lt;&lt; 16; rgb += green &lt;&lt; 8; rgb += blue; return rgb; } </code></pre> <p>This works flawlessly for all the images I've worked with thus far, except for any image I generate using MSPaint. For example, I made a 5x1 image in paint containing 5 shades of yellow. When I load this image into my program however, the image stride is 16! I suspected it to be 15 (3 bytes per pixel, 5 pixels) but for some reason after the first three bytes (first pixel) there is an extra byte, and then the rest of the pixels follow in the array.</p> <p>I have only found this for images that are saved by MSpaint and I was hoping anyone could explain me what that extra byte is for and how to detect that extra byte.</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.
 

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