求助大神这是什么歌,有关byte转Texture2D的问题,求大神指点

21:18 提问
关于byte赋值问题,求大神解答
byte b1= 1;
byte b2 =2;
byte b3 =b1+b2;
这种编译为什么会报错
按赞数排序
可能回导致溢出吧。。所以编译不通过,
这个问题上回你不问题过吗?还回复过
在执行byte b3 =b1+b2;这个语句时,b1和b2的值就会自动转成int类型来运算,但是int类型不能在赋给byte类型的b3,数据类型精度有可能会丢失,就会编译出错。
其他相似问题5227人阅读
最近学习emgucv,我在写读摄像头的程序然后需要bitmap 转texture2D 其实可以直接使用setdata&bytes&的方法赋值,感觉帧率不是一般的低,是无法忍受的。
也试了多种方法,基本上都是无法忍受的。可能有人会用得到,将搜集的列下来。
这是一些方法的搜集。
Texture2D&toBitmap 的方法
public static Bitmap FastTextureToBitmap(Texture2D texture)
// Setup pointer back to bitmap
Bitmap newBitmap = new Bitmap(texture.Width, texture.Height);
// Get color data from the texture
Microsoft.Xna.Framework.Graphics.Color[ textureColors = GetColorDataFromTexture(texture);
System.Drawing.Imaging.BitmapData bmpData = newBitmap.LockBits(new System.Drawing.Rectangle(0, 0, newBitmap.Width, newBitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
// Loop through pixels and set values
byte* bmpPointer = (byte*)bmpData.Scan0;
for (int y = 0; y & texture.H y++)
for (int x = 0; x & texture.W x++)
bmpPointer[0] = textureColors[x + y * texture.Width].B;
bmpPointer[1] = textureColors[x + y * texture.Width].G;
bmpPointer[2] = textureColors[x + y * texture.Width].R;
bmpPointer[3] = textureColors[x + y * texture.Width].A;
bmpPointer += 4;
bmpPointer += bmpData.Stride - (bmpData.Width * 4);
textureColors =
newBitmap.UnlockBits(bmpData);
return newB
bitmap to texture2D 的方法:
private Texture2D GetTexture(GraphicsDevice dev, System.Drawing.Bitmap bmp)
int[] imgData = new int[bmp.Width * bmp.Height];
Texture2D texture = new Texture2D(dev, bmp.Width, bmp.Height);
// lock bitmap
System.Drawing.Imaging.BitmapData origdata =
bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
uint* byteData = (uint*)origdata.Scan0;
// Switch bgra -& rgba
for (int i = 0; i & imgData.L i++)
byteData[i] = (byteData[i] & 0x000000ff) && 16 | (byteData[i] & 0x0000FF00) | (byteData[i] & 0x00FF0000) && 16 | (byteData[i] & 0xFF000000);
// copy data
System.Runtime.InteropServices.Marshal.Copy(origdata.Scan0, imgData, 0, bmp.Width * bmp.Height);
byteData =
// unlock bitmap
bmp.UnlockBits(origdata);
texture.SetData(imgData);
貌似对于赋值这一步C#的效率好像不高,帧率一般都会降的很低。我还没有想到更好的办法,表示无奈中。。。&
实际上还有一个方法可以直接使用
BitmapSourceExtensions.CopyTo Method (BitmapSource, Texture2D)
此方法需要使用&&&我倒还没有尝试过。估计应该是挺好用的。
例子可以参见/en-us/library/gg712857(v=vs.96).aspx&
这种方法试过,但是不管用,自己也是新手,搞不定了。。。就扔了,有人搞懂的话,可以回个帖。~
值得一提的是对于BITMAP的格式,要值得仔细查看一下,不要直接套用上面的方法
这个是另外一种方法,试了一下貌似帧率有所提高,但依然还是不如窗体直接用的那种性能好。都准备放弃XNA了。。。
1publicstatic Texture2D BitmapToTexture2D(
2 GraphicsDevice GraphicsDevice,
3 System.Drawing.Bitmap image)
5 // Buffer size is size of color array multiplied by 4 because
6// each pixel has four color bytes
7int bufferSize = image.Height * image.Width * 4;
9 // Create new memory stream and save image to stream so
10// we don't have to save and read file
11 System.IO.MemoryStream memoryStream =
12 new System.IO.MemoryStream(bufferSize);
13 image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
14 15 // Creates a texture from IO.Stream - our memory stream
16 Texture2D texture = Texture2D.FromStream(
17 GraphicsDevice, memoryStream);
我依然觉得这是个性能瓶颈。。。又搜到了国外某人发的一个类
using Microsoft.Xna.F
using Microsoft.Xna.Framework.G
using System.D
using System.Drawing.I
using System.Runtime.InteropS
namespace MrXNAHelper
public class ImageToTexture
byte[] bmpB
int width,
public ImageToTexture(Game game, int width, int height)
this.width =
this.height =
this.game =
GenerateBitmap(game, width, height);
public ImageToTexture(Game game)
this.game =
private void GenerateBitmap(Game game, int width, int height)
background = new Texture2D(game.GraphicsDevice, width, height);
public Texture2D ConvertBitmapToTexture(Bitmap b)
game.GraphicsDevice.Textures[0] =
if (background == null ||
b.Width != background.Width ||
b.Height != background.Height)
width = b.W
height = b.H
GenerateBitmap(game, width, height);
BitmapData bData = b.LockBits(new System.Drawing.Rectangle(new System.Drawing.Point(), b.Size),
ImageLockMode.ReadOnly,
PixelFormat.Format32bppRgb);
// number of bytes in the bitmap
int byteCount = bData.Stride * b.H
if (bmpBytes == null ||
bmpBytes.Length != byteCount)
bmpBytes = new byte[byteCount];
// Copy the locked bytes from memory
Marshal.Copy(bData.Scan0, bmpBytes, 0, byteCount);
// don't forget to unlock the bitmap!!
b.UnlockBits(bData);
background.SetData(bmpBytes);
我终于会插代码了,,,,囧
现在知道降低XNA帧率或者自己写个定时器,总之不要使用60Hz的话,好像帧率也会有很大的提高。
byte[] bgrData = nextFrame.B
for (int i = 0; i & colorData.L i++)
colorData[i] = new Color(bgrData[3 * i + 2], bgrData[3 * i + 1], bgrData[3 * i]);
惊人也没有用到什么内存指针之类的,但是就做到了。使用2张贴图交错的加载方法。膜拜老外啊。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:312515次
积分:3381
积分:3381
排名:第8188名
原创:51篇
转载:48篇
评论:44条
(1)(2)(1)(1)(1)(1)(1)(3)(1)(1)(1)(3)(1)(3)(7)(7)(4)(2)(3)(1)(7)(2)(4)(1)(1)(3)(6)(5)(6)(3)(5)(3)(2)(3)(1)(3)(1)}

我要回帖

更多关于 求助大神这是什么歌 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信