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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:21:51+00:00 2026-05-20T07:21:51+00:00

I have a class that creates a matrix of Color values by reading a

  • 0

I have a class that creates a matrix of Color values by reading a Bitmap. The class directly reads each byte in the pixels inside an unsafe block using the pointer to the image. The purpose of the class is to read the pixel values into memory where I can run filters on them before saving the image as a new file.

I can recreate the image using GDI+‘s setPixel() method, however it is too slow for my needs.

I am attempting to save a new image file using the following function:

    public void saveImageFromPixels()
    {

        this.newBitmap = new Bitmap(srcBitmap.Width, srcBitmap.Height);

        BitmapData imgData = newBitmap.LockBits(new Rectangle(0, 0, newBitmap.Width, newBitmap.Height),
                                                ImageLockMode.ReadWrite,
                                                PixelFormat.Format24bppRgb);

        int stride = imgData.Stride;
        System.IntPtr Scan0 = imgData.Scan0;

        unsafe
        {
            byte* p = (byte*)(void*)Scan0;
            int nOffset = stride - newBitmap.Width * 3;

            for (int x = 0; x < newBitmap.Height; ++x)
            {

                for (int y = 0; y < newBitmap.Width; ++y)
                {
                    p[0] = (byte)(255 - matrix[y][x].B);
                    p[1] = (byte)(255 - matrix[y][x].G);
                    p[2] = (byte)(255 - matrix[y][x].R);
                    p += 3;
                }
                p += nOffset;
            }


        }

        this.newBitmap.Save(@"C:\images\1-d.jpg");
    }

However the result is an empty image (with the proper dimensions). The code that directly accesses the pixels and saves the value as a Color works fine, it is just saving the image I am having problems with.

The following code defines srcBitmap and newBitmap

    private Bitmap srcBitmap;
    private Bitmap newBitmap;
    private List<List<Color>> matrix;

    public PixelMatrix(string path)
    {

        this.srcBitmap = new Bitmap(path);

        this.matrix = new List<List<Color>>(srcBitmap.Width);

        for (int x = 0; x < srcBitmap.Width; x++)
        {
            this.matrix.Add(new List<Color>(srcBitmap.Height));
        }

    }
  • 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-20T07:21:52+00:00Added an answer on May 20, 2026 at 7:21 am

    You need to UnlockBits().

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

Sidebar

Related Questions

I have a class that I can have many instances of. Inside it creates
I have a PHP class that creates a SQL query based on values entered
I have a class that creates several IDisposable objects, all of these objects are
I have a class that creates a file. I am now doing integration tests
I have a PHP class that creates a PNG image on the fly and
I have a class Page that creates an instance of DB , which is
I have a utility class that creates & returns a db connection: Public Shared
I have a CLickableBox class that creates boxes for me and now I need
Example: I have an class that inherits from UIImageView. An object creates an instance
I have the following code that creates a serverside object of the xmlhttp 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.