Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8617989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:00:20+00:00 2026-06-12T06:00:20+00:00

This puzzles me for last two hours. Reading an image file results in different

  • 0

This puzzles me for last two hours. Reading an image file results in different pixel values between imread in Matlab and Image.FromFile in C#?

aa=imread('myfile.tif')

max(aa(:)) = 248 in matlab

In C#

var image2Array = imageToByteArray((Bitmap) Image.FromFile("myfile.tif"));
byte maxx = 0;
foreach(var a in image2Array)
{
     maxx = Math.Max(maxx, a);
}
//maxx = 255

Futhermore, in Matlab,

aa(1,1) = 13, 
aa(1,2) = 13 

but in C#

image2Array[0]=17,  
image2Array[1]=0

They should be the same.

BTW, in this case, pixel type is uint8. so there is no dimensional difference.

If you ask me how I got byte array from Image, I used MSDN document to make this method.

    public byte[] imageToByteArray(Bitmap bmp)
    {
        // Lock the bitmap's bits.  
        Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
        BitmapData bmpData = bmp.LockBits(
            rect,
            ImageLockMode.ReadWrite,
            bmp.PixelFormat);

        // Get the address of the first line.
        IntPtr ptr = bmpData.Scan0;

        // Declare an array to hold the bytes of the bitmap. 
        int bytes = Math.Abs(bmpData.Stride)*bmp.Height;
        byte[] rgbValues = new byte[bytes];

        // Copy the RGB values into the array.
        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
        // Unlock the bits.
        bmp.UnlockBits(bmpData);
        return rgbValues;
    }

What did I do wrong here? I suspect that they use different reading algorithms because two resulting images look same.

UPDATE:

I don’t think there is anything wrong with what I was doing. I concluded that reading tif as a bitmap was the cause of the problem. To confirm this theory,

  1. I displayed the two images and they looked exactly the same. So there is no mistake on my part, I think.

  2. I tried to read the same file with opencv and its pixel values were exactly the same as the ones from matlab. This was surprisingly to me. I would very cautiously use Bitmap in C# from now on.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:00:21+00:00Added an answer on June 12, 2026 at 6:00 am

    TIFF has many formats, you are attempting to read it as a bitmap.

    I suggest reading it using a proprietary TIFF reader instead : Good Tiff library for .NET

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been banging my head with this issue for the last two days. Googled
This question relates to those parts of the KenKen Latin Square puzzles which ask
This really puzzled for hours, I searched all over the internet, but got no
Everything in this little VBS script works right up until I reach the last
This has puzzled me for two days now. I have a LinearLayout like the
This ruby/rails construct always puzzles me: User.where(:name => Thiago).limit(3).using(:slave_one) This must execute from left-to-right,
i am writing algorithm for puzzle game.I am putting puzzles in spiral in this
I have this algorithm that I found here, just one thing puzzles me about
I am a bit puzzled reading this: Gcc 4.5 online manual --- Standards section
Please see this image: Can someone explain the difference? Edit Let me indicate what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.