您的位置:首页 > 其它

IrrlichtLime中Image对象与.NET中Image对象相互转换

2010-12-05 02:52 387 查看
    IrrlichtLime是Irrlicht Engine的.Net版本。Irrlicht Engine是一个高性能实时的三维引擎,可以跨平台,并可以使用Drectx8、Drectx9和OpenGL等进行渲染。

 

    使用IrrlichtLime进行开发时,有时需要将IrrlichtLime.Video.Image与System.Drawing.Image进行相互转换。

   1.System.Drawing.Image 转换为 IrrlichtLime.Video.Image

 

     (1) 首先将System.Drawing.Image转换为字节数组

          System.Drawing.Image sourceImage;// System.Drawing.Image 对象,使用时,需先赋值

          System.IO.MemoryStream sourceImageStream = new System.IO.MemoryStream();

          sourceImage.Save(sourceImageStream , source.RawFormat);// 将图像保存到内存流中

          byte[]  sourceImageBytes=sourceImageStream .ToArray();  // 将图像内存流转换为字节数组

     (2) 使用字节数组创建irrlichtLime.Video.Image对象

          IrrlichtLime.IrrlichtDevice device // Irrlicht Engine渲染设备,需先创建该对象

          IrrlichtLime.IO.ReadFile irrFile = device.FileSystem.CreateMemoryReadFile("temp", sourceImageBytes);//使用字节创建内存读文件

 

          // 以下使用刚刚创建的内存读文件,创建irrlichtLime.Video.Image对象

          IrrlichtLime.Video.Image irrImage = device.VideoDriver.CreateImage(irrFile);

          irrFileirrFile.drop();

 

  2. IrrlichtLime.Video.Image转换为System.Drawing.Image 
    IrrlichtLime.Video.Image对象提供了  CopyToScaling(byte[], int, int, IrrlichtLime.Video.ColorFormat)方法,可以转换为字节数组。但是使用该方法,不知道是不是.Net版本的问题,始终无法得到正确的字节数组。因此,这里使用该对象的GetPixel方法,依次获取各个像素点的颜色,来实现转换。

 

     (1) 创建一个等大的System.Drawing.Bitmap

          

          int width = sourceImage.Dimension.Width; int height = sourceImage.Dimension.Height;

            // 像素点格式,使用两种标准:24bppRgb 或 32bppArgb
            System.Drawing.Imaging.PixelFormat pixelformat = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
            int pixelSize = 3;
            if (sourceImage.ColorFormat.ToString().Contains("A")) {
                pixelformat = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
                pixelSize = 4;
            }
            System.Drawing.Bitmap targetImage = new System.Drawing.Bitmap(width, height,pixelformat);

 

    (2) 使用指针操作BitmapData ,设置System.Drawing.Bitmap每个像素点的颜色

 

            System.Drawing.Imaging.BitmapData bitmapData =
                targetImage.LockBits(new System.Drawing.Rectangle(0, 0, width, height),
                 System.Drawing.Imaging.ImageLockMode.ReadWrite, pixelformat);           

            unsafe
            {
                for (int y = 0; y < height; y++)
                {
                    byte* row = (byte*)bitmapData.Scan0 + (y * bitmapData.Stride);
                    for (int x = 0; x < width; x++)
                    {

                        //获取原始图像指定点的颜色值
                        IrrlichtLime.Video.Color color = sourceImage.GetPixel(x, y);

                        // 设置目标图像指定的RGB值,按照B G R (A)  的顺序设置

                        row[x * pixelSize + 0] = (byte)color.Blue;
                        row[x * pixelSize + 1] = (byte)color.Green;
                        row[x * pixelSize + 2] = (byte)color.Red;
                        if (pixelSize == 4) row[x * pixelSize + 3] = (byte)color.Alpha;
                    }
                }
            }

            targetImage.UnlockBits(bitmapData);

 

   若编译无法通过,需要设置项目“ 允许运行不安全代码”。

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息