简体   繁体   English

将C#中的位图转换为C ++

[英]Bitmap in C# into C++

I think this must be an easy question for somebody who uses bitmap in C++. 我认为对于在C ++中使用位图的人来说,这肯定是一个简单的问题。 I have my a working code in C# - how to do something simillar in C++ ?? 我在C#中有一个工作代码-如何在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;

}

Raw translation to C++/CLI, I didn't run the example so it may contains some typo. 原始翻译为C ++ / CLI,我没有运行该示例,因此它可能包含一些错字。 Anyway there are different ways to get the same result in C++ (because you can use the standard CRT API). 无论如何,有多种方法可以在C ++中获得相同的结果(因为您可以使用标准的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;

}

您可以使用Easy BMP库执行非常相似的循环

C++ does contains nothing in reference to images or processing images. C ++不包含任何有关图像或处理图像的内容。 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 (ie, 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. 如果您可以仅将数据(即不是标头或其他元数据)提取到unsigned char[] (或给定图像格式的某种其他适当类型)中,则可以像在C#中一样对每个像素进行迭代。例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM