当前位置:网站首页>Copy image bitmap by C # memory method

Copy image bitmap by C # memory method

2022-06-23 07:59:00 pcjiushizhu

/**
         *  Memory method to copy pictures 
         * */
        private void copyBitmap(Bitmap bmpSrc, Bitmap bmpDest)
        {
            int w = bmpSrc.Width, h = bmpSrc.Height;
            PixelFormat format = bmpSrc.PixelFormat;

            // Lock the bitmap's bits.   Lock bitmap 
            Rectangle rect = new Rectangle(0, 0, w, h);
            BitmapData bmpDataSrc =
                bmpSrc.LockBits(rect, ImageLockMode.ReadOnly,
                format);
            // Get the address of the first line. Get the first line address 
            IntPtr ptrSrc = bmpDataSrc.Scan0;

            BitmapData bmpDataDest =
                bmpDest.LockBits(rect, ImageLockMode.WriteOnly,
                format);
            IntPtr ptrDest = bmpDataDest.Scan0;


            // Declare an array to hold the bytes of the bitmap. Define an array to hold bitmaps 
            int bytes = Math.Abs(bmpDataSrc.Stride) * h;
            byte[] rgbValues = new byte[bytes];

            // Copy the RGB values into the array. Copy RGB Value to array 
            System.Runtime.InteropServices.Marshal.Copy(ptrSrc, rgbValues, 0, bytes);
            // Copy to new diagram 
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptrDest, bytes);

            // Unlock the bits. Unlock 
            bmpSrc.UnlockBits(bmpDataSrc);
            bmpDest.UnlockBits(bmpDataDest);
        }


// Pay attention to  bmpSRC.Dispose();

 

原网站

版权声明
本文为[pcjiushizhu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230729015662.html