Image<Bgr, Byte> video = cap.QueryFrame();
Texture2D t = new Texture2D(GraphicsDevice, video.Width, video.Height, false, SurfaceFormat.Color);
t.SetData<byte>(video.Bytes);
ArgumentException が処理されませんでした
渡されたデータのサイズが、このリソースに対して大きすぎるか小さすぎます。
私の推奨する方法は、画像をメモリに「保存」してから、Texture2D.FromStream
関数でロードすることです。
Texture2D t;
using(MemoryStream memStream = new MemoryStream())
{
Image<Bgr, Byte> video = cap.QueryFrame();
cap.save(memStream, ImageFormat.PNG);
t = Texture2D.FromStream(GraphicsDevice, memStream, video.Width, video.Height, 1f)
}
これは NonPremultiplied BlendStates で機能しますが、Premultiplied アルファを使用する場合は、次の関数を使用して Texture2D を実行する必要があります。この関数は単に GPU を使用して、コンテンツ プロセッサと同じようにテクスチャのアルファをすばやく事前乗算します。
static public void PreMultiplyAlpha(this Texture2D texture) {
//Setup a render target to hold our final texture which will have premulitplied color values
var result = new RenderTarget2D(texture.GraphicsDevice, texture.Width, texture.Height);
texture.GraphicsDevice.SetRenderTarget(result);
texture.GraphicsDevice.Clear(Color.Black);
// Using default blending function
// (source × Blend.SourceAlpha) + (destination × Blend.InvSourceAlpha)
// Destination is zero so the reduces to
// (source × Blend.SourceAlpha)
// So this multiplies our color values by the alpha value and draws it to the RenderTarget
var blendColor = new BlendState {
ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue,
AlphaDestinationBlend = Blend.Zero,
ColorDestinationBlend = Blend.Zero,
AlphaSourceBlend = Blend.SourceAlpha,
ColorSourceBlend = Blend.SourceAlpha
};
var spriteBatch = new SpriteBatch(texture.GraphicsDevice);
spriteBatch.Begin(SpriteSortMode.Immediate, blendColor);
spriteBatch.Draw(texture, texture.Bounds, Color.White);
spriteBatch.End();
// Simply copy over the alpha channel
var blendAlpha = new BlendState {
ColorWriteChannels = ColorWriteChannels.Alpha,
AlphaDestinationBlend = Blend.Zero,
ColorDestinationBlend = Blend.Zero,
AlphaSourceBlend = Blend.One,
ColorSourceBlend = Blend.One
};
spriteBatch.Begin(SpriteSortMode.Immediate, blendAlpha);
spriteBatch.Draw(texture, texture.Bounds, Color.White);
spriteBatch.End();
texture.GraphicsDevice.SetRenderTarget(null);
var t = new Color[result.Width * result.Height];
result.GetData(t);
texture.SetData(t);
}
ビデオは画像を(青、緑、赤)形式で保存します。一方、texture2d は追加のアルファ ピクセルも必要とします。
Image<Bgr, Byte> video = cap.QueryFrame();
Image<Bgra, Byte> video2 = video.Convert<Bgra, Byte>();
Texture2D t = new Texture2D(GraphicsDevice, video.Width, video.Height, false, SurfaceFormat.Color);
t.SetData<byte>(video2.Bytes);