# I can‘t open a bmp image using vtkbmpreader

**URL:** https://discourse.vtk.org/t/i-can-t-open-a-bmp-image-using-vtkbmpreader/9025
**Category:** Support
**Created:** [July 27, 2022, 3:57am UTC](https://discourse.vtk.org/t/i-can-t-open-a-bmp-image-using-vtkbmpreader/9025 "2022-07-27T03:57:37Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![BOB0Young](https://discourse.vtk.org/user_avatar/discourse.vtk.org/bob0young/32/5560_2.png) [@BOB0Young](https://discourse.vtk.org/u/BOB0Young)
#### Post date: [July 27, 2022, 3:57am UTC](https://discourse.vtk.org/t/i-can-t-open-a-bmp-image-using-vtkbmpreader/9025/1 "2022-07-27T03:57:37Z")

</div>

vtkSmartPointerReader = vtkSmartPointer::New();  
//Reader-\>SetAllow8BitBMP(16);  
Reader-\>SetDataScalarTypeToUnsignedChar();  
Reader-\>SetFileDimensionality(3);  
Reader-\>SetFilePrefix(“/home/yanghantao/Documents/ruxian/”);  
//Reader-\>SetFileNameSliceSpacing(1);  
Reader-\>SetFilePattern(“%s%03d\_R.bmp”);  
Reader-\>SetDataExtent(0, 2047, 0, 2047, 1, 30);  
Reader-\>SetDataSpacing(1, 1, 10);  
Reader-\>Update();

ERROR: In /home/yanghantao/Documents/VTK-8.2.0/IO/Image/vtkImageReader2.cxx, line 595  
vtkBMPReader (0xe0f1b0): Initialize: Could not open file /home/yanghantao/Documents/ruxian/000\_R.bmp

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [July 27, 2022, 3:16pm UTC](https://discourse.vtk.org/t/i-can-t-open-a-bmp-image-using-vtkbmpreader/9025/2 "2022-07-27T15:16:56Z")

</div>

Hello,

An inaccessible file may be caused by a multitude of factors.

The usual checklist applies (assuming you are on some Unix-like system):

- Do an `ls /home/yanghantao/Documents/ruxian/000_R.bmp` to test whether it exists or is accessible if any of those directories was mounted from some network share.
- Do a `cat /home/yanghantao/Documents/ruxian/000_R.bmp` to test whether you have at least read access to it.
- Do an `ls` on each directory all the way down to the file to test whether you have the execution flag set properly on each of them.
- Open `/home/yanghantao/Documents/ruxian/000_R.bmp` in another program to rule out file corruption.

TIP: place your code between two ``` so it doesn’t get parsed by the markup parser.

regards,

Paulo

---

<div class="post-metadata">

### Author: ![BOB0Young](https://discourse.vtk.org/user_avatar/discourse.vtk.org/bob0young/32/5560_2.png) [@BOB0Young](https://discourse.vtk.org/u/BOB0Young)
#### Post date: [July 28, 2022, 6:57am UTC](https://discourse.vtk.org/t/i-can-t-open-a-bmp-image-using-vtkbmpreader/9025/3 "2022-07-28T06:57:37Z")

</div>

Thank you, I have solved my problem.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [July 28, 2022, 1:04pm UTC](https://discourse.vtk.org/t/i-can-t-open-a-bmp-image-using-vtkbmpreader/9025/4 "2022-07-28T13:04:09Z")

</div>

How did you solve it? Others may run into the same problem in the future.

---

<div class="post-metadata">

### Author: ![BOB0Young](https://discourse.vtk.org/user_avatar/discourse.vtk.org/bob0young/32/5560_2.png) [@BOB0Young](https://discourse.vtk.org/u/BOB0Young)
#### Post date: [July 29, 2022, 3:04am UTC](https://discourse.vtk.org/t/i-can-t-open-a-bmp-image-using-vtkbmpreader/9025/5 "2022-07-29T03:04:23Z")

</div>

> [@BOB0Young](#):
>
> Reader-\>SetAllow8BitBMP(16);

Actually，I don’t really know why. I use another way to reader my series of image. But it seems that `reader->SetAllow8BitBMP(16)` is necessarly.

```auto
        vtkSmartPointer <vtkStringArray> fileArray = 
                        vtkSmartPointer< vtkStringArray >::New();
        char fileName[128];
        for(int i = 1; i < 31; i++)
        {
            sprintf(fileName,"/home/yanghantao/Documents/ruxian/%03d_R.bmp", i);
            std::string fileStr(fileName);
            fileArray->InsertNextValue(fileStr);
        }

        //read BMP
        vtkSmartPointer<vtkBMPReader> reader = vtkSmartPointer<vtkBMPReader>::New();
        reader->SetFileNames(fileArray);
        reader->SetAllow8BitBMP(16);

```

Another quetion about the code `Reader->SetDataExtent(0, 2047, 0, 2047, 1, 30);` I have already I set the fifth and sixth parameters to be 1 and 30 respectively. Why my console show me ` Could not open file /home/yanghantao/Documents/ruxian/000_R.bmp`. Actually I didn’t let him to read the 000\_R.bmp

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [August 2, 2022, 1:42pm UTC](https://discourse.vtk.org/t/i-can-t-open-a-bmp-image-using-vtkbmpreader/9025/6 "2022-08-02T13:42:03Z")

</div>

Hello,

I suggest you to use a debugger to track down the source of that incorrect string. Your loop starts with 1. So why you’re getting an all-zero path you have to probe your code carefully. I’d certainly avoid those dreaded `char *`-based C functions like `printf` and the sort. They are common sources of hard-to-find bugs. Try using modern C++ replacements like:

```cpp
#include <sstream>
#include <iomanip>

(...)

std::string filename;
(...)
   std::stringstream ss;
   ss << "/home/yanghantao/Documents/ruxian/" << std::setw(3) << std::setfill('0') << i << "_R.bmp";
   filename = ss.str();
(...)

```

take care,

Paulo
