I don't know what that error means exactly, but check if bmap_to_format actually worked.
If it returns 0, then the conversion failed.

Also, as you can read in the manual under "file formats", the use of the jpg is discouraged:
Quote:
JPG images should not be used for textures in realtime games. They combine all disadvantages of the other formats, plus the additional disadvantage of slow loading, high video memory consumption, and bad texture quality. Their only advantage is small file size.


Also:
Quote:
Also supported, but not recommended are BMP, PNG and JPG images - they are read through the DirectX library and thus depend on the DirectX version, and have other shortcomings. The selected image format does matter because all formats have their specific advantages and disadvantages:

So maybe DirectX loads them in a way so that you can't use bmap lock and other manipulation functions. You might want to try the same code with another file format.

Quote:
Recommended image formats are TGA, DDS, PCX and WAD.


Hope this helped.