0

I think this must be an easy question for somebody who uses bitmap in C++. I have my a working code in C# - how to do something simillar in C++ ?? Thanks for your codes (help) :-))

public Bitmap Visualize ()
{


  PixelFormat fmt = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
  Bitmap result = new Bitmap( Width, Height, fmt );

  BitmapData data = result.LockBits( new Rectangle( 0, 0, Width, Height ), ImageLockMode.ReadOnly, fmt );
  unsafe
  {
    byte* ptr;

    for ( int y = 0; y < Height; y++ )
    {
      ptr = (byte*)data.Scan0 + y * data.Stride;

      for ( int x = 0; x < Width; x++ )
      {
          float num = 0.44;
          byte c = (byte)(255.0f * num);
          ptr[0] = ptr[1] = ptr[2] = c;


          ptr += 3;
      }
    }
  }
  result.UnlockBits( data );

  return result;

}

3 Answers 3

1

Raw translation to C++/CLI, I didn't run the example so it may contains some typo. Anyway there are different ways to get the same result in C++ (because you can use the standard CRT API).

Bitmap^ Visualize ()
{
  PixelFormat fmt = System::Drawing::Imaging::PixelFormat::Format24bppRgb;
  Bitmap^ result = gcnew Bitmap( Width, Height, fmt );

  BitmapData^ data = result->LockBits( Rectangle( 0, 0, Width, Height ), ImageLockMode::ReadOnly, fmt );
  for ( int y = 0; y < Height; y++ )
  {
    unsigned char* ptr = reinterpret_cast<unsigned char*>((data->Scan0 + y * data->Stride).ToPointer());

    for ( int x = 0; x < Width; x++ )
    {
        float num = 0.44f;
        unsigned char c = static_cast<unsigned char>(255.0f * num);
        ptr[0] = ptr[1] = ptr[2] = c;

        ptr += 3;
    }
  }

  result->UnlockBits( data );

  return result;

}
Sign up to request clarification or add additional context in comments.

Comments

0

You can do very similar loops using the Easy BMP library

Comments

0

C++ does contains nothing in reference to images or processing images. Many libraries are available for this, and the way in which you operate on the data may be different for each.

At it's most basic level, an image consists of a bunch of bytes. If you can extract just the data (i.e., not headers or other metadata) into a unsigned char[] (or some other appropriate type given the format of your image) then you can iterate through each pixel much like you have done in your C# example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.