示例#1
0
        public static int[] CopyImgBuffer(this MemBitmap memBmp, int width, int height, bool flipY = false)
        {
            //calculate stride for the width

            int destStride = MemBitmap.CalculateStride(width, PixelFormat.ARGB32);
            int newBmpW    = destStride / 4;

            int[] buff2 = new int[newBmpW * height];
            unsafe
            {
                using (TempMemPtr srcBufferPtr = MemBitmap.GetBufferPtr(memBmp))
                {
                    byte *srcBuffer = (byte *)srcBufferPtr.Ptr;
                    int   srcIndex  = 0;
                    int   srcStride = memBmp.Stride;

                    if (flipY)
                    {
                        fixed(int *destHead = &buff2[0])
                        {
                            byte *destHead2 = (byte *)destHead;

                            srcBuffer += (height - 1) * srcStride;
                            for (int line = 0; line < height; ++line)
                            {
                                //System.Runtime.InteropServices.Marshal.Copy(srcBuffer, srcIndex, (IntPtr)destHead2, destStride);
                                MemMx.memcpy((byte *)destHead2, srcBuffer + srcIndex, destStride);
                                srcIndex  -= srcStride;
                                destHead2 += destStride;
                            }
                        }
                    }
                    else
                    {
                        fixed(int *destHead = &buff2[0])
                        {
                            byte *destHead2 = (byte *)destHead;

                            for (int line = 0; line < height; ++line)
                            {
                                //System.Runtime.InteropServices.Marshal.Copy(srcBuffer, srcIndex, (IntPtr)destHead2, destStride);
                                MemMx.memcpy((byte *)destHead2, srcBuffer + srcIndex, destStride);
                                srcIndex  += srcStride;
                                destHead2 += destStride;
                            }
                        }
                    }
                }
            }
            return(buff2);
        }
示例#2
0
        /// <summary>
        /// copy from mem image direct to hBmpScan0
        /// </summary>
        /// <param name="srcMemBmp"></param>
        /// <param name="dstHBmpScan0"></param>
        public static void CopyToWindowsBitmapSameSize(
            MemBitmap srcMemBmp,
            IntPtr dstHBmpScan0)
        {
            //1st, fast
            //byte[] rawBuffer = ActualImage.GetBuffer(actualImage);

            TempMemPtr memPtr = MemBitmap.GetBufferPtr(srcMemBmp);

            unsafe
            {
                MemMx.memcpy((byte *)dstHBmpScan0, (byte *)memPtr.Ptr, srcMemBmp.Stride * srcMemBmp.Height);
            }
            //System.Runtime.InteropServices.Marshal.Copy(rawBuffer, 0,
            //   hBmpScan0, rawBuffer.Length);

            memPtr.Dispose();
        }
示例#3
0
        public static MemBitmap CopyImgBuffer(this MemBitmap src, int srcX, int srcY, int srcW, int srcH)
        {
            //simple copy

#if DEBUG
            Rectangle orgSourceRect = new Rectangle(0, 0, src.Width, src.Height);
            Rectangle requestRect   = new Rectangle(srcX, srcY, srcW, srcH);
#endif

            Rectangle toCopyRect = Rectangle.Intersect(
                new Rectangle(0, 0, src.Width, src.Height),               //orgSourceRect
                new Rectangle(srcX, srcY, srcW, srcH));                   //reqstRect

            if (toCopyRect.Width == 0 || toCopyRect.Height == 0)
            {
                return(null);
            }
            //-----
            MemBitmap copyBmp = new MemBitmap(toCopyRect.Width, toCopyRect.Height);
            unsafe
            {
                using (TempMemPtr srcBufferPtr = MemBitmap.GetBufferPtr(src))
                    using (TempMemPtr dstBufferPtr = MemBitmap.GetBufferPtr(copyBmp))
                    {
                        int *srcPtr  = (int *)srcBufferPtr.Ptr;
                        int *dstPtr  = (int *)dstBufferPtr.Ptr;
                        int  lineEnd = srcY + srcH;
                        int  orgSrcW = src.Width;
                        for (int line = toCopyRect.Top; line < toCopyRect.Bottom; ++line)
                        {
                            MemMx.memcpy((byte *)dstPtr, (byte *)(srcPtr + ((line * orgSrcW) + toCopyRect.Left)), toCopyRect.Width * 4);
                            dstPtr += toCopyRect.Width;
                        }
                    }
            }

            return(copyBmp);
        }
