Пример #1
0
        private void setIconImage(string path, PictureBox pBx)
        {
            try
            {
                PresentParameters pp = new PresentParameters();
                pp.Windowed   = true;
                pp.SwapEffect = SwapEffect.Copy;
                Device device = new Device(0, DeviceType.Hardware, this.tgaIconPicBx, CreateFlags.HardwareVertexProcessing, pp);
                Microsoft.DirectX.Direct3D.Texture tx = TextureLoader.FromFile(device, path);
                Microsoft.DirectX.GraphicsStream   gs = TextureLoader.SaveToStream(ImageFileFormat.Png, tx);

                Bitmap btmap = new Bitmap(gs);

                pBx.Image = btmap;

                gs.Dispose();
                tx.Dispose();
                device.Dispose();
            }
            catch (Exception ex)
            { }
        }
Пример #2
0
        private void setIconImage(string path)
        {
            try
            {
                PresentParameters pp = new PresentParameters();
                pp.Windowed   = true;
                pp.SwapEffect = SwapEffect.Copy;
                Device device = new Device(0, DeviceType.Hardware, this.rgb_picBx, CreateFlags.HardwareVertexProcessing, pp);
                Microsoft.DirectX.Direct3D.Texture tx = TextureLoader.FromFile(device, path);
                Microsoft.DirectX.GraphicsStream   gs = TextureLoader.SaveToStream(ImageFileFormat.Dib, tx);

                Bitmap RGB_bitmap   = new Bitmap(gs);
                Bitmap alpha_bitmap = new Bitmap(gs);

                rgb_picBx.Image = RGB_bitmap;

                //alpha_bitmap.MakeTransparent();
                gs.Seek(0, 0);

                System.Drawing.Imaging.BitmapData bmpData = alpha_bitmap.LockBits(new Rectangle(0, 0, alpha_bitmap.Width, alpha_bitmap.Height),
                                                                                  System.Drawing.Imaging.ImageLockMode.ReadWrite,
                                                                                  alpha_bitmap.PixelFormat);

                // Get the address of the first line.
                IntPtr ptr = bmpData.Scan0;

                // Declare an array to hold the bytes of the bitmap.
                int bytes = Math.Abs(bmpData.Stride) * alpha_bitmap.Height;

                int gsLen = (int)gs.Length;

                byte[] rgbaValues = new byte[bytes];

                byte[] gsBytes = new byte[gsLen];

                gs.Read(gsBytes, 0, gsLen);

                int gsLastI = gsLen - 1;

                // Copy the RGB values into the array.
                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbaValues, 0, bytes);
                int i = 0;
                for (int y = 0; y < bmpData.Height; y++)
                {
                    for (int x = 0; x < bmpData.Width; x++)
                    {
                        byte g = gsBytes[gsLastI - i++],
                             b = gsBytes[gsLastI - i++],
                             a = gsBytes[gsLastI - i++],
                             r = gsBytes[gsLastI - i++];

                        Color pClr = Color.FromArgb(255, a, a, a);

                        System.Runtime.InteropServices.Marshal.WriteInt32(bmpData.Scan0, (bmpData.Stride * y) + (4 * x), pClr.ToArgb());
                    }
                }

                alpha_bitmap.UnlockBits(bmpData);

                alpha_bitmap.RotateFlip(RotateFlipType.Rotate180FlipY);

                alpha_picBx.Image = alpha_bitmap;
                gs.Dispose();
                tx.Dispose();
                device.Dispose();
            }
            catch (Exception ex)
            { }
        }
Пример #3
0
        private Bitmap getImage(string fileName)
        {
            Microsoft.DirectX.GraphicsStream   gs = null;
            Microsoft.DirectX.Direct3D.Texture tx = null;
            try
            {
                this.invertAlpha.Enabled = false;

                System.IO.MemoryStream mSr = getMemStream(fileName);

                mSr.Seek(0, 0);

                tx = TextureLoader.FromStream(device, mSr);

                mSr.Seek(0, 0);

                ImageInformation imInfo = TextureLoader.ImageInformationFromStream(mSr);

                mSr.Close();

                fillTxtBx(imInfo);

                gs = TextureLoader.SaveToStream(ImageFileFormat.Dib, tx);

                Bitmap btmap = new Bitmap(gs);

                if (hasAlpha)
                {
                    aChk.Enabled        = true;
                    invertAlpha.Enabled = true;
                    fixBitmapAlpha(gs, btmap, imInfo);
                }

                return(btmap);
            }
            catch (Exception e)
            {
                if (gs != null)
                {
                    gs.Close();

                    gs.Dispose();
                }
                if (tx != null)
                {
                    tx.Dispose();
                }

                if (pp != null)
                {
                    pp = null;

                    pp = new PresentParameters();

                    pp.Windowed = true;

                    pp.SwapEffect = SwapEffect.Copy;
                }

                if (device != null)
                {
                    device.Dispose();

                    device = null;

                    device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, pp);
                }

                return(null);
            }
        }
