2012-03-14 84 views
0

我认为对于使用C++中的位图的人来说,这一定是一个简单的问题。我有我在C#中的工作代码 - 如何在C++中做一些simillar?感谢您的代码(帮助):-))将C#中的位图转换为C++

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; 

} 

回答

1

原始翻译为C++/CLI,我没有运行示例,所以它可能包含一些错字。无论如何,有不同的方法可以在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; 

} 
0

C++不包含任何参考图像或处理后图像做的非常相似的循环。许多库都可用于此目的,您对这些数据进行操作的方式可能各不相同。

在最基本的层面上,图像由一堆字节组成。如果您只能将数据(即不包含标题或其他元数据)提取到unsigned char[](或给定图像格式的某种其他适当类型),则可以像在C#示例中一样遍历每个像素。