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

  • Home
  • SEARCH
  • 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 3992238
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:44:27+00:00 2026-05-20T06:44:27+00:00

This is my piece of code that i gathered from here . private unsafe

  • 0

This is my piece of code that i gathered from here.

private unsafe byte[] BmpToBytes_Unsafe(Bitmap bmp)

        {

            BitmapData bData = bmp.LockBits(new Rectangle(0,0,1000,1000),

                ImageLockMode.ReadOnly,

                PixelFormat.Format24bppRgb);

            // number of bytes in the bitmap



            int byteCount = bData.Stride * bmp.Height;

            byte[] bmpBytes = new byte[byteCount];



            // Copy the locked bytes from memory





           Marshal.Copy(bData.Scan0, bmpBytes, 0, byteCount);

            Marshal.



            // don't forget to unlock the bitmap!!

            bmp.UnlockBits(bData);



            return bmpBytes;

        }

I have a function that gets byte from above mentioned function and just displays it without further processing. But i get the inverted image at the output. Can somebody explain?

  • 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-05-20T06:44:27+00:00Added an answer on May 20, 2026 at 6:44 am

    When you say “inverted”, I suppose that you mean upside down?

    You can’t rely on the “l33t hax0r skillz” of the person posting that code. He lacks some vital information about how bitmaps are handled in memory.

    When you read the data from the bitmap you can’t read it all in one chunk. The data is stored in lines, and the lines may be stored either with the top line first or the bottom line first. Also, there may be padding between the lines to get each line on an even word boundary.

    The Scan0 property is a pointer to the start of the first line, and the Stride property is the offset to the start of the next line. The Width property can be used to determine how much data there is in each line.

    So, you have to copy the data one line at a time:

    int lineSize = bData.Width * 3;
    int byteCount = lineSize * bData.Height;
    byte[] bmpBytes = new byte[byteCount];
    
    IntPtr scan = bData.Scan0;
    for (int i = 0; i < bData.Height; i++) {
      Marshal.Copy(scan, bmpBytes[i * lineSize], 0, lineSize);
      scan += bData.Stride;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been here and accordingly found out that this piece of code here var
i have this piece of code that selects all users from the table and
here's my problem. I have this piece of code that sets the user location
I have this piece of code that drives me mad. FUNCTION DiskInDrive(CONST DriveNumber: Byte):
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have this piece of code that works fine in subsonic 2.2, I migrated
I have this piece of code that is giving me trouble. I know all
I wrote this piece of code that is supposed to redirect something written on
I have this small piece of code that basically takes a list and runs
I've got this really simple piece of code that I thought was the correct

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.