Пример #4
0
        ///<summary>
        ///    @copydoc HardwarePixelBuffer.BlitToMemory
        ///</summary>
        public override void BlitToMemory(BasicBox srcBox, PixelBox dst)
        {
            // Decide on pixel format of temp surface
            PixelFormat tmpFormat = format;

            if (D3DHelper.ConvertEnum(dst.Format) == D3D.Format.Unknown)
            {
                tmpFormat = dst.Format;
            }
            if (surface != null)
            {
                Debug.Assert(srcBox.Depth == 1 && dst.Depth == 1);
                // Create temp texture
                D3D.Texture tmp =
                    new D3D.Texture(device, dst.Width, dst.Height,
                                    1, // 1 mip level ie topmost, generate no mipmaps
                                    0, D3DHelper.ConvertEnum(tmpFormat),
                                    Pool.Scratch);
                D3D.Surface subSurface = tmp.GetSurfaceLevel(0);
                // Copy texture to this temp surface
                Rectangle destRect, srcRect;
                srcRect  = ToD3DRectangle(srcBox);
                destRect = ToD3DRectangleExtent(dst);

                SurfaceLoader.FromSurface(subSurface, destRect, surface, srcRect, Filter.None, 0);

                // Lock temp surface and copy it to memory
                int            pitch; // Filled in by D3D
                GraphicsStream data = subSurface.LockRectangle(D3D.LockFlags.ReadOnly, out pitch);
                // Copy it
                PixelBox locked = new PixelBox(dst.Width, dst.Height, dst.Depth, tmpFormat);
                FromD3DLock(locked, pitch, data);
                PixelUtil.BulkPixelConversion(locked, dst);
                subSurface.UnlockRectangle();
                // Release temporary surface and texture
                subSurface.Dispose();
                tmp.Dispose();
            }
            else
            {
                // Create temp texture
                D3D.VolumeTexture tmp =
                    new D3D.VolumeTexture(device, dst.Width, dst.Height, dst.Depth,
                                          0, D3D.Usage.None,
                                          D3DHelper.ConvertEnum(tmpFormat),
                                          Pool.Scratch);
                D3D.Volume subVolume = tmp.GetVolumeLevel(0);
                // Volume
                D3D.Box ddestBox = ToD3DBoxExtent(dst);
                D3D.Box dsrcBox  = ToD3DBox(srcBox);

                VolumeLoader.FromVolume(subVolume, ddestBox, volume, dsrcBox, Filter.None, 0);
                // Lock temp surface and copy it to memory
                D3D.LockedBox  lbox;                // Filled in by D3D
                GraphicsStream data = subVolume.LockBox(LockFlags.ReadOnly, out lbox);
                // Copy it
                PixelBox locked = new PixelBox(dst.Width, dst.Height, dst.Depth, tmpFormat);
                FromD3DLock(locked, lbox, data);
                PixelUtil.BulkPixelConversion(locked, dst);
                subVolume.UnlockBox();
                // Release temporary surface and texture
                subVolume.Dispose();
                tmp.Dispose();
            }
        }
