예제 #1
0
        /// <summary>
        /// Creates a new surface based on the PixelFormat of the Bitmap.
        /// </summary>
        /// <param name="image">The input Bitmap.</param>
        /// <param name="imageMode">The ImageMode of the current surface.</param>
        /// <returns></returns>
        internal static unsafe SurfaceBase CreateFromGdipBitmap(Bitmap image, out ImageModes imageMode)
        {
            int width  = image.Width;
            int height = image.Height;

            imageMode = ImageModes.RGB;
            SurfaceBGRA32 surface = new SurfaceBGRA32(width, height, image.HorizontalResolution, image.VerticalResolution);

            using (Bitmap temp = new Bitmap(image)) // Copy the image to remove any invalid meta-data that causes LockBits to fail.
            {
                BitmapData data = temp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

                try
                {
                    byte *scan0  = (byte *)data.Scan0.ToPointer();
                    int   stride = data.Stride;

                    ulong length = (ulong)width * 4UL;
                    for (int y = 0; y < height; y++)
                    {
                        ImageSurfaceMemory.Copy(surface.GetRowAddressUnchecked(y), scan0 + (y * stride), length);
                    }
                }
                finally
                {
                    temp.UnlockBits(data);
                }
            }


            return(surface);
        }
예제 #2
0
        public void CopySurface(SurfaceBase source)
        {
            if (width == source.width &&
                height == source.height &&
                stride == source.stride &&
                (width * bytesPerPixel) == stride)
            {
                unsafe
                {
                    ImageSurfaceMemory.Copy(scan0.VoidStar,
                                            source.Scan0.VoidStar,
                                            ((ulong)(height - 1) * (ulong)stride) + ((ulong)width * (ulong)bytesPerPixel));
                }
            }
            else
            {
                int copyWidth  = Math.Min(width, source.Width);
                int copyHeight = Math.Min(height, source.Height);

                unsafe
                {
                    for (int y = 0; y < copyHeight; ++y)
                    {
                        ImageSurfaceMemory.Copy(GetRowAddressUnchecked(y), source.GetRowAddressUnchecked(y), (ulong)copyWidth * (ulong)bytesPerPixel);
                    }
                }
            }
        }