Esempio n. 1
0
        /// <summary>
        /// Gets an array containing all pixels of the underlying bitmap in Bgra format.
        /// </summary>
        public byte[] GetPixelsAsBgra()
        {
            if (m_wicBitmap == null)
            {
                throw new ObjectDisposedException("WicBitmap");
            }

            byte[] result = new byte[m_wicBitmap.Size.Width * m_wicBitmap.Size.Height * 4];
            m_wicBitmap.CopyPixels(result, m_wicBitmap.Size.Width * 4);
            return(result);
        }
Esempio n. 2
0
        private static System.Drawing.Bitmap ConvertToBitmap(WICBitmap wicBitmap)
        {
            var width = wicBitmap.Size.Width;
            var height = wicBitmap.Size.Height;
            var gdiBitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

            var gdiBitmapData = gdiBitmap.LockBits(
                new System.Drawing.Rectangle(0, 0, gdiBitmap.Width, gdiBitmap.Height),
                ImageLockMode.ReadWrite,
                System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

            var buffer = new int[width * height];
            wicBitmap.CopyPixels(buffer);
            Marshal.Copy(buffer, 0, gdiBitmapData.Scan0, buffer.Length);

            gdiBitmap.UnlockBits(gdiBitmapData);
            return gdiBitmap;
        }
        private static System.Drawing.Bitmap ConvertToBitmap(WICBitmap wicBitmap)
        {
            var width     = wicBitmap.Size.Width;
            var height    = wicBitmap.Size.Height;
            var gdiBitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

            var gdiBitmapData = gdiBitmap.LockBits(
                new System.Drawing.Rectangle(0, 0, gdiBitmap.Width, gdiBitmap.Height),
                ImageLockMode.ReadWrite,
                System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

            var buffer = new int[width * height];

            wicBitmap.CopyPixels(buffer);
            Marshal.Copy(buffer, 0, gdiBitmapData.Scan0, buffer.Length);

            gdiBitmap.UnlockBits(gdiBitmapData);
            return(gdiBitmap);
        }
Esempio n. 4
0
        private unsafe byte *GetBitmapData(string s, out int stride)
        {
            FreeBitmapData();

            var size = GetMeasureString(s);

            var w = (int)(Math.Ceiling(size.Width += 2));
            var h = (int)(Math.Ceiling(size.Height += 2));

            CreateBitmap(w, h);

            _renderTarget.BeginDraw();

            _renderTarget.Clear(TransparentColor);
            _renderTarget.AntialiasMode = AntialiasMode.Aliased;
            textFormat.TextAlignment    = TextAlignment.Center;
            _renderTarget.DrawText(s, textFormat, new RectangleF(0, 0, w, h), _brush);

            _renderTarget.EndDraw();

            //SaveToFile(@"C:\Xamarin\Cocos2D-XNAGraphics\" + s + ".png");

            // Calculate stride of source
            stride = _bitmap.Size.Width * 4;
            // Create data array to hold source pixel data
            //byte[] data = new byte[stride * _bitmap.Size.Height];
            byte[] data = new byte[stride * _bitmap.Size.Height];

            using (var datas = new DataStream(stride * _bitmap.Size.Height, true, true))
            {
                _bitmap.CopyPixels(stride, datas);

                data = datas.ReadRange <byte>(data.Length);
            }

            ReleaseResources();

            pinnedArray = GCHandle.Alloc(data, GCHandleType.Pinned);
            IntPtr pointer = pinnedArray.AddrOfPinnedObject();

            return((byte *)pointer);;
        }
Esempio n. 5
0
        public void PaintToBitmap(Rectangle rc)
        {
            if (bitmap == null || drawType != DrawType.IMAGE)
            {
                return;
            }

            ScDrawNode rootNode;

            graphics.BeginDraw();
            rootNode = reDrawTree.ReCreateReDrawTree(rootScLayer, rc);
            reDrawTree.Draw(graphics);
            graphics.EndDraw();

            if (graphicsType == GraphicsType.D2D && rootNode != null)
            {
                unsafe
                {
                    RectangleF clip = rootNode.clipRect;
                    clip.X      = (int)(clip.X / sizeScale.Width);
                    clip.Y      = (int)(clip.Y / sizeScale.Height);
                    clip.Width  = (int)(clip.Width / sizeScale.Width);
                    clip.Height = (int)(clip.Height / sizeScale.Height);

                    Rectangle  bitmapRect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                    BitmapData srcBmData  = bitmap.LockBits(bitmapRect, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);


                    //DataPointer dataPtr = new DataPointer((IntPtr)srcBmData.Scan0, bitmap.Height * srcBmData.Stride);
                    //wicBitmap.CopyPixels(srcBmData.Stride, dataPtr);
                    byte *ptr = (byte *)srcBmData.Scan0;
                    ptr += (int)clip.Y * srcBmData.Stride + (int)clip.X * 4;
                    DataPointer dataPtr = new DataPointer(ptr, (int)clip.Height * srcBmData.Stride);
                    RawBox      box     = new RawBox((int)clip.X, (int)clip.Y, (int)clip.Width, (int)clip.Height);
                    wicBitmap.CopyPixels(box, srcBmData.Stride, dataPtr);


                    bitmap.UnlockBits(srcBmData);
                }

                //unsafe
                //{
                //    RectangleF clip = rootNode.clipRect;
                //    clip.X = (int)(clip.X / sizeScale.Width);
                //    clip.Y = (int)(clip.Y / sizeScale.Height);
                //    clip.Width = (int)(clip.Width / sizeScale.Width);
                //    clip.Height = (int)(clip.Height / sizeScale.Height);
                //    Rectangle bitmapRect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                //    BitmapData srcBmData = bitmap.LockBits(bitmapRect, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);


                //    BitmapLock bmplock = wicBitmap.Lock(BitmapLockFlags.Read);
                //    DataRectangle dataRect = bmplock.Data;
                //    byte* wicPtr = (byte*)dataRect.DataPointer;

                //    byte* dstScan = (byte*)(srcBmData.Scan0);
                //    byte* dstPtr = dstScan + (int)clip.Y * srcBmData.Stride + (int)clip.X * 4;
                //    byte* srcPtr = wicPtr + (int)clip.Y * bmplock.Stride + (int)clip.X * 4;
                //    int pos;

                //    for (int i = 0; i < (int)clip.Height; i++)
                //    {
                //        pos = i * srcBmData.Stride;

                //        for (int j = 0; j < (int)clip.Width; j++)
                //        {
                //            *(uint*)(dstPtr + pos) = *(uint*)(srcPtr + pos);
                //            pos += 4;
                //        }
                //    }

                //    bmplock.Dispose();
                //    bitmap.UnlockBits(srcBmData);
                //}
            }
        }