示例#4
0
        /////////////////////////////////////////////////////////////////////////////////////
        public static void CopyToGdiPlusBitmapSameSizeNotFlip(
            MemBitmap srcMemBmp,
            Bitmap dstBitmap)
        {
            //agg store image buffer head-down
            //when copy to window bmp we here to flip
            //style1: copy row by row *** (fastest)***
            {
                //System.GC.Collect();
                //System.Diagnostics.Stopwatch sss = new System.Diagnostics.Stopwatch();
                //sss.Start();
                //for (int i = 0; i < 1000; ++i)
                //{
                int        h           = dstBitmap.Height;
                int        w           = dstBitmap.Width;
                BitmapData bitmapData1 = dstBitmap.LockBits(
                    new Rectangle(0, 0,
                                  w,
                                  h),
                    System.Drawing.Imaging.ImageLockMode.ReadWrite,
                    dstBitmap.PixelFormat);
                IntPtr scan0  = bitmapData1.Scan0;
                int    stride = bitmapData1.Stride;
                //byte[] srcBuffer = ActualImage.GetBuffer(actualImage);
                TempMemPtr srcBufferPtr = MemBitmap.GetBufferPtr(srcMemBmp);
                unsafe
                {
                    //fixed (byte* bufferH = &srcBuffer[0])
                    byte *srcBufferH = (byte *)srcBufferPtr.Ptr;
                    {
                        byte *target     = (byte *)scan0;
                        int   startRowAt = 0;
                        for (int y = 0; y < h; ++y)
                        {
                            //byte* src = bufferH + ((y - 1) * stride);
                            byte *src = srcBufferH + startRowAt;
                            //System.Runtime.InteropServices.Marshal.Copy(
                            //   srcBuffer,//src
                            //   startRowAt,
                            //   (IntPtr)target,
                            //   stride);
                            MemMx.memcpy(target, src, stride);

                            startRowAt += stride;
                            target     += stride;
                        }
                    }
                }
                srcBufferPtr.Dispose();
                dstBitmap.UnlockBits(bitmapData1);
                //}
                //sss.Stop();
                //long ms = sss.ElapsedMilliseconds;
            }
            //-----------------------------------
            //style2: copy all, then flip again
            //{
            //    System.GC.Collect();
            //    System.Diagnostics.Stopwatch sss = new System.Diagnostics.Stopwatch();
            //    sss.Start();
            //    for (int i = 0; i < 1000; ++i)
            //    {
            //        byte[] rawBuffer = ActualImage.GetBuffer(actualImage);
            //        var bmpdata = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
            //          System.Drawing.Imaging.ImageLockMode.ReadOnly,
            //         bitmap.PixelFormat);


            //        System.Runtime.InteropServices.Marshal.Copy(rawBuffer, 0,
            //            bmpdata.Scan0, rawBuffer.Length);

            //        bitmap.UnlockBits(bmpdata);
            //        bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
            //    }

            //    sss.Stop();
            //    long ms = sss.ElapsedMilliseconds;
            //}
            //-----------------------------------

            //-----------------------------------
            //style3: copy row by row +
            //{
            //    System.GC.Collect();
            //    System.Diagnostics.Stopwatch sss = new System.Diagnostics.Stopwatch();
            //    sss.Start();
            //    for (int i = 0; i < 1000; ++i)
            //    {
            //        int h = bitmap.Height;
            //        int w = bitmap.Width;
            //        BitmapData bitmapData1 = bitmap.LockBits(
            //                  new Rectangle(0, 0,
            //                      w,
            //                      h),
            //                      System.Drawing.Imaging.ImageLockMode.ReadWrite,
            //                      bitmap.PixelFormat);
            //        IntPtr scan0 = bitmapData1.Scan0;
            //        int stride = bitmapData1.Stride;
            //        byte[] buffer = ActualImage.GetBuffer(actualImage);
            //        unsafe
            //        {
            //            fixed (byte* bufferH = &buffer[0])
            //            {
            //                byte* target = (byte*)scan0;
            //                for (int y = h; y > 0; --y)
            //                {
            //                    byte* src = bufferH + ((y - 1) * stride);
            //                    for (int n = stride - 1; n >= 0; --n)
            //                    {
            //                        *target = *src;
            //                        target++;
            //                        src++;
            //                    }
            //                }
            //            }
            //        }
            //        bitmap.UnlockBits(bitmapData1);
            //    }
            //    sss.Stop();
            //    long ms = sss.ElapsedMilliseconds;
            //}
        }