C# 使用像素数组初始化BitmapSource
我正在尝试将本机图像数据结构封送到BitmapSource 这是本机结构:C# 使用像素数组初始化BitmapSource,c#,c++-cli,bitmapsource,C#,C++ Cli,Bitmapsource,我正在尝试将本机图像数据结构封送到BitmapSource 这是本机结构: struct Bitmap_8Bit { public: unsigned char* data; int stride; int rows; int cols; int nChannels; //either 1 or 3. in case of three the o
struct Bitmap_8Bit
{
public:
unsigned char* data;
int stride;
int rows;
int cols;
int nChannels; //either 1 or 3. in case of three the order of the channels is BGR
};
这是我的封送功能:
template<>
inline
System::Windows::Media::Imaging::BitmapSource^ marshal_as<System::Windows::Media::Imaging::BitmapSource^, Bitmap_8Bit>(const Bitmap_8Bit& nativeBitmap)
{
System::Windows::Media::PixelFormat format = System::Windows::Media::PixelFormats::Default;
System::Windows::Media::Imaging::BitmapPalette^ palette = nullptr;
if (nativeBitmap.nChannels == 1)
{
format = System::Windows::Media::PixelFormats::Gray8;
palette = System::Windows::Media::Imaging::BitmapPalettes::Gray256;
}
else if (nativeBitmap.nChannels == 3)
{
format = System::Windows::Media::PixelFormats::Bgr24;
palette = System::Windows::Media::Imaging::BitmapPalettes::Halftone256;
}
else
{
throw gcnew System::InvalidOperationException("Unsupported number of channels " + nativeBitmap.nChannels);
}
//copy data
int pixelDataLength = nativeBitmap.rows*nativeBitmap.stride;
System::IntPtr source = System::IntPtr(nativeBitmap.data);
cli::array<unsigned char>^ buffer = gcnew cli::array<unsigned char>(pixelDataLength);
System::Runtime::InteropServices::Marshal::Copy(source, buffer, 0, pixelDataLength);
System::Windows::Media::Imaging::BitmapSource^ managedBitmap =
System::Windows::Media::Imaging::BitmapSource::Create(nativeBitmap.cols, nativeBitmap.rows,
96, 96,
format, palette,
buffer, nativeBitmap.stride);
return managedBitmap;
}
模板
内联
系统::Windows::媒体::图像::位图源^marshal_as(常量位图_8Bit和nativeBitmap)
{
System::Windows::Media::PixelFormat format=System::Windows::Media::PixelFormats::Default;
System::Windows::Media::Imaging::BitmapPalette^palette=nullptr;
如果(nativeBitmap.nChannels==1)
{
format=System::Windows::Media::PixelFormats::Gray8;
palette=System::Windows::Media::Imaging::BitmapPalettes::Gray256;
}
else if(nativeBitmap.nChannels==3)
{
格式=系统::Windows::媒体::像素格式::Bgr24;
调色板=系统::Windows::媒体::图像::位图调色板::半色调256;
}
其他的
{
抛出gcnew System::InvalidOperationException(“不支持的通道数”+nativeBitmap.nChannels);
}
//复制数据
int pixelDataLength=nativeBitmap.rows*nativeBitmap.stride;
System::IntPtr source=System::IntPtr(nativeBitmap.data);
cli::array^buffer=gcnew cli::array(pixelDataLength);
System::Runtime::InteropServices::Marshal::Copy(源、缓冲区、0、pixelDataLength);
系统::Windows::媒体::图像::位图源^managedBitmap=
System::Windows::Media::Imaging::BitmapSource::Create(nativeBitmap.cols、nativeBitmap.rows、,
96, 96,
格式、调色板、,
缓冲区,nativeBitmap.stride);
返回managedBitmap;
}
我正在一个简单的单通道情况下测试我的功能:
BSII::IP::Bitmap_8Bit native;
native.cols = 8;
native.rows = 4;
native.nChannels = 1;
native.stride = 8;
int dataLength = native.stride * native.rows;
native.data = new unsigned char[dataLength];
unsigned char gray = 0;
for (int i = 0; i < native.rows; ++i)
{
for (int j = 0; j < native.cols; ++j)
{
native.data[native.stride*i + j] = gray;
gray += 10;
}
}
System::Windows::Media::Imaging::BitmapSource^ managed = msclr::interop::marshal_as<System::Windows::Media::Imaging::BitmapSource^>(native);
System::IO::FileStream^ stream = gcnew System::IO::FileStream("c:\\workspace\\temp\\managed.bmp", System::IO::FileMode::Create);
System::Windows::Media::Imaging::BmpBitmapEncoder^ encoder = gcnew System::Windows::Media::Imaging::BmpBitmapEncoder();
System::Windows::Media::Imaging::BitmapFrame^ bitmapFrame = System::Windows::Media::Imaging::BitmapFrame::Create(managed);
encoder->Frames->Add(bitmapFrame);
encoder->Save(stream);
stream->Close();
BSII::IP::bitmap8bitnative;
native.cols=8;
native.rows=4;
native.nChannels=1;
native.stride=8;
int dataLength=native.stride*native.rows;
native.data=新的无符号字符[dataLength];
无符号字符灰度=0;
对于(int i=0;i帧->添加(位图帧);
编码器->保存(流);
流->关闭();
这不管用。当我调试它时,我看到缓冲区数组中有正确的值,但是当我将BitmapSource保存到一个图像时,它看起来不像它应该的样子
我也试着用nullptr来代替灰度256或半色调256,但也没用
我想我在使用BitmapSource时遗漏了一些东西。有什么想法吗
谢谢,
Dina使用
PngBitmapEncoder
代替BmpBitmapEncoder
BmpBitmapEncoder
使图像变形。
使用PngBitmapEncoder
示例可以正常工作
此外,出于性能原因,请尝试使用:
BitmapSource.Create(Int32, Int32, Double, Double, PixelFormat, BitmapPalette,
IntPtr, Int32, Int32)
而不是
BitmapSource.Create(Int32, Int32, Double, Double, PixelFormat, BitmapPalette,
Array, Int32)