简体   繁体   中英

.bmp is not a windows bitmap?

when I create a bitmap like this:

var testImage = new Bitmap(320, 240);
                var testDataLock = testImage.LockBits(new Rectangle(new Point(), testImage.Size),
                                    System.Drawing.Imaging.ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);

                unsafe
                {
                    var aaa = CamData.ToArray();
                    UInt32 lOffset = 0;
                    UInt32 lPos = 0;
                    byte* lDst = (byte*)testDataLock.Scan0;
                    byte bitshift = 8;
                    fixed (UInt16* lSrc = aaa)
                    {
                        while (lOffset < testImage.Width * testImage.Height)
                        {
                            lDst[lPos] = (byte)(lSrc[lOffset] >> bitshift);
                            lDst[lPos + 1] = lDst[lPos];
                            lDst[lPos + 2] = lDst[lPos];

                            lOffset++;
                            lPos += 3;

                            // take care of the padding in the destination bitmap
                            if ((lOffset % testImage.Width) == 0)
                                lPos += (UInt32)testDataLock.Stride - (uint)(testImage.Width * 3);
                        }

                    }
                }
                testImage.UnlockBits(testDataLock);
                testImage.Save(@"H:\Test.bmp");

I alway get an error while trying to open this file with an visualisation lib:

Unknown file type! H:\test.bmp is not a Windows BMP file!

but in windows I can open the file with the viewer etc... there are no problems does anybody know why I get this error?

thanks

you can save a System.Drawing.Bitmap to a valid windows .bmp like this:

//bmp is a System.Drawing.Bitmap
bmp.Save("MyBitmap.bmp", ImageFormat.Bmp);

The second parameter (which you did not include) specifies the format in which the bitmap must be saved.

Also, be sure to check if your visualisation lib supports 24Bit Per Pixel bitmaps, since this is the format you are creating your bitmap in.

see: PixelFormat.Format24bppRgb

正如您可以在MSDN的“备注”部分中阅读的那样,如果未指定编码器,则图像将另存为PNG。

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