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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:12:45+00:00 2026-06-11T10:12:45+00:00

I am trying to understand why I have troubles creating a Bitmap from a

  • 0

I am trying to understand why I have troubles creating a Bitmap from a byte array. I post this after a careful scrutiny of the existing posts about Bitmap creation from byte arrays, like the followings:

  • Creating a bitmap from a byte[],
  • Working with Image and Bitmap in c#?,
  • C#: Bitmap Creation using bytes array

My code is aimed to execute a filter on a digital image 8bppIndexed writing the pixel value on a byte[] buffer to be converted again (after some processing to manage gray levels) in a 8BppIndexed Bitmap

My input image is a trivial image created by means of specific perl code:

input image

Of course, after executing the filter the output image has lost the first and last rows and the first and last columns, due to the way the filter manage borders, so from the original 256 x 256 image i get a 254 x 254 image.

Just to stay focused on the issue I have commented the code responsible for executing the filter so that the operation really performed is an obvious:

ComputedPixel = InputImage.GetPixel(myColumn, myRow).R;  

I know, i should use lock and unlock but I prefer one headache one by one. Anyway this code should be a sort of identity transform, and at last i use:

private unsafe void FillOutputImage()
{           
    OutputImage = new Bitmap (OutputImageCols, OutputImageRows, PixelFormat .Format8bppIndexed);
    ColorPalette ncp = OutputImage.Palette;
    for (int i = 0; i < 256; i++)
        ncp.Entries[i] = Color .FromArgb(255, i, i, i);
    OutputImage.Palette = ncp;

    Rectangle area  = new Rectangle(0, 0, OutputImageCols,  OutputImageRows);
    var data = OutputImage.LockBits(area, ImageLockMode.WriteOnly, OutputImage.PixelFormat);
    Marshal.Copy(byteBuffer, 0, data.Scan0, byteBuffer.Length);

    OutputImage.UnlockBits(data);         
}

The output image I get is the following:

output image

It is quite clear that I am losing a pixel per row, but i cannot understand why:

  1. I have carefully controlled all the parameters: OutputImageCols, OutputImageRows and the byte[] byteBuffer length and content even writing known values as way to test.
  2. The code is nearly identical to other code posted in StackOverflow and elsewhere.

Someone maybe could help to identify where the problem is?

  • 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-11T10:12:46+00:00Added an answer on June 11, 2026 at 10:12 am

    The answer is not properly mine, the true answer comes from Hans Passant that gave me the hint triggering the solution (thanks for that, Hans). I close my answer just providing a simple detail, just in case someone is interested: instead of working on a buffer and then copying it by means of the Marshal.Copy, dealing with the 4-byte alignement issue on a different buffer and using more memory than the strictly required one, I simply used LockBits to work directly on the image data, using a data structure already setup for the operation. This was the alternative I was thinking of in my question edited to Hans; I am aware that my question unfortunately has been not clearly stated. Anyway, now all works fine.

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

Sidebar

Related Questions

I am trying to understand Adaboost algorithm but i have some troubles. After reading
okay i have been trying to understand this for hours i am learning VB
i have been trying to understand the pack function. I'm having trouble on this
Just trying to understand that - I have never used it before. How is
I am trying understand ViewModels deeper and I have read many articles and blogs
I have been trying to understand the way ActionScript's events are implemented, but I'm
I am trying to understand how memory management goes on low level and have
I am now trying to understand some code and I have found a pattern,
I am trying to understand TDD more and all the examples I have seen
I am trying to understand the difference between 2 methods that have the same

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.