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 8213537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:09:09+00:00 2026-06-07T11:09:09+00:00

I have a method to copy the data out of a System.Drawing.Bitmap which looks

  • 0

I have a method to copy the data out of a System.Drawing.Bitmap which looks like this:

var readLock = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
byte[] data = new byte[3 * image.Width * image.Height];
if (data.Length != readLock.Stride * readLock.Height)
    throw new InvalidOperationException("Incorrect number of bytes");
Marshal.Copy(readLock.Scan0, data , 0, data.Length);
image.UnlockBits(readLock);

Pretty simple, and it works for most of my images. However for a very small image (14×14) it hits the exception. In the failing case Stride is 44, not 42 (14 * 3) as expected.

The pixel format is Format24bppRgb, so there should be three bytes for every pixel in the image. Where are these extra bytes coming from, and how can I deal with them when processing the image data?

For anyone interested, I’m generating Normal data from a heightmap, so I need to be able to get each pixel and its neighbours accurately).

  • 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-07T11:09:11+00:00Added an answer on June 7, 2026 at 11:09 am

    Every pixel line of Bitmap must be aligned, that’s why stride is not always width * bytes-per-pixel. You should ignore any extra bytes. It means that if you are working with byte arrays with unaligned data, you might not always be able to copy all image data in a single Marshal.Copy() call. Every line of pixels starts at readLock.Scan0 + y * readLock.Stride and contains readLock.Width * bytes-per-pixel meaningful bytes.

    Solution:

    const int BYTES_PER_PIXEL = 3;
    var data = new byte[readLock.Width * readLock.Height * BYTES_PER_PIXEL];
    if(readLock.Stride == readLock.Width * BYTES_PER_PIXEL)
    {
        Marshal.Copy(readLock.Scan0, data, 0, data.Length);
    }
    else
    {
        for(int y = 0; y < readLock.Height; ++y)
        {
            IntPtr startOfLine = (IntPtr)((long)readLock.Scan0 + (readLock.Stride * y));
            int dataOffset = y * readLock.Width * BYTES_PER_PIXEL;
            Marshal.Copy(startOfLine, data, dataOffset, readLock.Width * BYTES_PER_PIXEL);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does Java not have a file copy method? This seems like such an
I have been using this copy method for quite a while, in lots of
Dont why this is happening I have a method getOutage which returns an array
Hej, I have some data shipped out with the app which shall be copied
When I have my own init method with synthesized properties as such: @property (copy,
I have method that returns Drawable , and if its Bitmap object is recycled
I create small application which is media player. I have method where I have
I have a Canvas I draw on, I'm trying to take the bitmap out,
I'm writing a system at the moment that needs to copy data from a
Suppose we have an arbitrary graph represented by nodes and pointers like this: class

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.