Aditya
Aditya

Reputation: 44

how do i know my byte array containing blank .tif file or not

i have a application where i need to check my scanner generates blank tif file or not. here i share my example code

 private void button1_Click(object sender, EventArgs e)
    {
        string path2 = @"F:\3333.tif";
        string path = @"F:\Document Scanned @ 1-blank.tif";
        System.Drawing.Image img = System.Drawing.Image.FromFile(path);
        byte[] bytes;
        using (MemoryStream ms = new MemoryStream())
        {
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Tiff);
            bytes = ms.ToArray();
        }

        System.Drawing.Image img2 = System.Drawing.Image.FromFile(path2);
        byte[] bytes2;

        using (MemoryStream ms3 = new MemoryStream())
        {
            img2.Save(ms3, System.Drawing.Imaging.ImageFormat.Tiff);
            bytes2 = ms3.ToArray();
        }
        bool t = false;
        t = bytes.SequenceEqual(bytes2);
    }

Note: Blank tif image meance white page .

In above bool t always returns true why ? i used two diff images solved

Upvotes: 0

Views: 903

Answers (1)

Sebastian Negraszus
Sebastian Negraszus

Reputation: 12215

Essentially, you are comparing the bytes of two TIF files (with the indirection of using Image). This can fail for various reasons:

Dimension: If the two images do not have the exact same height and width, the byte sequences will -of course- be different even if both are completely white.

Metadata: As far as I know, the TIF format contains various metadata. Therefore, two files may be different even if they have the same pixels. I would recommend manually checking all pixel values (e.g. Bitmap.GetPixel) and comparing them to white (Color.FromArgb(255,255,255,255)).

Noise: Are you sure that a blank file is always pure white (255,255,255)? Maybe some random pixels have slightly different values such as (255,254,255)...

Upvotes: 1

Related Questions