开发者

.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 &l开发者_高级运维t; 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


As you can read at MSDN in the Remarks section your image will be saved as PNG if no encoder is specified.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