调用IWICComponentFactory.CreateBitmapFromMemory时出现WINCODEC_ERR_Win32; Win32错误0x88982F94
调用IWICComponentFactory.CreateBitmapFromMemory并将指针传递给32bppArgb GDI+位图的Scan0时,我遇到以下错误调用IWICComponentFactory.CreateBitmapFromMemory时出现WINCODEC_ERR_Win32; Win32错误0x88982F94,com,interop,gdi+,gdi,wic,Com,Interop,Gdi+,Gdi,Wic,调用IWICComponentFactory.CreateBitmapFromMemory并将指针传递给32bppArgb GDI+位图的Scan0时,我遇到以下错误 WINCODEC_ERR_WIN32ERROR 0x88982F94 Windows Codecs received an error from the Win32 system. IWICComponentFactory接口decl: new IWICBitmap CreateBitmapFromMemory(
WINCODEC_ERR_WIN32ERROR
0x88982F94
Windows Codecs received an error from the Win32 system.
IWICComponentFactory接口decl:
new IWICBitmap CreateBitmapFromMemory(
uint uiWidth,
uint uiHeight,
[MarshalAs(UnmanagedType.LPStruct)]
Guid pixelFormat,
uint cbStride,
uint cbBufferSize,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 5)]
byte[] pbBuffer
);
new IWICBitmap CreateBitmapFromMemory(
uint uiWidth,
uint uiHeight,
[MarshalAs(UnmanagedType.LPStruct)]
Guid pixelFormat,
uint cbStride,
uint cbBufferSize,
IntPtr pbBuffer
);
完整代码:
public static IWICBitmap ToWic(IWICComponentFactory factory, Bitmap bit) {
BitmapData bd = bit.LockBits(new Rectangle(0, 0, bit.Width, bit.Height),
ImageLockMode.ReadOnly, bit.PixelFormat);
IWICBitmap b = null;
try {
//Create WIC bitmap directly from unmanaged memory
b = factory.CreateBitmapFromMemory((uint)bit.Width, (uint)bit.Height,
ConversionUtils.FromPixelFormat(bit.PixelFormat), (uint)bd.Stride,
(uint)(bd.Stride * bd.Height), bd.Scan0);
return b;
} finally {
bit.UnlockBits(bd);
}
}
宽度、高度、缓冲区大小、格式GUID和扫描大小似乎都正确。我不明白为什么会发生这种情况错误代码或消息没有谷歌搜索结果这并不能解释为什么原始代码不起作用,但这是一个解决办法。使用iwicmagingfactory\u CreateBitmapFromMemory\u代理,一切正常。但是为什么原著没有像它应该的那样?为什么34;代理方法具有几乎相同的签名"?
[DllImport("WindowsCodecs.dll", EntryPoint = "IWICImagingFactory_CreateBitmapFromMemory_Proxy")]
internal static extern int CreateBitmapFromMemory(IWICComponentFactory factory, uint width, uint height, ref Guid pixelFormatGuid, uint stride, uint cbBufferSize, IntPtr pvPixels, out IWICBitmap ppIBitmap);
public static IWICBitmap ToWic(IWICComponentFactory factory, Bitmap bit) {
Guid pixelFormat = ConversionUtils.FromPixelFormat(bit.PixelFormat);
if (pixelFormat == Guid.Empty) throw new NotSupportedException("PixelFormat " + bit.PixelFormat.ToString() + " not supported.");
BitmapData bd = bit.LockBits(new Rectangle(0, 0, bit.Width, bit.Height), ImageLockMode.ReadOnly, bit.PixelFormat);
IWICBitmap b = null;
try {
//Create WIC bitmap directly from unmanaged memory
long result = CreateBitmapFromMemory(factory, (uint)bit.Width,
(uint)bit.Height, ref pixelFormat, (uint)bd.Stride,
(uint)(bd.Stride * bd.Height), bd.Scan0, out b);
if (result == 0x80070057) throw new ArgumentException();
if (result < 0) throw new Exception("HRESULT " + result);
return b;
} finally {
bit.UnlockBits(bd);
}
}
供参考,及。两者都使用[IN]字节*pbBuffer 通过使用代理函数,可以避免对IWICxxx接口使用损坏的COM声明。有证据表明你在另一个问题上错了。错误的声明会使代码调用错误的方法。你得到的错误代码肯定是假的。你能帮我理解COM声明的错误吗?我用IntPtr包装[out]BYTE*pbBuffer,而不是[out][MarshalAsUnmanagedType.LPArray,SizeParamIndex=2]。代理函数参数也是[out]BYTE*pbBuffer,但在那里工作正常。当两个调用使用相同的签名时,我不明白为什么它应该在一个实例中工作而不在另一个实例中工作。这个问题是关于将[IN]BYTE*pbBuffer更改为IntPtr而不是LPArray,但在其他情况下是相同的。