gpt4 book ai didi

c# - 如何将emgu图像转换为XNA Texture2D?

转载 作者:行者123 更新时间:2023-12-02 17:52:19 26 4
gpt4 key购买 nike

Image<Bgr, Byte> video = cap.QueryFrame();
Texture2D t = new Texture2D(GraphicsDevice, video.Width, video.Height, false, SurfaceFormat.Color);
t.SetData<byte>(video.Bytes);

ArgumentException was unhandled

The size of the data passed in is too large or too small for this resource.

最佳答案

我的方法是将图像“保存”到内存中,然后使用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 alpha,则应通过以下功能运行Texture2D。该功能仅使用GPU即可快速将纹理的Alpha乘以内容处理器所需的Alpha。
    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);
}

关于c# - 如何将emgu图像转换为XNA Texture2D?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17824893/

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