简体   繁体   中英

create .bmp from ushort array

I have several problem with creating a bitmap from a array. I have a camera and from this I get grayscale values in ushort format. But how to create a bitmap from this values? Only:

System.Drawing.Bitmap checks = new System.Drawing.Bitmap(10,  10);
.
.
checks.Save(@"C:\test.bmp", ImageFormat.Bmp);

will not work:(. I get an image and can open it with window tools, but when I will open the file with another graphic lib, I get alot of errors. So does anybody now how to create a correct bmp file with header etc? does anybody have some code example? this would help most.

thanks

You should create a Bitmap with the right dimensions (width, height), and use LockBits to get a handle to memory that you should write to. If your data is in a .NET supported PixelFormat, you can pass that to LockBits and simply copy data. If not, you might have to do some data conversion manually.

It all boils down to what format you receive data samples in, but the above description outlines the steps you need to take to generate your image.

Update : Since your data is 16 bits gray scale, there is a PixelFormat you can use directly, PixelFormat.16bppGrayScale .

public class(path,wid,height,boolean)
{ 

System.Drawing.Image myThumbnail150;
            System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
            System.Drawing.Image imagesize = System.Drawing.Image.FromFile(pic.FilePath);
            using (Bitmap bitmapNew = new Bitmap(imagesize))
            {

            double maxWidth = Convert.ToDouble(ConfigurationSettings.AppSettings["ImageWidth"]);
            double maxHeight = Convert.ToDouble(ConfigurationSettings.AppSettings["ImageHeight"]);
            int w = imagesize.Width;
            int h = imagesize.Height;
            // Longest and shortest dimension 
            int longestDimension = (w > h) ? w : h;
            int shortestDimension = (w < h) ? w : h;
            // propotionality  
            float factor = ((float)longestDimension) / shortestDimension;
            // default width is greater than height    
            double newWidth = maxWidth;
            double newHeight = maxWidth / factor;
            // if height greater than width recalculate  
            if (w < h)
            {
                newWidth = maxHeight / factor;
                newHeight = maxHeight;
            }
            myThumbnail150 = bitmapNew.GetThumbnailImage((int)newWidth, (int)newHeight, myCallback, IntPtr.Zero);
            string name = pic.Name.Replace(Path.GetExtension(pic.Name), ".Bmp");

            //Create a new directory name ThumbnailImage
            //Save image in TumbnailImage folder
            myThumbnail150.Save(yourpath+ name, System.Drawing.Imaging.ImageFormat.Bmp);
            bitmapNew.Dispose();
}

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