gpt4 book ai didi

c++ - C# 中的位图转换为 C++

转载 作者:行者123 更新时间:2023-11-28 08:08:25 25 4
gpt4 key购买 nike

我认为对于在 C++ 中使用位图的人来说,这一定是一个简单的问题。我在 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;

}

最佳答案

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;

}

关于c++ - C# 中的位图转换为 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9709742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com