# Crop Image C++ using Rect() Map()

**URL:** <https://forum.commonvisionblox.com/t/crop-image-c-using-rect-map/2239>\
**Category:** C++ API\
**Created:** [October 14, 2024, 3:00pm UTC](https://forum.commonvisionblox.com/t/crop-image-c-using-rect-map/2239 "2024-10-14T15:00:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emma](https://avatars.discourse-cdn.com/v4/letter/e/57b2e6/32.png) [@Emma](https://forum.commonvisionblox.com/u/Emma)\
**Post date:** [October 14, 2024, 3:00pm UTC](https://forum.commonvisionblox.com/t/crop-image-c-using-rect-map/2239/1 "2024-10-14T15:00:52Z")

</div>

Hi Everyone!

I hope someone can help me with an issue I’m facing when cropping an image. I’m using the Rect() function to define a rectangle for cropping and the Map() function to crop the image.

When I use Rect(0, 0, 100, 100), the CreateImageMap() function successfully creates the cropped image for display. However, when I try to move the rectangle by defining it as Rect(5, 0, 100, 100) (to shift the cropping region), I get an error in the CreateImageMap() function.

ERROR: C-API call failed; try getting a log by using CVB LogGUI/silogutil: {“Name”: “CreateImageMap”, “CVC\_ERROR\_CODE”: 1}

Has anyone encountered a similar issue or know why this happens when changing the `x` or `y` coordinates of the rectangle?

Thanks in advance for your help!

---

<div class="post-metadata">

**Author:** ![MandanaS](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/mandanas/32/1211_2.png) [@MandanaS](https://forum.commonvisionblox.com/u/MandanaS)\
**Post date:** [October 15, 2024, 9:25am UTC](https://forum.commonvisionblox.com/t/crop-image-c-using-rect-map/2239/2 "2024-10-15T09:25:49Z")

</div>

Hi Emma,

I have tested the functionality with CVB 14.1.000 and it works as expected. You can find the C++ code below. I will test it with the latest CVB version and update you here.

```cpp
int main()
{

  try
  {
    const auto baseName = Cvb::String("FruitBowl");
    const auto extension = Cvb::String(".bmp");
    const Cvb::String fileName = Cvb::InstallPath() + Cvb::String("tutorial/") + baseName + Cvb::String("Bayer") + extension;
    std::unique_ptr<Cvb::Image> originalImage = Cvb::Image::Load(fileName);
    Cvb::Rect<int> cropRect(50, 50, 200, 200);
    std::unique_ptr<Cvb::Image> croppedImage = originalImage->Map(cropRect);
    croppedImage->Save("./cropped.bmp");

  }
  catch (const std::exception &error)
  {
    std::cout << error.what() << std::endl;
  }
  return 0;
}

```

---

<div class="post-metadata">

**Author:** ![MandanaS](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/mandanas/32/1211_2.png) [@MandanaS](https://forum.commonvisionblox.com/u/MandanaS)\
**Post date:** [October 15, 2024, 11:47am UTC](https://forum.commonvisionblox.com/t/crop-image-c-using-rect-map/2239/3 "2024-10-15T11:47:36Z")

</div>

@Emma I tested with CVB 14.1.002, it works as well.

---

<div class="post-metadata">

**Author:** ![Emma](https://avatars.discourse-cdn.com/v4/letter/e/57b2e6/32.png) [@Emma](https://forum.commonvisionblox.com/u/Emma)\
**Post date:** [October 16, 2024, 7:27am UTC](https://forum.commonvisionblox.com/t/crop-image-c-using-rect-map/2239/4 "2024-10-16T07:27:02Z")

</div>

Thanks for the update 🙂
