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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:53:41+00:00 2026-05-25T21:53:41+00:00

I have a performance critical operation where I need to work with 1bpp images.

  • 0

I have a “performance critical” operation where I need to work with 1bpp images.
Actually I’m using the Bitmap class and I’m doing, each iteration of a graphic update cycle, a copy of the bitmap inside the byte array.

Watching my task manager, this is not that I can keep doing: it uses 2% cpu all time, I think it’s quite a lot for something like an utility program.

I need to waste less memory as possible and almost 0 cpu. The image is 160×43, quite small.

Why I am not using directly the byte array? Easy: I would like to write over it, do some common operations which I don’t want to rewrite by myself.

I can use obviusly a different image class (from wpf for example, I don’t know). I need the possibility to work with a 1bpp image.

Offtopic:
I have the same “problem” with a 32bpp image, I need a way to work with it as an image while it is a byte array, I can’t make a copy of my bytes each time!!! I’m wasting cpu in this way.

  • 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-25T21:53:41+00:00Added an answer on May 25, 2026 at 9:53 pm

    You should use Bitmap.LockBits to get the underlying memory of the bitmap pinned (so the friendly garbage collector won’t move it around for you).

    As an example, this small program loads a png, sets a simple pixel pattern, and saves the resulting image:

    unsafe void Main()
    {
        Bitmap bm = (Bitmap)Image.FromFile("D:\\word.png");
        var locked = bm.LockBits(new Rectangle(0,0,bm.Width, bm.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
        try 
        {
            byte v = 0xaa;
            byte* pBuffer = (byte*)locked.Scan0;
            for(int r = 0 ; r < locked.Height ; r++) 
            {
                byte* row = pBuffer + r*locked.Stride;
                for(int c = 0 ; c < locked.Stride ; c++) 
                    row[c] = v;
            }
        }
        finally
        {
            bm.UnlockBits(locked);
        }
        bm.Save("D:\\generated.png");
    }
    

    LockBits will have an overhead depending on whether it needs to convert the internal memory representation to what you request (so it might matter where you get it from). As alwyas with performance, measure and profile to find your bottlenecks.

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

Sidebar

Related Questions

I have a performance critical piece of code for which I am considering using
I have a piece of performance critical code written with pointers and dynamic memory.
We have some performance problems with one of our applications. I thought about using
I have several performance issue in my website. I'm using asp.net mvc 2 and
I have a situation (where performance is critical) to check if a customer account
Background: I have a performance-critical query I'd like to run and I don't care
I have a simple (but performance critical) algorithm in C (embedded in C++) to
I'm writing Objective-C code with LLVM. I have one file full of very performance-critical
I'm writing a performance-critical .NET application which makes heavy use of multithreading. Using the
I have a performance critical inline function, inline T func(T a, T b, int

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.