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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:28:53+00:00 2026-05-12T17:28:53+00:00

I need to access each pixel of a Bitmap, work with them, then save

  • 0

I need to access each pixel of a Bitmap, work with them, then save them to a Bitmap.

Using Bitmap.GetPixel() and Bitmap.SetPixel(), my program runs slowly.

How can I quickly convert Bitmap to byte[] and back?

I need a byte[] with length = (4 * width * height), containing RGBA data of each pixel.

  • 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-12T17:28:54+00:00Added an answer on May 12, 2026 at 5:28 pm

    You can do it a couple of different ways. You can use unsafe to get direct access to the data, or you can use marshaling to copy the data back and forth. The unsafe code is faster, but marshaling doesn’t require unsafe code. Here’s a performance comparison I did a while back.

    Here’s a complete sample using lockbits:

    /*Note unsafe keyword*/
    public unsafe Image ThresholdUA(float thresh)
    {
        Bitmap b = new Bitmap(_image);//note this has several overloads, including a path to an image
    
        BitmapData bData = b.LockBits(new Rectangle(0, 0, _image.Width, _image.Height), ImageLockMode.ReadWrite, b.PixelFormat);
    
        byte bitsPerPixel = GetBitsPerPixel(bData.PixelFormat);
    
        /*This time we convert the IntPtr to a ptr*/
        byte* scan0 = (byte*)bData.Scan0.ToPointer();
    
        for (int i = 0; i < bData.Height; ++i)
        {
            for (int j = 0; j < bData.Width; ++j)
            {
                byte* data = scan0 + i * bData.Stride + j * bitsPerPixel / 8;
    
                //data is a pointer to the first byte of the 3-byte color data
                //data[0] = blueComponent;
                //data[1] = greenComponent;
                //data[2] = redComponent;
            }
        }
    
        b.UnlockBits(bData);
    
        return b;
    }
    

    Here’s the same thing, but with marshaling:

    /*No unsafe keyword!*/
    public Image ThresholdMA(float thresh)
    {
        Bitmap b = new Bitmap(_image);
    
        BitmapData bData = b.LockBits(new Rectangle(0, 0, _image.Width, _image.Height), ImageLockMode.ReadWrite, b.PixelFormat);
    
        /* GetBitsPerPixel just does a switch on the PixelFormat and returns the number */
        byte bitsPerPixel = GetBitsPerPixel(bData.PixelFormat);
    
        /*the size of the image in bytes */
        int size = bData.Stride * bData.Height;
    
        /*Allocate buffer for image*/
        byte[] data = new byte[size];
    
        /*This overload copies data of /size/ into /data/ from location specified (/Scan0/)*/
        System.Runtime.InteropServices.Marshal.Copy(bData.Scan0, data, 0, size);
    
        for (int i = 0; i < size; i += bitsPerPixel / 8 )
        {
            double magnitude = 1/3d*(data[i] +data[i + 1] +data[i + 2]);
    
            //data[i] is the first of 3 bytes of color
    
        }
    
        /* This override copies the data back into the location specified */
        System.Runtime.InteropServices.Marshal.Copy(data, 0, bData.Scan0, data.Length);
    
        b.UnlockBits(bData);
    
        return b;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 285k
  • Answers 285k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Java always makes a copy of parameters before sending them… May 13, 2026 at 4:48 pm
  • Editorial Team
    Editorial Team added an answer You can do this by setting the FileAttributes for the… May 13, 2026 at 4:48 pm
  • Editorial Team
    Editorial Team added an answer The explanation is at http://clojure.org/java_interop. user> (macroexpand '(.PI Math)) (.… May 13, 2026 at 4:48 pm

Related Questions

unfortunately I cannot resort to C# in my current project, so I'll have to
I have a large set of data (a data cube of 250,000 X 1,000
I need good word-wrapping handling for Java. Not too difficult, except for one wrinkle:
I have a web application inside where i need to check whether the user

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.