Preview image damaged

Hello, i have problem with get model preview image. I used On_WindowsBitmap from openNURBS and his functions Read and Write. But the result is not a picture that i can write as image (Viewer said that its damaged). There is part of my code. Can u advice me please?

NSString *filePath = [NSString stringWithFormat:@"%@/%@", [localDocumentsDirectoryURL path], @"preview.bmp"];
FILE *fp = ON::OpenFile([filePath UTF8String], "wb");
ON_WindowsBitmap bitmap;  
Read3dmThumbnailPreviewImage([[model documentsFilename] UTF8String], bitmap);
ON_BinaryFile file(ON::write, fp);
bitmap.Write(file);
ON::CloseFile(fp);

maybe someone can help me and show how i can get preview image from model? because i trying solve this problem for about a weak and have just not readable files.

Can we assume you are the code from this example?

Have you traced through the code to see if you can figure out what is going on?

Note, not all 3DM files have thumbnail preview images. Make sure your test file does.

Thanks for your reply!
Yes, i use this code for read file BitmapInfo. Then i try to write image in bmp file with function Write from On_WindowsBitmap. But result is file that cant be read.
I looked in file and did not see “BM” that should be in Bitmap Header. It must be so?
I tried to write it manually but my attempts were unsuccessful.

Hello again, i solved my problem today.
I think I should have said earlier that i working with OSX. May be features of this os did not allow me use default Write from openNURBS.
I wrote own function that writes file pixel by pixel and its working fine now.

Our openNURBS toolkit is used in MacRhino. So we are fairly confident that file I/O works.

ON_WindowsBitmap::Write requires an ON_BinaryArchive object that has been opened for writing. Did you create this?

Yes, my first post contains code example where i create ON_BinartyFile. Or i do it wrong?

I’m no Objective-C expert, but at a glace it looks correct. You might put some error checking in (validate the pointer returned by ON::OpenFile and check the return values of Read3dmThumbnailPreviewImage and ON_WindowsBitmap::Write. You should also add a break point on your code and step into each function to see if it succeeding or not.