# Load image file which is changed the filename extension

**URL:** <https://forum.commonvisionblox.com/t/load-image-file-which-is-changed-the-filename-extension/1040>\
**Category:** Programming Questions\
**Tags:** csharp\
**Created:** [February 4, 2020, 3:19am UTC](https://forum.commonvisionblox.com/t/load-image-file-which-is-changed-the-filename-extension/1040 "2020-02-04T03:19:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jesse](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@Jesse](https://forum.commonvisionblox.com/u/Jesse)\
**Post date:** [February 4, 2020, 3:19am UTC](https://forum.commonvisionblox.com/t/load-image-file-which-is-changed-the-filename-extension/1040/1 "2020-02-04T03:19:25Z")

</div>

Hi Sir:

I have a CVB Load image problem.

First, I change 1234564.tif.jpg the image filename extension to 1234564.tif , 1234564.jpg , 1234564.bmp.  
Second, I try to use Stemmer.Cvb.Image.FromFile() function and Cvb.Image.LoadImageFile() function to load the three images, but it has a error message.

![tif-1](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/a38a6dcd40f0916093f73ffcf18877d2e7996a44.png)

 ![tif-2](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/2c8b9d7399b9744d273c758386d4197965a3d378.png)

![jpg-1](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/59d89f7863ae858a4e56c861df92603f1bee8ca5.png)

![jpg-2](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/d0532a2cfb662b792935f1eada8f433a68db5f75.png)

![bmp-1](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/ac4812cc8ce97392272df097748dd16d56ba332c.png)

![bmp-2](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/816ad18183bb12f46989a16360d17fdae38307a8.png)

```auto
My C# Code : 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Stemmer.Cvb;

using Cvb;
using CVBImage = Cvb.Image;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                //string filepath = "1234564.jpg";
                //string filepath = "1234564.tif";
                string filepath = "1234564.bmp";

                var image = Stemmer.Cvb.Image.FromFile(filepath);
                image.Save("test.jpg");
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
            
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                Cvb.Image.IMG image = 0;
                //string filepath = "1234564.jpg";
                //string filepath = "1234564.tif";
                string filepath = "1234564.bmp";

                bool r = Cvb.Image.LoadImageFile(filepath, out image);

                bool t = Cvb.Image.WriteImageFile(image, "789.jpg");
            }
            catch(Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
            
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![parsd](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/parsd/32/7_2.png) [@parsd](https://forum.commonvisionblox.com/u/parsd)\
**Post date:** [February 4, 2020, 1:25pm UTC](https://forum.commonvisionblox.com/t/load-image-file-which-is-changed-the-filename-extension/1040/2 "2020-02-04T13:25:24Z")

</div>

Hi @Jesse,

I’m not totally sure what you are trying to do. But :cvb: determins the file type by its extension. If the data inside the file doesn’t resemble that format, loading is not possible.

---

<div class="post-metadata">

**Author:** ![MartinK](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/martink/32/74_2.png) [@MartinK](https://forum.commonvisionblox.com/u/MartinK)\
**Post date:** [February 4, 2020, 2:33pm UTC](https://forum.commonvisionblox.com/t/load-image-file-which-is-changed-the-filename-extension/1040/3 "2020-02-04T14:33:00Z")

</div>

Hi @Jesse,

can it bee that your sample program is targetting 64bit Windows but the Visual Studio configuration setting is _AnyCPU_ and the _Prefer32Bit_ option is checked? In that case you will get all the problems mentioned in several articles here regarding a mix of 32bit and 64bit runtimes such as [here](https://forum.commonvisionblox.com/t/cant-use-ocx-components-on-x64-failed-to-create-axhost/487/2) or [here](https://forum.commonvisionblox.com/t/hello-world-cvb/249/2).

This code here works fine having a _x64_ configuration or having the _Prefer32Bit_ option disabled:

```cs
private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                string ext = "bmp";
                using (var image = Stemmer.Cvb.Image.FromFile("C:\\cvb\\tutorial\\clara." + ext))
                {
                    Console.WriteLine("Writing *." + ext);
                    image.Save("C:\\cvb\\Myclara." + ext);
                    ext = "png";
                    Console.WriteLine("Writing *." + ext);
                    image.Save("C:\\cvb\\Myclara." + ext);
                    ext = "tif";
                    Console.WriteLine("Writing *." + ext);
                    image.Save("C:\\cvb\\Myclara." + ext);
                    ext = "jpeg";
                    Console.WriteLine("Writing *." + ext);
                    image.Save("C:\\cvb\\Myclara." + ext);
                    ext = "jpg";
                    Console.WriteLine("Writing *." + ext);
                    image.Save("C:\\cvb\\Myclara." + ext);
                }
                Console.WriteLine("Finished");
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex);
            }
        }

