コード例 #1
0
        /// <summary>
        /// Loads a bitmap into a texture.
        /// </summary>
        public Texture(System.Drawing.Bitmap bitmap, BindFlags flags) : this()
        {
            LoadBitmap(bitmap, flags);
            LoadArraySlices(flags);

            IsLoaded = true;
        }
コード例 #2
0
 public void DrawImage(System.Drawing.Bitmap bitmap, System.Drawing.Rectangle rectangle)
 {
     _renderTarget2D.DrawBitmap(
         bitmap: _bitmapHelper.LoadFromFile(_renderTarget2D, bitmap),
         destinationRectangle: _rectangleConverter.Convert(rectangle),
         opacity: 1.0f,
         interpolationMode: BitmapInterpolationMode.Linear);
 }
コード例 #3
0
        public static void RiverBordersWriteImage(List <List <TileForGeneration> > riverBorders, float[][] heights, int resolution, string path)
        {
            var img = new System.Drawing.Bitmap(resolution, resolution, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            for (int y = 0; y < resolution; y++)
            {
                for (int x = 0; x < resolution; x++)
                {
                    var colorRGB = (float)heights[x][y];

                    if (heights[x][y] > 1)
                    {
                        colorRGB = 1;
                    }

                    if (heights[x][y] <= TileNoiseInterpreter.SeaLevelThreshold)
                    {
                        colorRGB = 0;
                    }

                    var col = new Color(colorRGB, colorRGB, colorRGB, 1f);
                    img.SetPixel(x, resolution - 1 - y, col.ToColor());
                }
            }

            var random = new InternalRandom();

            foreach (var riverBorder in riverBorders)
            {
                var color = new Color(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255), 1f);
                foreach (var tileForGeneration in riverBorder)
                {
                    img.SetPixel(tileForGeneration.x, resolution - 1 - tileForGeneration.y, color.ToColor());
                }
            }

            img.Save(path);


            // Texture2D tex = new Texture2D(NamelessGame.DebugDevice, resolution, resolution, false, SurfaceFormat.Color);
            //tex.SetData(arrBytes);



            //using (var str = File.OpenWrite(path))
            //{
            //    tex.SaveAsPng(str, resolution, resolution);
            //}
            //tex.Dispose();
        }
コード例 #4
0
        public static void WaterWriteImage(bool[][] data, float[][] heights, int resolution, string path, Color waterColor)
        {
            var img = new System.Drawing.Bitmap(resolution, resolution, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            for (int x = 0; x < resolution; x++)
            {
                for (int y = 0; y < resolution; y++)
                {
                    var colorRGB = (float)heights[x][y];

                    if (heights[x][y] > 1)
                    {
                        colorRGB = 1;
                    }

                    if (heights[x][y] <= TileNoiseInterpreter.SeaLevelThreshold)
                    {
                        colorRGB = 0;
                    }

                    var col = new Color(colorRGB, colorRGB, colorRGB, 1f);

                    if (data[x][y])
                    {
                        img.SetPixel(x, resolution - 1 - y, waterColor.ToColor());
                    }
                    else
                    {
                        img.SetPixel(x, resolution - 1 - y, col.ToColor());
                    }
                }
            }
            img.Save(path);


            // Texture2D tex = new Texture2D(NamelessGame.DebugDevice, resolution, resolution, false, SurfaceFormat.Color);
            //tex.SetData(arrBytes);



            //using (var str = File.OpenWrite(path))
            //{
            //    tex.SaveAsPng(str, resolution, resolution);
            //}
            //tex.Dispose();
        }
コード例 #5
0
            public SharpDX.Direct2D1.Bitmap LoadFromFile(RenderTarget renderTarget, System.Drawing.Bitmap bitmap)
            {
                SharpDX.Direct2D1.Bitmap match;

                if (_cache.TryGetValue(bitmap, out match))
                {
                    return(match);
                }

                var sourceArea       = new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height);
                var bitmapProperties = new BitmapProperties(new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied));
                var size             = new Size2(bitmap.Width, bitmap.Height);

                // Transform pixels from BGRA to RGBA
                int stride = bitmap.Width * sizeof(int);

                using (var tempStream = new DataStream(bitmap.Height * stride, true, true))
                {
                    // Lock System.Drawing.Bitmap
                    var bitmapData = bitmap.LockBits(sourceArea, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

                    // Convert all pixels
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        int offset = bitmapData.Stride * y;
                        for (int x = 0; x < bitmap.Width; x++)
                        {
                            // Not optimized
                            byte B    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                            byte G    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                            byte R    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                            byte A    = Marshal.ReadByte(bitmapData.Scan0, offset++);
                            int  rgba = R | (G << 8) | (B << 16) | (A << 24);
                            tempStream.Write(rgba);
                        }
                    }
                    bitmap.UnlockBits(bitmapData);
                    tempStream.Position = 0;

                    match = new SharpDX.Direct2D1.Bitmap(renderTarget, size, tempStream, stride, bitmapProperties);

                    _cache.TryAdd(bitmap, match);
                }
                return(match);
            }
コード例 #6
0
        private void LoadBitmap(System.Drawing.Bitmap bitmap, BindFlags flags)
        {
            var texDesc = new Texture2DDescription
            {
                Width             = bitmap.Width,
                Height            = bitmap.Height,
                ArraySize         = 1,
                BindFlags         = flags,
                Usage             = ResourceUsage.Immutable,
                CpuAccessFlags    = CpuAccessFlags.None,
                Format            = Format.R8G8B8A8_UNorm,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0)
            };

            var rect = DirectXUtil.LoadBitmapData(bitmap);

            NativeTexture = new Texture2D(Renderer.Device, texDesc, rect);
        }