Ejemplo n.º 1
0
        async Task SaveToFile(SharpDX.WIC.Bitmap bitmap)         // debugging helper method, remove when no longer needed.
        {
            var width  = bitmap.Size.Width;
            var height = bitmap.Size.Height;
            var pixels = new SharpDX.ColorBGRA[bitmap.Size.Width * bitmap.Size.Height];

            bitmap.CopyPixels(pixels);
            var bytes = new byte[width * height * 4];

            for (var i = 0; i < pixels.Length; ++i)
            {
                bytes[i * 4 + 0] = (byte)pixels[i].B;
                bytes[i * 4 + 1] = (byte)pixels[i].G;
                bytes[i * 4 + 2] = (byte)pixels[i].R;
                bytes[i * 4 + 3] = (byte)pixels[i].A;
            }

            var folder = ApplicationData.Current.LocalFolder;
            var file   = await folder.CreateFileAsync("test.png", CreationCollisionOption.ReplaceExisting);

            using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);

                encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                     BitmapAlphaMode.Ignore,
                                     (uint)bitmap.Size.Width, (uint)bitmap.Size.Height,
                                     96, 96, bytes);

                await encoder.FlushAsync();
            }
        }
Ejemplo n.º 2
0
 private void CopyWicPixelsToBuffer(RawBox box)
 {
     if (_bitmap != null)
     {
         lock (_bitmapLock)
         {
             _bitmap.CopyPixels(box, _intBuffer);
         }
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns a System.Drawing.Bitmap object that is a copy of the image stored in this
        /// D2dWicGraphics object.</summary>
        /// <returns>System.Drawing.Bitmap copy of image</returns>
        public System.Drawing.Bitmap Copy()
        {
            var bitmap = new System.Drawing.Bitmap(m_wicBitmap.Size.Width, m_wicBitmap.Size.Height);

            System.Drawing.Imaging.BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                                                                     System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat);
            m_wicBitmap.CopyPixels(data.Stride, data.Scan0, data.Height * data.Stride);
            bitmap.UnlockBits(data);
            return(bitmap);
        }
        public static Bitmap CopyWicBitmapToBitmap(WicBitmap wicBitmap, int width, int height, object wicBitmapLock)
        {
            var pixelData = new byte[width * height * 4];

            lock (wicBitmapLock)
            {
                wicBitmap.CopyPixels(pixelData, width * 4);
            }
            var bitmap        = new Bitmap(width, height);
            var lockRectangle = new Rectangle(0, 0, width, height);
            var pixelFormat   = System.Drawing.Imaging.PixelFormat.Format32bppPArgb;
            var lockFlags     = ImageLockMode.WriteOnly;
            var bitmapData    = bitmap.LockBits(lockRectangle, lockFlags, pixelFormat);

            Marshal.Copy(pixelData, 0, bitmapData.Scan0, pixelData.Length);
            bitmap.UnlockBits(bitmapData);
            return(bitmap);
        }
Ejemplo n.º 5
0
        public byte[] GetPixels()
        {
            if (bitmap == null)
            {
                throw new InvalidOperationException("Cannot GetPixels on SwapChain graphics");
            }

            var width    = bitmap.Size.Width;
            var numBytes = width * bitmap.Size.Height * 4;
            var bytes    = new byte[numBytes];

            unsafe
            {
                fixed(byte *pb = bytes)
                {
                    bitmap.CopyPixels(width * 4, new IntPtr(pb), numBytes);
                }
            }
            return(bytes);
        }