Load image file which is changed the filename extension

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

jpg-1

jpg-2

bmp-1

bmp-2

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());
            }
            
        }
    }
}

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.

1 Like

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 or here.

This code here works fine having a x64 configuration or having the Prefer32Bit option disabled:

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

1 Like

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 paint2020_02_07_10_13_21_開始_功能表 , they can be opened.

thank you for helping me to check.

Hi @Jesse,

@parsd actually gave you then answer:

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.

2 Likes

Hi illusive:

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

1 Like