```

In more detail: Bitmaps and TIFs (and all of its high-bit versions) are written by the _CVCImg.DLL_. Other formats such as PNG, TGA, JPEG, JPEG2000 etc. are implemented in the _CVCFile.DLL_ which makes use of OpenSource libraries. The loading of the _CVCFile.DLL_ fails in case of the 32/64 bit mismatch.

kind regards,  
Martin

---

<div class="post-metadata">

**Author:** ![Jesse](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@Jesse](https://forum.commonvisionblox.com/u/Jesse)\
**Post date:** [February 7, 2020, 2:52am UTC](https://forum.commonvisionblox.com/t/load-image-file-which-is-changed-the-filename-extension/1040/4 "2020-02-07T02:52:04Z")

</div>

Hi MartinK and Parsd:

Because my user’s AOI machine save the image file extension to be \*.tif,jpg, I use mouse to change the image’s extension to be \*.tif, \*.jpg, and \*.bmp and use Stemmer.Cvb.Image.FromFile() function and Cvb.Image.LoadImageFile() function to load the images.

I disable Prefer32Bit and try the Martin’s C# code to load C:\cvb\tutorial\clara."bmp is normal.  
I use the same code to load my _.tif,jpg,_.tif, \*.jpg, and \*.bmp images , has load files error.

I open the \*.tif.jpg image and \*.tif, \*.jpg, and \*.bmp images which be changed extension by paint ![2020_02_07_10_13_21_開始_功能表](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/d3f60af66decb59c444016dbfab2580f199b8361.png) , they can be opened.

thank you for helping me to check.

---

<div class="post-metadata">

**Author:** ![illusive](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/illusive/32/61_2.png) [@illusive](https://forum.commonvisionblox.com/u/illusive)\
**Post date:** [February 7, 2020, 8:32am UTC](https://forum.commonvisionblox.com/t/load-image-file-which-is-changed-the-filename-extension/1040/5 "2020-02-07T08:32:53Z")

</div>

Hi @Jesse,

@parsd actually gave you then answer:

> [@parsd](#):
>
> I’m not totally sure what you are trying to do. But :cvb: determins the file type by its extension. If the data inside the file doesn’t resemble that format, loading is not possible.

Some software packages (like for example Windows Paint) determine the Bitmap file type by looking at the first few bytes of a file and if these match a known bitmap format the use the matching loader. :cvb: just looks at the file’s extension. Therefore if you take - for example - a _\*.bmp_ file and rename it to - for example - _\*.jpg_, :cvb: will simply apply the jpg loader to it, but the content is **not** a jpeg file and therefore loading Fails.

In short: If the file name’s extension does not match the file type, :cvb: won’t be able to load it.

---

<div class="post-metadata">

**Author:** ![Jesse](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@Jesse](https://forum.commonvisionblox.com/u/Jesse)\
**Post date:** [February 7, 2020, 8:53am UTC](https://forum.commonvisionblox.com/t/load-image-file-which-is-changed-the-filename-extension/1040/6 "2020-02-07T08:53:41Z")

</div>

> [@illusive](#):
>
> will simply apply the jpg loader to it, but the content is **not**

Hi illusive:

Thank you for reply me. I understand why the reason of loading file failed .