Пример #5
0
        //Loads the texture into a sprite
        private void SetTextureToScreen()
        {
            // Load texture from other sprite's texture
            if (this.ParentSprite == null)
            {
                uint f                  = 0; //Frame of animation
                uint x                  = 0; //Frame x reltive to frame
                uint y                  = 0; //Frame y reletive to frame
                uint SourceXStart       = 0; //Frame's starting x reletive to sprite sheet
                uint SourceYStart       = 0; //Frame's starting y reletive to sprite sheet
                uint DestinationY       = 0;
                uint DestinationX       = 0;
                uint AnimsPerSheetWidth = (uint)(this.SheetWidth / this.Width);

                //Load the whole texture till we can chop it up
                Direct3D.Texture SpriteSheet = Direct3D.TextureLoader.FromFile
                                               (
                    this.ParentDevice, this.Image, 0, 0, 0, Direct3D.Usage.None,
                    Direct3D.Format.A8B8G8R8, Direct3D.Pool.Managed,
                    Direct3D.Filter.Linear, Direct3D.Filter.Linear, this.MaskColor.ToArgb()
                                               );

                //Chop this sprite sheet up into frames
                //Create a blank bitmap and use it as a template for creating a texture
                System.Drawing.Bitmap SingleFrameBackground = new System.Drawing.Bitmap(this.Width, this.Height);

                uint[,] DestinationPixels;
                uint[,] SourcePixels = (uint[, ])SpriteSheet.LockRectangle(typeof(uint), 0, new Rectangle(0, 0, this.SheetHeight, this.SheetWidth), Direct3D.LockFlags.None, this.SheetHeight, this.SheetWidth);
                this.textures        = new Direct3D.Texture[this.AnimCount];

                //For each frame of animation
                for (f = 0; f < this.AnimCount; f++)
                {
                    //Get this frame's starting y
                    if (f >= AnimsPerSheetWidth)
                    {
                        SourceYStart = (uint)(f / AnimsPerSheetWidth);
                    }

                    //Get this frame's starting x
                    SourceXStart = (uint)(f - (SourceYStart * AnimsPerSheetWidth));

                    SourceYStart *= (uint)this.Height;
                    SourceXStart *= (uint)this.Width;

                    DestinationY = 0;
                    DestinationX = 0;

                    //Create the texture's background
                    this.textures[f]  = new Direct3D.Texture(this.ParentDevice, SingleFrameBackground, Direct3D.Usage.None, Direct3D.Pool.Managed);
                    DestinationPixels = (uint[, ]) this.textures[f].LockRectangle(typeof(uint), 0, new Rectangle(0, 0, this.Height, this.Width), Direct3D.LockFlags.None, this.Height, this.Width);

                    //For this frame's x
                    for (x = SourceXStart; x < SourceXStart + this.Width; x++)
                    {
                        //For this frame's y
                        for (y = SourceYStart; y < SourceYStart + this.Height; y++)
                        {
                            DestinationPixels[DestinationY, DestinationX] = SourcePixels[y, x];
                            DestinationY++;
                        }
                        DestinationX++;
                        DestinationY = 0;
                    }

                    this.textures[f].UnlockRectangle(0);
                }

                SpriteSheet.UnlockRectangle(0);
                SpriteSheet.Dispose();

                //Create masks for each frame of animation
                this.CollisionRects = new CollisionRectAccess(this.ParentDevice, this.textures, this.Verticies);
            }
        }
        ///<summary>
        ///    @copydoc HardwarePixelBuffer.BlitToMemory
        ///</summary>
        public override void BlitToMemory(BasicBox srcBox, PixelBox dst)
        {
            // Decide on pixel format of temp surface
            PixelFormat tmpFormat = format;
            if (D3DHelper.ConvertEnum(dst.Format) == D3D.Format.Unknown)
                tmpFormat = dst.Format;
            if (surface != null) {
                Debug.Assert(srcBox.Depth == 1 && dst.Depth == 1);
                // Create temp texture
                D3D.Texture tmp =
                    new D3D.Texture(device, dst.Width, dst.Height,
                                    1, // 1 mip level ie topmost, generate no mipmaps
                                    0, D3DHelper.ConvertEnum(tmpFormat),
                                    Pool.Scratch);
                D3D.Surface subSurface = tmp.GetSurfaceLevel(0);
                // Copy texture to this temp surface
                Rectangle destRect, srcRect;
                srcRect = ToD3DRectangle(srcBox);
                destRect = ToD3DRectangleExtent(dst);

                SurfaceLoader.FromSurface(subSurface, destRect, surface, srcRect, Filter.None, 0);

                // Lock temp surface and copy it to memory
                int pitch; // Filled in by D3D
                GraphicsStream data = subSurface.LockRectangle(D3D.LockFlags.ReadOnly, out pitch);
                // Copy it
                PixelBox locked = new PixelBox(dst.Width, dst.Height, dst.Depth, tmpFormat);
                FromD3DLock(locked, pitch, data);
                PixelUtil.BulkPixelConversion(locked, dst);
                subSurface.UnlockRectangle();
                // Release temporary surface and texture
                subSurface.Dispose();
                tmp.Dispose();
            }
            else {
                // Create temp texture
                D3D.VolumeTexture tmp =
                    new D3D.VolumeTexture(device, dst.Width, dst.Height, dst.Depth,
                                          0, D3D.Usage.None,
                                          D3DHelper.ConvertEnum(tmpFormat),
                                          Pool.Scratch);
                D3D.Volume subVolume = tmp.GetVolumeLevel(0);
                // Volume
                D3D.Box ddestBox = ToD3DBoxExtent(dst);
                D3D.Box dsrcBox = ToD3DBox(srcBox);

                VolumeLoader.FromVolume(subVolume, ddestBox, volume, dsrcBox, Filter.None, 0);
                // Lock temp surface and copy it to memory
                D3D.LockedBox lbox; // Filled in by D3D
                GraphicsStream data = subVolume.LockBox(LockFlags.ReadOnly, out lbox);
                // Copy it
                PixelBox locked = new PixelBox(dst.Width, dst.Height, dst.Depth, tmpFormat);
                FromD3DLock(locked, lbox, data);
                PixelUtil.BulkPixelConversion(locked, dst);
                subVolume.UnlockBox();
                // Release temporary surface and texture
                subVolume.Dispose();
                tmp.Dispose();
            }
        }