Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating 16-bit+ grayscale images in WPF
    primarykey
    data
    text
    <p>I want to create a 16-bit grayscale image from data values in my WPF program. Currently I have been looking at using a WriteableBitmap with PixelFormats.Gray16 set.</p> <p>However I can't get this to work, and a Microsoft page (http://msdn.microsoft.com/en-us/magazine/cc534995.aspx) lists the Gray16 format as not writeable via the WriteableBitmap, but does not suggest how else to make one in this way.</p> <p>Currently my code operates within a loop, where <code>i</code> represents the image height and <code>j</code> the width, and looks something like this:</p> <pre><code>short dataValue = GetDataSamplePixelValue(myDataValue); //the pixel to fill with this data value: int pixelOffset = ((i * imageWidth) + j) * bytesPerPixel; //set the pixel colour values: pixels[pixelOffset] = dataValue; </code></pre> <p>I do get an image with this but it is just a bunch of vertical black and white lines. I don't have a problem if using just 8-bit grayscale data (in which case in the above example <code>short</code> is changed to <code>byte</code>).</p> <p>Does anyone know how to create a 16-bit per pixel or higher grayscale image using WPF? This image will ultimately need to be saved as well.</p> <p>Any advice is much appreciated.</p> <p>EDIT</p> <p>Further to this I have done some editing and am now getting a sensible image using the Gray16 PixelFormat. It's very difficult for me to tell if it is actually 16-bit though, as a colour count by an image program gives 256, and I am not sure if this is because the image is being constrained by WPF, or perhaps the image program does not support it as apparently many image programs ignore the lower 8-bits. For now I will stick with what I have.</p> <p>For information the code is like this: </p> <pre><code>myBitmap = new WriteableBitmap((int)visualRect.Width, (int)visualRect.Height, 96, 96, PixelFormats.Gray16, null); int bytesPerPixel = myBitmap.Format.BitsPerPixel / 8; ushort[] pixels = new ushort[(int)myBitmap.PixelWidth * (int)myBitmap.PixelHeight]; //if there is a shift factor, set the background colour to white: if (shiftFactor &gt; 0) { for (int i = 0; i &lt; pixels.Length; i++) { pixels[i] = 255; } } //the area to be drawn to: Int32Rect drawRegionRect = new Int32Rect(0, 0, (int)myBitmap.PixelWidth, (int)myBitmap.PixelHeight); //the number of samples available at this line (reduced by one so that the picked sample can't lie beyond the array): double availableSamples = myDataFile.DataSamples.Length - 1; for (int i = 0; i &lt; numDataLinesOnDisplay; i++) { //the current line to use: int currentLine = ((numDataLinesOnDisplay - 1) - i) + startLine &lt; 0 ? 0 : ((numDataLinesOnDisplay- 1) - i) + startLine; for (int j = 0; j &lt; myBitmap.PixelWidth; j++) { //data sample to use: int sampleToUse = (int)(Math.Floor((availableSamples / myBitmap.PixelWidth) * j)); //get the data value: ushort dataValue = GetDataSamplePixelValue(sampleToUse); //the pixel to fill with this data value: int pixelOffset = (((i + shiftFactor) * (int)myBitmap.PixelWidth) + j); //set the pixel colour values: pixels[pixelOffset] = dataValue; } } //copy the byte array into the image: int stride = myBitmap.PixelWidth * bytesPerPixel; myBitmap.WritePixels(drawRegionRect, pixels, stride, 0); </code></pre> <p>In this example startLine and shiftFactor are already set, and depend on from which point in the data file the user is viewing, with shiftFactor only non-zero in the cases of a data file smaller than the screen, in which case I am centering the image vertically using this value.</p>
    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.
 

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