简体   繁体   中英

ushort array to Image object

I have an array of ushort pixel data that I need to save as a jpeg file. From what I've found I can do this by using

Image.Save(path, ImageFormat.Jpeg);

but I don't know how to get the ushort data into the Image object. I've found ways to do this with byte arrays but not ushort. I've spent far to much time trying to figure this out so now I ask the mighty StackOverflow, How do I this?

Edit: Sorry, the ushorts are 16 bit grayscale values.

This is a complete working example based on the accepted answer:

 public static void SaveJpg(string fileName,int sizeX,int sizeY,ushort [] imData)
    {
        var bitmap = new Bitmap(sizeX, sizeY, PixelFormat.Format48bppRgb);
        int count = 0;
        for (int y = 0; y < sizeY; y++)
        {
            for (int x = 0; x < sizeX; x++)
            {
                bitmap.SetPixel(x, y, Color.FromArgb(imData[count], imData[count], imData[count]));
                count++;
            }
        }
        bitmap.Save(fileName, ImageFormat.Jpeg);

    }

I think you have to actually create a Bitmap , draw the pixels onto it and save it afterwards.

Something like this:

var bitmap = new Bitmap(sizeX, sizeY, Imaging.PixelFormat.Format16bppGrayScale)

for (y = 0; ...)
for (x = 0; ...)
{
  bitmap.SetPixel(x, y, color information from ushort array);
}

bitmap.Save("filename.jpg", ImageFormat.Jpeg);

Note that I don't know how to get a 16 bit greyscale color information into the Color struct.

I believe you want to use the Bitmap class, which inherits from Image. This MSDN reference may assist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM